diff --git a/src/Api/Webpage.php b/src/Api/Webpage.php index d42876a7..23c1c6ec 100644 --- a/src/Api/Webpage.php +++ b/src/Api/Webpage.php @@ -95,6 +95,16 @@ public function value(string $selector): string return $this->guessLocator($selector)->inputValue(); } + /** + * Get a locator for the given selector for use in custom assertions. + * + * When multiple elements match, use `->all()` to iterate or `->first()` / `->nth()` for one. + */ + public function element(string $selector, ?string $value = null): Locator + { + return $this->guessLocator($selector, $value); + } + /** * Gets the locator for the given selector. */ diff --git a/tests/Browser/Webpage/ElementTest.php b/tests/Browser/Webpage/ElementTest.php new file mode 100644 index 00000000..67fcb9a1 --- /dev/null +++ b/tests/Browser/Webpage/ElementTest.php @@ -0,0 +1,28 @@ + ' + wow + wow + wow + '); + + $page = visit('/'); + + foreach ($page->element('.link')->all() as $element) { + expect($element->textContent())->toBe('wow'); + } +}); + +it('may assert text on the first matching element', function (): void { + Route::get('/', fn (): string => ' + wow + other + '); + + $page = visit('/'); + + expect($page->element('.link')->first()->textContent())->toBe('wow'); +});