From 14668279c617931b7635c9da4edf0f6c729f47d8 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 26 Aug 2022 15:32:09 +0200 Subject: [PATCH] [feature] add doubleClick and rightClick to PantherBrowser (#104) * feat: add doubleClick and rightClick to PantherBrowser * feat: migrate phpunit config-file to version 9 * Update src/Browser/PantherBrowser.php Co-authored-by: Kevin Bond * Update src/Browser/PantherBrowser.php Co-authored-by: Kevin Bond * Update src/Browser.php Co-authored-by: Kevin Bond Co-authored-by: Christopher Georg Co-authored-by: Kevin Bond --- README.md | 3 + src/Browser.php | 60 +++++++++++--------- src/Browser/PantherBrowser.php | 16 ++++++ src/Browser/Session/Driver/PantherDriver.php | 12 ++++ tests/Fixture/files/page1.html | 2 + tests/PantherBrowserTest.php | 36 ++++++++++++ 6 files changed, 103 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 754214b..5da17c7 100644 --- a/README.md +++ b/README.md @@ -480,6 +480,9 @@ $browser ->waitUntilSeeIn('.selector', 'some text') ->waitUntilNotSeeIn('.selector', 'some text') + ->doubleClick('Link') + ->rightClick('Link') + // dump() the browser's console error log ->dumpConsoleLog() diff --git a/src/Browser.php b/src/Browser.php index 8dd3101..b7edf7a 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -2,6 +2,7 @@ namespace Zenstruck; +use Behat\Mink\Element\NodeElement; use Symfony\Component\BrowserKit\AbstractBrowser; use Symfony\Component\BrowserKit\CookieJar; use Symfony\Component\DomCrawler\Crawler; @@ -296,32 +297,7 @@ final public function attachFile(string $selector, $filename): self */ final public function click(string $selector): self { - // try button - $element = $this->session()->page()->findButton($selector); - - if (!$element) { - // try link - $element = $this->session()->page()->findLink($selector); - } - - if (!$element) { - // try by css - $element = $this->session()->page()->find('css', $selector); - } - - if (!$element) { - Assert::fail('Clickable element "%s" not found.', [$selector]); - } - - if (!$element->isVisible()) { - Assert::fail('Clickable element "%s" is not visible.', [$selector]); - } - - if ($button = $this->session()->page()->findButton($selector)) { - if (!$button->isVisible()) { - Assert::fail('Button "%s" is not visible.', [$selector]); - } - } + $element = $this->getClickableElement($selector); $element->click(); @@ -448,6 +424,38 @@ public function savedArtifacts(): array return ['Saved Source Files' => $this->savedSources]; } + final protected function getClickableElement(string $selector): NodeElement + { + // try button + $element = $this->session()->page()->findButton($selector); + + if (!$element) { + // try link + $element = $this->session()->page()->findLink($selector); + } + + if (!$element) { + // try by css + $element = $this->session()->page()->find('css', $selector); + } + + if (!$element) { + Assert::fail('Clickable element "%s" not found.', [$selector]); + } + + if (!$element->isVisible()) { + Assert::fail('Clickable element "%s" is not visible.', [$selector]); + } + + if ($button = $this->session()->page()->findButton($selector)) { + if (!$button->isVisible()) { + Assert::fail('Button "%s" is not visible.', [$selector]); + } + } + + return $element; + } + /** * @internal */ diff --git a/src/Browser/PantherBrowser.php b/src/Browser/PantherBrowser.php index b98c80a..8342435 100644 --- a/src/Browser/PantherBrowser.php +++ b/src/Browser/PantherBrowser.php @@ -202,4 +202,20 @@ final public function savedArtifacts(): array ['Saved Console Logs' => $this->savedConsoleLogs, 'Saved Screenshots' => $this->savedScreenshots] ); } + + final public function doubleClick(string $selector): self + { + $element = $this->getClickableElement($selector); + $element->doubleClick(); + + return $this; + } + + final public function rightClick(string $selector): self + { + $element = $this->getClickableElement($selector); + $element->rightClick(); + + return $this; + } } diff --git a/src/Browser/Session/Driver/PantherDriver.php b/src/Browser/Session/Driver/PantherDriver.php index c0bab48..b311c29 100644 --- a/src/Browser/Session/Driver/PantherDriver.php +++ b/src/Browser/Session/Driver/PantherDriver.php @@ -185,6 +185,18 @@ public function click($xpath): void $this->client()->refreshCrawler(); } + public function doubleClick($xpath): void + { + $this->client()->getMouse()->doubleClick($this->toCoordinates($xpath)); + $this->client()->refreshCrawler(); + } + + public function rightClick($xpath): void + { + $this->client()->getMouse()->contextClick($this->toCoordinates($xpath)); + $this->client()->refreshCrawler(); + } + public function executeScript($script): void { if (\preg_match('/^function[\s(]/', $script)) { diff --git a/tests/Fixture/files/page1.html b/tests/Fixture/files/page1.html index a7d98e0..c64ab29 100644 --- a/tests/Fixture/files/page1.html +++ b/tests/Fixture/files/page1.html @@ -9,6 +9,8 @@

h1 title

exception link

+ +
  • list 1
  • list 2
  • diff --git a/tests/PantherBrowserTest.php b/tests/PantherBrowserTest.php index 58c57b2..7d9b24e 100644 --- a/tests/PantherBrowserTest.php +++ b/tests/PantherBrowserTest.php @@ -188,6 +188,42 @@ public function cannot_follow_invisible_link(): void ; } + /** + * @test + */ + public function double_click_on_element(): void + { + $this->browser() + ->visit('/page1') + ->click('#double-click-link a') + ->assertOn('/page1') + ; + + $this->browser() + ->visit('/page1') + ->doubleClick('#double-click-link a') + ->assertOn('/page2') + ; + } + + /** + * @test + */ + public function context_menu_on_element(): void + { + $this->browser() + ->visit('/page1') + ->click('#double-click-link a') + ->assertOn('/page1') + ; + + $this->browser() + ->visit('/page1') + ->rightClick('#context-menu-link a') + ->assertOn('/page2') + ; + } + protected function browser(): PantherBrowser { return $this->pantherBrowser();