Skip to content

Commit

Permalink
[feature] add doubleClick and rightClick to PantherBrowser (#104)
Browse files Browse the repository at this point in the history
* 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 <kevinbond@gmail.com>

* Update src/Browser/PantherBrowser.php

Co-authored-by: Kevin Bond <kevinbond@gmail.com>

* Update src/Browser.php

Co-authored-by: Kevin Bond <kevinbond@gmail.com>

Co-authored-by: Christopher Georg <christopher.georg@sr-travel.de>
Co-authored-by: Kevin Bond <kevinbond@gmail.com>
  • Loading branch information
3 people committed Aug 26, 2022
1 parent fc51e88 commit 1466827
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 26 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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()

Expand Down
60 changes: 34 additions & 26 deletions src/Browser.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Browser/PantherBrowser.php
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/Browser/Session/Driver/PantherDriver.php
Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixture/files/page1.html
Expand Up @@ -9,6 +9,8 @@
<h1>h1 title</h1>
<p id="link"><a href="/page2">a link</a> not a link</p>
<p><a href="/exception">exception link</a></p>
<p id="double-click-link"><a onclick="return false" ondblclick="location=this.href" href="/page2">Double Click</a></p>
<p id="context-menu-link"><a onclick="return false" oncontextmenu="location=this.href" href="/page2">Right Click</a></p>
<ul>
<li>list 1</li>
<li>list 2</li>
Expand Down
36 changes: 36 additions & 0 deletions tests/PantherBrowserTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit 1466827

Please sign in to comment.