diff --git a/src/Api/Concerns/InteractsWithElements.php b/src/Api/Concerns/InteractsWithElements.php index 24566e7e..6dbc0273 100644 --- a/src/Api/Concerns/InteractsWithElements.php +++ b/src/Api/Concerns/InteractsWithElements.php @@ -12,11 +12,11 @@ trait InteractsWithElements { /** - * Click the link with the given text. + * Click an element using smart selector detection (id, class, data-test, submit, text, etc). */ - public function click(string $text): self + public function click(string $selector): self { - $this->guessLocator($text)->click(); + $this->guessLocator($selector)->click(); return $this; } diff --git a/tests/Browser/Webpage/ClickTest.php b/tests/Browser/Webpage/ClickTest.php index 0396359e..80bcb458 100644 --- a/tests/Browser/Webpage/ClickTest.php +++ b/tests/Browser/Webpage/ClickTest.php @@ -73,3 +73,18 @@ '[name$="test"]', 'button[name="test"]', ]); + +it('can click on the element using the data-test selector', function (): void { + Route::get('/', fn (): string => ' +
+ Click Me +
+
+ '); + + $page = visit('/'); + + $page->click('@test-selector'); + + expect($page->text('#result'))->toBe('Selector Clicked'); +});