Skip to content

Commit 8d24059

Browse files
author
Giulio Troccoli-Allard
committed
Add assertAttribute method
1 parent 5c29a4c commit 8d24059

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Concerns/MakesAssertions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,33 @@ 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+
551578
/**
552579
* Assert that the element with the given selector is visible.
553580
*

tests/MakesAssertionsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,43 @@ 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+
5693
public function test_assert_present()
5794
{
5895
$driver = m::mock(stdClass::class);

0 commit comments

Comments
 (0)