diff --git a/src/Concerns/MakesAssertions.php b/src/Concerns/MakesAssertions.php index 0ca2ff633..cb520653b 100644 --- a/src/Concerns/MakesAssertions.php +++ b/src/Concerns/MakesAssertions.php @@ -249,6 +249,26 @@ public function assertSeeNothingIn($selector) return $this; } + /** + * Assert that a given element is present a given amount of times. + * + * @param string $selector + * @param int $expected + * @return $this + */ + public function assertCount($selector, $expected) + { + $fullSelector = $this->resolver->format($selector); + + PHPUnit::assertCount( + $expected, + $this->resolver->all($selector), + "Expected element [{$fullSelector}] exactly {$expected} times." + ); + + return $this; + } + /** * Assert that the given JavaScript expression evaluates to the given value. * diff --git a/tests/Unit/MakesAssertionsTest.php b/tests/Unit/MakesAssertionsTest.php index ebea4026a..22ba7b44d 100644 --- a/tests/Unit/MakesAssertionsTest.php +++ b/tests/Unit/MakesAssertionsTest.php @@ -1257,6 +1257,32 @@ public function test_assert_vue_contains_with_no_result() } } + public function test_assert_count() + { + $driver = m::mock(stdClass::class); + + $element1 = m::mock(RemoteWebElement::class); + $element2 = m::mock(RemoteWebElement::class); + + $resolver = m::mock(stdClass::class); + $resolver->shouldReceive('format')->with('foo')->andReturn('body foo'); + $resolver->shouldReceive('all')->with('foo')->andReturn([$element1, $element2]); + + $browser = new Browser($driver, $resolver); + + $browser->assertCount('foo', 2); + + try { + $browser->assertCount('foo', 3); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertStringContainsString( + 'Expected element [body foo] exactly 3 times.', + $e->getMessage() + ); + } + } + public function test_assert_script() { $driver = m::mock(stdClass::class);