Skip to content
19 changes: 18 additions & 1 deletion src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Selenium2Driver extends CoreDriver
*/
private $isW3C = false;

/**
* @var bool
*/
private $isRightClickSupported = false;

/**
* The timeout configuration
* @var array{script?: int, implicit?: int, page?: int}
Expand Down Expand Up @@ -352,7 +357,9 @@ public function start()
$this->wdSession = $this->webDriver->session($this->browserName, $this->desiredCapabilities);

$status = $this->webDriver->status();
$this->isW3C = version_compare($status['build']['version'], '3.0.0', '>=');
$majorSeleniumServerVersion = (int)explode('.', $status['build']['version'])[0];
$this->isW3C = $majorSeleniumServerVersion >= 3;
$this->isRightClickSupported = $majorSeleniumServerVersion !== 3;

$this->applyTimeouts();
$this->initialWindowHandle = $this->getWebDriverSession()->window_handle();
Expand Down Expand Up @@ -936,6 +943,16 @@ public function doubleClick(string $xpath)

public function rightClick(string $xpath)
{
if (!$this->isRightClickSupported) {
// See: https://github.com/SeleniumHQ/selenium/commit/085ceed1f55fbaaa1d419b19c73264415c394905.
throw new DriverException(<<<TEXT
Right-clicking via JsonWireProtocol is not possible on Selenium Server 3.

Please use the "mink/webdriver-classic-driver" Mink driver or switch to Selenium Server 2 or 4+.
TEXT
);
}

$this->mouseOver($xpath);
$this->getWebDriverSession()->click(array('button' => 2));
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Selenium2Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Behat\Mink\Tests\Driver\Basic\BasicAuthTest;
use Behat\Mink\Tests\Driver\Basic\HeaderTest;
use Behat\Mink\Tests\Driver\Basic\StatusCodeTest;
use Behat\Mink\Tests\Driver\Css\HoverTest;
use Behat\Mink\Tests\Driver\Js\EventsTest;
use Behat\Mink\Tests\Driver\Js\JavascriptTest;

class Selenium2Config extends AbstractConfig
Expand Down Expand Up @@ -72,6 +74,16 @@ public function skipMessage($testCase, $test): ?string
}
}

if (array(HoverTest::class, 'testRightClickHover') === array($testCase, $test)
|| array(EventsTest::class, 'testRightClick') === array($testCase, $test)
) {
$majorSeleniumServerVersion = (int)explode('.', $_SERVER['SELENIUM_VERSION'] ?? '0.0.0')[0];

if ($majorSeleniumServerVersion === 3) {
return 'The Selenium Server 3.x doesn\'t support right-clicking via JsonWireProtocol. See https://github.com/SeleniumHQ/selenium/commit/085ceed1f55fbaaa1d419b19c73264415c394905.';
}
}

return parent::skipMessage($testCase, $test);
}

Expand Down
Loading