Skip to content

Commit 4ad4a11

Browse files
authored
Inform developers about non-working "rightClick" on Selenium Server 3.x (#407)
Inform developers about non-working "rightClick" on Selenium Server 3.x
1 parent 906ea02 commit 4ad4a11

5 files changed

Lines changed: 152 additions & 15 deletions

File tree

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ jobs:
5252
- selenium_version: '3.141.59'
5353
php: '8.3'
5454
with_coverage: true
55+
- selenium_version: '4'
56+
php: '8.3'
57+
with_coverage: true
5558
fail-fast: false
5659

5760
steps:

src/Selenium2Driver.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,30 @@ private function executeJsOnElement(Element $element, string $script, bool $sync
349349
public function start()
350350
{
351351
try {
352-
$this->wdSession = $this->webDriver->session($this->browserName, $this->desiredCapabilities);
353-
354352
$status = $this->webDriver->status();
355-
$this->isW3C = version_compare($status['build']['version'], '3.0.0', '>=');
353+
$seleniumVersion = $status['build']['version'] ?? $status['nodes'][0]['version'] ?? 'unknown';
354+
$seleniumMajorVersion = (int) explode('.', $seleniumVersion)[0];
355+
} catch (\Throwable $ex) {
356+
throw new DriverException("Selenium Server version could not be detected: {$ex->getMessage()}", 0, $ex);
357+
}
358+
359+
if ($seleniumMajorVersion > 3) {
360+
throw new DriverException(<<<TEXT
361+
This driver requires Selenium version 3 or lower, but version {$seleniumVersion} was found.
362+
363+
Please use the "mink/webdriver-classic-driver" Mink driver or switch to Selenium Server 2.x/3.x.
364+
TEXT
365+
);
366+
}
367+
368+
try {
369+
$this->isW3C = $seleniumMajorVersion === 3;
370+
$this->wdSession = $this->webDriver->session($this->browserName, $this->desiredCapabilities);
356371

357372
$this->applyTimeouts();
358373
$this->initialWindowHandle = $this->getWebDriverSession()->window_handle();
359374
} catch (\Exception $e) {
360-
throw new DriverException('Could not open connection: '.$e->getMessage(), 0, $e);
375+
throw new DriverException('Could not open connection: ' . $e->getMessage(), 0, $e);
361376
}
362377

363378
$this->started = true;
@@ -936,6 +951,16 @@ public function doubleClick(string $xpath)
936951

937952
public function rightClick(string $xpath)
938953
{
954+
if ($this->isW3C) {
955+
// See: https://github.com/SeleniumHQ/selenium/commit/085ceed1f55fbaaa1d419b19c73264415c394905.
956+
throw new DriverException(<<<TEXT
957+
Right-clicking via JsonWireProtocol is not possible on Selenium Server 3.x.
958+
959+
Please use the "mink/webdriver-classic-driver" Mink driver or switch to Selenium Server 2.x.
960+
TEXT
961+
);
962+
}
963+
939964
$this->mouseOver($xpath);
940965
$this->getWebDriverSession()->click(array('button' => 2));
941966
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Behat\Mink\Tests\Driver\Custom;
4+
5+
use Behat\Mink\Exception\DriverException;
6+
use Behat\Mink\Tests\Driver\Selenium2Config;
7+
use Behat\Mink\Tests\Driver\TestCase;
8+
9+
final class SeleniumSupportTest extends TestCase
10+
{
11+
public function testDriverCannotBeUsedInUnsupportedSelenium(): void
12+
{
13+
if (Selenium2Config::getInstance()->isSeleniumVersionSupported()) {
14+
$this->markTestSkipped('This test applies to unsupported Selenium versions only.');
15+
}
16+
17+
$this->expectException(DriverException::class);
18+
$this->expectExceptionMessage('This driver requires Selenium version 3 or lower');
19+
20+
$this->createDriver()->start();
21+
}
22+
23+
public function testThatRightClickingCannotBeUsedInUnsupportedSelenium(): void
24+
{
25+
if (Selenium2Config::getInstance()->isRightClickingInSeleniumSupported()) {
26+
$this->markTestSkipped('This test applies to Selenium 3 only.');
27+
}
28+
29+
$this->expectException(DriverException::class);
30+
$this->expectExceptionMessage(<<<TEXT
31+
Right-clicking via JsonWireProtocol is not possible on Selenium Server 3.x.
32+
33+
Please use the "mink/webdriver-classic-driver" Mink driver or switch to Selenium Server 2.x.
34+
TEXT
35+
);
36+
37+
$driver = $this->createDriver();
38+
$driver->start();
39+
$driver->rightClick('//');
40+
}
41+
}

tests/Custom/WebDriverTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Behat\Mink\Tests\Driver\Custom;
44

55
use Behat\Mink\Driver\Selenium2Driver;
6+
use Behat\Mink\Exception\DriverException;
67
use Behat\Mink\Tests\Driver\TestCase;
8+
use WebDriver\WebDriver;
79

810
class WebDriverTest extends TestCase
911
{
@@ -18,4 +20,21 @@ public function testGetWebDriverSessionId()
1820
$driver = new Selenium2Driver();
1921
$this->assertNull($driver->getWebDriverSessionId(), 'Not started session don\'t have an ID');
2022
}
23+
24+
public function testUnsupportedStatusResponseHandling(): void
25+
{
26+
$mockWebDriver = $this->createMock(WebDriver::class);
27+
$mockWebDriver->expects($this->once())
28+
->method('__call')
29+
->with($this->equalTo('status'))
30+
->willThrowException(new \RuntimeException('some internal error'));
31+
32+
$driver = new Selenium2Driver();
33+
$driver->setWebDriver($mockWebDriver);
34+
35+
$this->expectException(DriverException::class);
36+
$this->expectExceptionMessage('Selenium Server version could not be detected: some internal error');
37+
38+
$driver->start();
39+
}
2140
}

tests/Selenium2Config.php

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77
use Behat\Mink\Tests\Driver\Basic\BasicAuthTest;
88
use Behat\Mink\Tests\Driver\Basic\HeaderTest;
99
use Behat\Mink\Tests\Driver\Basic\StatusCodeTest;
10+
use Behat\Mink\Tests\Driver\Css\HoverTest;
11+
use Behat\Mink\Tests\Driver\Custom\SeleniumSupportTest;
12+
use Behat\Mink\Tests\Driver\Form\Html5Test;
13+
use Behat\Mink\Tests\Driver\Js\EventsTest;
1014
use Behat\Mink\Tests\Driver\Js\JavascriptTest;
1115

1216
class Selenium2Config extends AbstractConfig
1317
{
18+
19+
/**
20+
* @var integer
21+
*/
22+
protected $seleniumMajorVersion;
23+
24+
public function __construct()
25+
{
26+
$this->seleniumMajorVersion = (int) explode('.', $_SERVER['SELENIUM_VERSION'] ?? '')[0];
27+
}
28+
1429
public static function getInstance(): self
1530
{
1631
return new self();
@@ -36,15 +51,17 @@ public function mapRemoteFilePath($file): string
3651

3752
public function skipMessage($testCase, $test): ?string
3853
{
39-
if (
40-
'Behat\Mink\Tests\Driver\Form\Html5Test' === $testCase
41-
&& 'testHtml5Types' === $test
42-
) {
43-
return 'WebDriver does not support setting value in color inputs. See https://code.google.com/p/selenium/issues/detail?id=7650';
54+
$testCallback = [$testCase, $test];
55+
56+
if ([Html5Test::class, 'testHtml5Types'] === $testCallback) {
57+
return <<<TEXT
58+
WebDriver does not support setting value in color inputs.
59+
60+
See https://code.google.com/p/selenium/issues/detail?id=7650.
61+
TEXT;
4462
}
4563

46-
if (
47-
'Behat\Mink\Tests\Driver\Js\WindowTest' === $testCase
64+
if ('Behat\Mink\Tests\Driver\Js\WindowTest' === $testCase
4865
&& (0 === strpos($test, 'testWindowMaximize'))
4966
&& 'true' === getenv('GITHUB_ACTIONS')
5067
) {
@@ -63,20 +80,52 @@ public function skipMessage($testCase, $test): ?string
6380
return 'Checking status code is not supported.';
6481
}
6582

66-
if (array(JavascriptTest::class, 'testDragDropOntoHiddenItself') === array($testCase, $test)) {
67-
$seleniumVersion = $_SERVER['SELENIUM_VERSION'] ?? null;
83+
if ([JavascriptTest::class, 'testDragDropOntoHiddenItself'] === $testCallback) {
6884
$browser = $_SERVER['WEB_FIXTURES_BROWSER'] ?? null;
6985

70-
if ($seleniumVersion && version_compare($seleniumVersion, '3.0.0', '<') && $browser === 'firefox') {
71-
return 'The Firefox browser compatible with Selenium Server 2.x doesn\'t fully implement drag-n-drop support.';
86+
if ($browser === 'firefox' && $this->getSeleniumMajorVersion() === 2) {
87+
return 'The Firefox browser compatible with Selenium 2.x does not fully implement drag-n-drop support.';
7288
}
7389
}
7490

91+
// Skip right-clicking tests, when an unsupported Selenium version detected.
92+
if (([HoverTest::class, 'testRightClickHover'] === $testCallback || [EventsTest::class, 'testRightClick'] === $testCallback)
93+
&& !$this->isRightClickingInSeleniumSupported()
94+
) {
95+
return <<<TEXT
96+
Selenium 3.x does not support right-clicking via JsonWireProtocol.
97+
98+
See https://github.com/SeleniumHQ/selenium/commit/085ceed1f55fbaaa1d419b19c73264415c394905.
99+
TEXT;
100+
}
101+
102+
// Skips all tests, except mentioned below, for an unsupported Selenium version.
103+
if ([SeleniumSupportTest::class, 'testDriverCannotBeUsedInUnsupportedSelenium'] !== $testCallback
104+
&& !$this->isSeleniumVersionSupported()
105+
) {
106+
return 'Does not apply to unsupported Selenium versions.';
107+
}
108+
75109
return parent::skipMessage($testCase, $test);
76110
}
77111

78112
protected function supportsCss(): bool
79113
{
80114
return true;
81115
}
116+
117+
public function isRightClickingInSeleniumSupported(): bool
118+
{
119+
return $this->getSeleniumMajorVersion() < 3;
120+
}
121+
122+
public function isSeleniumVersionSupported(): bool
123+
{
124+
return $this->getSeleniumMajorVersion() < 4;
125+
}
126+
127+
protected function getSeleniumMajorVersion(): int
128+
{
129+
return $this->seleniumMajorVersion;
130+
}
82131
}

0 commit comments

Comments
 (0)