Skip to content

Commit d456cf2

Browse files
author
Giulio Troccoli-Allard
committed
Add assertDataAttribute method
1 parent 8d24059 commit d456cf2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Concerns/MakesAssertions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,19 @@ public function assertAttribute($selector, $attribute, $value)
575575
return $this;
576576
}
577577

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+
578591
/**
579592
* Assert that the element with the given selector is visible.
580593
*

tests/MakesAssertionsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ public function test_assert_attribute()
9090
}
9191
}
9292

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+
93130
public function test_assert_present()
94131
{
95132
$driver = m::mock(stdClass::class);

0 commit comments

Comments
 (0)