Skip to content

Commit 62d8f73

Browse files
authored
Merge pull request #751 from troccoli/add-assert-attribute-methods
[5.x] Add assert attribute methods
2 parents 5c29a4c + aed4cbb commit 62d8f73

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

src/Concerns/MakesAssertions.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,59 @@ public function assertValue($selector, $value)
548548
return $this;
549549
}
550550

551+
/**
552+
* Assert that the element at the given selector has the given attribute value.
553+
*
554+
* @param string $selector
555+
* @param string $attribute
556+
* @param string $value
557+
* @return $this
558+
*/
559+
public function assertAttribute($selector, $attribute, $value)
560+
{
561+
$fullSelector = $this->resolver->format($selector);
562+
563+
$actual = $this->resolver->findOrFail($selector)->getAttribute($attribute);
564+
565+
PHPUnit::assertNotNull(
566+
$actual,
567+
"Did not see expected attribute [{$attribute}] within element [{$fullSelector}]."
568+
);
569+
570+
PHPUnit::assertEquals(
571+
$value, $actual,
572+
"Expected '$attribute' attribute [{$value}] does not equal actual value [$actual]."
573+
);
574+
575+
return $this;
576+
}
577+
578+
/**
579+
* Assert that the element at the given selector has the given data attribute value.
580+
*
581+
* @param string $selector
582+
* @param string $attribute
583+
* @param string $value
584+
* @return $this
585+
*/
586+
public function assertDataAttribute($selector, $attribute, $value)
587+
{
588+
return $this->assertAttribute($selector, 'data-' . $attribute, $value);
589+
}
590+
591+
/**
592+
* Assert that the element at the given selector has the given aria attribute value.
593+
*
594+
* @param string $selector
595+
* @param string $attribute
596+
* @param string $value
597+
* @return $this
598+
*/
599+
public function assertAriaAttribute($selector, $attribute, $value)
600+
{
601+
return $this->assertAttribute($selector, 'aria-' . $attribute, $value);
602+
}
603+
551604
/**
552605
* Assert that the element with the given selector is visible.
553606
*

tests/MakesAssertionsTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,117 @@ public function test_assert_title_contains()
5353
}
5454
}
5555

56+
public function test_assert_attribute()
57+
{
58+
$driver = m::mock(stdClass::class);
59+
$element = m::mock(stdClass::class);
60+
$element->shouldReceive('getAttribute')->with('bar')->andReturn(
61+
'joe',
62+
null,
63+
'sue'
64+
);
65+
$resolver = m::mock(stdClass::class);
66+
$resolver->shouldReceive('format')->with('foo')->andReturn('Foo');
67+
$resolver->shouldReceive('findOrFail')->with('foo')->andReturn($element);
68+
$browser = new Browser($driver, $resolver);
69+
70+
$browser->assertAttribute('foo', 'bar', 'joe');
71+
72+
try {
73+
$browser->assertAttribute('foo', 'bar', 'joe');
74+
$this->fail();
75+
} catch (ExpectationFailedException $e) {
76+
$this->assertStringContainsString(
77+
"Did not see expected attribute [bar] within element [Foo].",
78+
$e->getMessage()
79+
);
80+
}
81+
82+
try {
83+
$browser->assertAttribute('foo', 'bar', 'joe');
84+
$this->fail();
85+
} catch (ExpectationFailedException $e) {
86+
$this->assertStringContainsString(
87+
"Expected 'bar' attribute [joe] does not equal actual value [sue].",
88+
$e->getMessage()
89+
);
90+
}
91+
}
92+
93+
public function test_assert_data_attribute()
94+
{
95+
$driver = m::mock(stdClass::class);
96+
$element = m::mock(stdClass::class);
97+
$element->shouldReceive('getAttribute')->with('data-bar')->andReturn(
98+
'joe',
99+
null,
100+
'sue'
101+
);
102+
$resolver = m::mock(stdClass::class);
103+
$resolver->shouldReceive('format')->with('foo')->andReturn('Foo');
104+
$resolver->shouldReceive('findOrFail')->with('foo')->andReturn($element);
105+
$browser = new Browser($driver, $resolver);
106+
107+
$browser->assertDataAttribute('foo', 'bar', 'joe');
108+
109+
try {
110+
$browser->assertDataAttribute('foo', 'bar', 'joe');
111+
$this->fail();
112+
} catch (ExpectationFailedException $e) {
113+
$this->assertStringContainsString(
114+
"Did not see expected attribute [data-bar] within element [Foo].",
115+
$e->getMessage()
116+
);
117+
}
118+
119+
try {
120+
$browser->assertDataAttribute('foo', 'bar', 'joe');
121+
$this->fail();
122+
} catch (ExpectationFailedException $e) {
123+
$this->assertStringContainsString(
124+
"Expected 'data-bar' attribute [joe] does not equal actual value [sue].",
125+
$e->getMessage()
126+
);
127+
}
128+
}
129+
130+
public function test_assert_aria_attribute()
131+
{
132+
$driver = m::mock(stdClass::class);
133+
$element = m::mock(stdClass::class);
134+
$element->shouldReceive('getAttribute')->with('aria-bar')->andReturn(
135+
'joe',
136+
null,
137+
'sue'
138+
);
139+
$resolver = m::mock(stdClass::class);
140+
$resolver->shouldReceive('format')->with('foo')->andReturn('Foo');
141+
$resolver->shouldReceive('findOrFail')->with('foo')->andReturn($element);
142+
$browser = new Browser($driver, $resolver);
143+
144+
$browser->assertAriaAttribute('foo', 'bar', 'joe');
145+
146+
try {
147+
$browser->assertAriaAttribute('foo', 'bar', 'joe');
148+
$this->fail();
149+
} catch (ExpectationFailedException $e) {
150+
$this->assertStringContainsString(
151+
"Did not see expected attribute [aria-bar] within element [Foo].",
152+
$e->getMessage()
153+
);
154+
}
155+
156+
try {
157+
$browser->assertAriaAttribute('foo', 'bar', 'joe');
158+
$this->fail();
159+
} catch (ExpectationFailedException $e) {
160+
$this->assertStringContainsString(
161+
"Expected 'aria-bar' attribute [joe] does not equal actual value [sue].",
162+
$e->getMessage()
163+
);
164+
}
165+
}
166+
56167
public function test_assert_present()
57168
{
58169
$driver = m::mock(stdClass::class);

0 commit comments

Comments
 (0)