From 4e7a035aa8be63412f2c41d180d665b864e9830f Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 3 Oct 2025 18:46:12 -0300 Subject: [PATCH 1/2] Fixes the assertSeeIn when there are multiple elements that match --- src/Api/Concerns/MakesElementAssertions.php | 8 +++++++- tests/Browser/Webpage/AssertSeeInTest.php | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Api/Concerns/MakesElementAssertions.php b/src/Api/Concerns/MakesElementAssertions.php index 0270add7..a0ed677b 100644 --- a/src/Api/Concerns/MakesElementAssertions.php +++ b/src/Api/Concerns/MakesElementAssertions.php @@ -96,7 +96,13 @@ public function assertSeeIn(string $selector, string|int|float $text): Webpage $locator = $this->guessLocator($selector); - expect($locator->getByText($text)->isVisible())->toBeTrue("Expected to see text [{$text}] within element [{$selector}] on the page initially with the url [{$this->initialUrl}], but it was not found or not visible."); + $entries = $this->page->unstrict(fn () => $locator->getByText($text)); + + expect($entries->count())->toBeGreaterThan(0, "Expected to see text [{$text}] within element [{$selector}] on the page initially with the url [{$this->initialUrl}], but it was not found."); + + foreach ($entries->all() as $entry) { + expect($entry->isVisible())->toBeTrue("Expected to see text [{$text}] within element [{$selector}] on the page initially with the url [{$this->initialUrl}], but it was not found or not visible."); + } return $this; } diff --git a/tests/Browser/Webpage/AssertSeeInTest.php b/tests/Browser/Webpage/AssertSeeInTest.php index 8c45298a..416176aa 100644 --- a/tests/Browser/Webpage/AssertSeeInTest.php +++ b/tests/Browser/Webpage/AssertSeeInTest.php @@ -35,3 +35,11 @@ $page->assertDontSeeIn('#content', 'Hello World'); })->throws(ExpectationFailedException::class); + +it('may assert text is in a selector when contains multiple', function (): void { + Route::get('/', fn (): string => '
HelloHelloHello
'); + + $page = visit('/'); + + $page->assertSeeIn('@content', 'Hello'); +}); From f1bb4502785f100760d1efcaa2b08b7cba30e73d Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 3 Oct 2025 18:48:21 -0300 Subject: [PATCH 2/2] Tweaks the implementation to match the assertSee one --- src/Api/Concerns/MakesElementAssertions.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Api/Concerns/MakesElementAssertions.php b/src/Api/Concerns/MakesElementAssertions.php index a0ed677b..43809446 100644 --- a/src/Api/Concerns/MakesElementAssertions.php +++ b/src/Api/Concerns/MakesElementAssertions.php @@ -98,13 +98,17 @@ public function assertSeeIn(string $selector, string|int|float $text): Webpage $entries = $this->page->unstrict(fn () => $locator->getByText($text)); - expect($entries->count())->toBeGreaterThan(0, "Expected to see text [{$text}] within element [{$selector}] on the page initially with the url [{$this->initialUrl}], but it was not found."); - foreach ($entries->all() as $entry) { - expect($entry->isVisible())->toBeTrue("Expected to see text [{$text}] within element [{$selector}] on the page initially with the url [{$this->initialUrl}], but it was not found or not visible."); + if ($entry->isVisible()) { + expect(true)->toBeTrue(); + + return $this; + } } - return $this; + throw new ExpectationFailedException( + "Expected to see text [{$text}] within element [{$selector}] on the page initially with the url [{$this->initialUrl}], but it was not found or not visible.", + ); } /** @@ -459,7 +463,7 @@ public function assertAriaAttribute(string $selector, string $attribute, string| { $value = (string) $value; - return $this->assertAttribute($selector, 'aria-'.$attribute, $value); + return $this->assertAttribute($selector, 'aria-' . $attribute, $value); } /** @@ -469,7 +473,7 @@ public function assertDataAttribute(string $selector, string $attribute, string| { $value = (string) $value; - return $this->assertAttribute($selector, 'data-'.$attribute, $value); + return $this->assertAttribute($selector, 'data-' . $attribute, $value); } /**