Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ $browser
->assertNotSee('some text')
->assertSeeIn('h1', 'some text')
->assertNotSeeIn('h1', 'some text')

// pass many selectors to check the same text in each of them
->assertSeeIn(['title', 'h1'], 'some text')
->assertNotSeeIn(['title', 'h1'], 'some text')
->assertSeeElement('h1')
->assertNotSeeElement('h1')
->assertElementCount('ul li', 2)
Expand Down
26 changes: 22 additions & 4 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,29 @@ final public function assertNotSee(string $expected): self
}

/**
* @param string|iterable<string> $selector One or many css selectors, all of which must contain the text
*
* @return static
*/
final public function assertSeeIn(string $selector, string $expected): self
final public function assertSeeIn(string|iterable $selector, string $expected): self
{
$this->session()->assert()->elementTextContains('css', $selector, $expected);
foreach (self::normalizeSelectors($selector) as $css) {
$this->session()->assert()->elementTextContains('css', $css, $expected);
}

return $this;
}

/**
* @param string|iterable<string> $selector One or many css selectors, none of which may contain the text
*
* @return static
*/
final public function assertNotSeeIn(string $selector, string $expected): self
final public function assertNotSeeIn(string|iterable $selector, string $expected): self
{
$this->session()->assert()->elementTextNotContains('css', $selector, $expected);
foreach (self::normalizeSelectors($selector) as $css) {
$this->session()->assert()->elementTextNotContains('css', $css, $expected);
}

return $this;
}
Expand Down Expand Up @@ -879,4 +887,14 @@ private function securityToken(): ?TokenInterface

return $container->get('security.token_storage')->getToken();
}

/**
* @param string|iterable<string> $selector
*
* @return iterable<string>
*/
private static function normalizeSelectors(string|iterable $selector): iterable
{
return \is_string($selector) ? [$selector] : $selector;
}
}
29 changes: 29 additions & 0 deletions tests/BrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,35 @@ public function html_assertions(): void
;
}

/**
* @test
*/
#[Test]
public function see_in_assertions_accept_many_selectors(): void
{
$this->browser()
->visit('/page1')
->assertSeeIn(['h1', 'body'], 'h1 title')
->assertNotSeeIn(['h1', 'title'], 'invalid text')
;

// every selector must match, so one that does not fails the assertion
Assert::that(function() {
$this->browser()
->visit('/page1')
->assertSeeIn(['h1', 'title'], 'h1 title')
;
})->throws(AssertionFailedError::class);

// and none may match for the negative one
Assert::that(function() {
$this->browser()
->visit('/page1')
->assertNotSeeIn(['title', 'h1'], 'h1 title')
;
})->throws(AssertionFailedError::class);
}

/**
* @test
*/
Expand Down
Loading