From 58b6246cdf82cd546798c5749ebbe1b1b32dc6dc Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 2 Oct 2021 11:16:18 -0700 Subject: [PATCH 1/4] Add Browser::clockOnElement() --- src/Browser.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Browser.php b/src/Browser.php index ac96bdc..190bb77 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -274,6 +274,16 @@ final public function click(string $selector): self return $this; } + /** + * @return static + */ + public function clickOnElement(string $selector): self + { + $this->documentElement()->findById($selector)->click(); + + return $this; + } + /** * @return static */ From 4641800480f918be9fe188fa9ac09b2c343d2484 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 2 Oct 2021 11:28:52 -0700 Subject: [PATCH 2/4] Allow CSS selectors --- src/Browser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Browser.php b/src/Browser.php index 190bb77..c7c9cb1 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -275,11 +275,11 @@ final public function click(string $selector): self } /** - * @return static + * @param string $selector Any CSS selector is valid */ public function clickOnElement(string $selector): self { - $this->documentElement()->findById($selector)->click(); + $this->documentElement()->find('css', $selector)->click(); return $this; } From 3420ca10c2bd118b55d5e0003d54cc31c7a343ea Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 2 Oct 2021 11:31:39 -0700 Subject: [PATCH 3/4] Added a small test --- tests/BrowserTests.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/BrowserTests.php b/tests/BrowserTests.php index 5934e4f..572aae8 100644 --- a/tests/BrowserTests.php +++ b/tests/BrowserTests.php @@ -336,6 +336,18 @@ public function link_action(): void ; } + /** + * @test + */ + public function click_on_element(): void + { + $this->browser() + ->visit('/page1') + ->clickOnElement('#link a') + ->assertOn('/page2') + ; + } + /** * @test */ From fdb2eecb3199ef5054990f51e82efd060d64a7a7 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 2 Oct 2021 14:03:49 -0700 Subject: [PATCH 4/4] Merge into click() --- src/Browser.php | 18 +++++++----------- tests/BrowserTests.php | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Browser.php b/src/Browser.php index c7c9cb1..9703927 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -260,6 +260,8 @@ final public function attachFile(string $selector, $filename): self } /** + * Click on a button, link or any DOM element. + * * @return static */ final public function click(string $selector): self @@ -268,22 +270,16 @@ final public function click(string $selector): self $this->documentElement()->pressButton($selector); } catch (ElementNotFoundException $e) { // try link - $this->documentElement()->clickLink($selector); + try { + $this->documentElement()->clickLink($selector); + } catch (ElementNotFoundException $e) { + $this->documentElement()->find('css', $selector)->click(); + } } return $this; } - /** - * @param string $selector Any CSS selector is valid - */ - public function clickOnElement(string $selector): self - { - $this->documentElement()->find('css', $selector)->click(); - - return $this; - } - /** * @return static */ diff --git a/tests/BrowserTests.php b/tests/BrowserTests.php index 572aae8..a733322 100644 --- a/tests/BrowserTests.php +++ b/tests/BrowserTests.php @@ -343,7 +343,7 @@ public function click_on_element(): void { $this->browser() ->visit('/page1') - ->clickOnElement('#link a') + ->click('#link a') ->assertOn('/page2') ; }