From 94a594b2f0d3347df61098ca9f87c564e700b299 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 3 Jul 2024 14:52:00 +0100 Subject: [PATCH 1/4] Fix w3c false issues --- lib/WebDriver/WebDriver.php | 46 +++++------ .../Test/WebDriver/ChromeDriverNonW3CTest.php | 76 +++++++++++++++++++ test/Test/WebDriver/ChromeDriverTest.php | 1 + .../Selenium4ChromeWebDriverNonW3CTest.php | 46 +++++++++++ .../WebDriver/SeleniumWebDriverTestBase.php | 2 + 5 files changed, 146 insertions(+), 25 deletions(-) create mode 100644 test/Test/WebDriver/ChromeDriverNonW3CTest.php create mode 100644 test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php diff --git a/lib/WebDriver/WebDriver.php b/lib/WebDriver/WebDriver.php index 844616f..d6f7744 100644 --- a/lib/WebDriver/WebDriver.php +++ b/lib/WebDriver/WebDriver.php @@ -61,7 +61,10 @@ public function session($browserName = Browser::FIREFOX, $desiredCapabilities = }; $w3c_mode = true; - if (isset($desiredCapabilities['w3c']) && $desiredCapabilities['w3c'] === false) { + if ( + (isset($desiredCapabilities['w3c']) && $desiredCapabilities['w3c'] === false) || + (isset($desiredCapabilities['goog:chromeOptions']['w3c']) && $desiredCapabilities['goog:chromeOptions']['w3c'] === false) || + ) { $w3c_mode = false; } @@ -77,32 +80,25 @@ public function session($browserName = Browser::FIREFOX, $desiredCapabilities = $parameters['capabilities']['alwaysMatch'] = $requiredCapabilities; } - try { - $result = $this->curl( - 'POST', - '/session', - $parameters, - array(CURLOPT_FOLLOWLOCATION => true) - ); - } catch (\Exception $e) { - // fallback to legacy JSON Wire Protocol - $capabilities = $desiredCapabilities ?: array(); - $capabilities[Capability::BROWSER_NAME] = $browserName; - - $parameters = array('desiredCapabilities' => $capabilities); - - if (is_array($requiredCapabilities) && count($requiredCapabilities)) { - $parameters['requiredCapabilities'] = $requiredCapabilities; - } - - $result = $this->curl( - 'POST', - '/session', - $parameters, - array(CURLOPT_FOLLOWLOCATION => true) - ); + if (!$w3c_mode) { + // fallback to legacy JSON Wire Protocol + $capabilities = $desiredCapabilities ?: array(); + $capabilities[Capability::BROWSER_NAME] = $browserName; + + $parameters = array('desiredCapabilities' => $capabilities); + + if (is_array($requiredCapabilities) && count($requiredCapabilities)) { + $parameters['requiredCapabilities'] = $requiredCapabilities; + } } + $result = $this->curl( + 'POST', + '/session', + $parameters, + array(CURLOPT_FOLLOWLOCATION => true) + ); + $this->capabilities = isset($result['value']['capabilities']) ? $result['value']['capabilities'] : null; $session = new Session($result['sessionUrl'], $this->capabilities); diff --git a/test/Test/WebDriver/ChromeDriverNonW3CTest.php b/test/Test/WebDriver/ChromeDriverNonW3CTest.php new file mode 100644 index 0000000..7aa8636 --- /dev/null +++ b/test/Test/WebDriver/ChromeDriverNonW3CTest.php @@ -0,0 +1,76 @@ + + */ + +namespace Test\WebDriver; + +use Test\WebDriver\WebDriverTestBase; +use WebDriver\Browser; +use WebDriver\Session; + +/** + * ChromeDriver + * + * @package WebDriver + * + * @group Functional + */ +class ChromeDriverNonW3CTest extends WebDriverTestBase +{ + protected $testWebDriverRootUrl = 'http://localhost:9515'; + protected $testWebDriverName = 'chromedriver'; + + protected function setUp(): void + { + parent::setUp(); + try { + $this->status = $this->driver->status(); + $this->session = $this->driver->session(Browser::CHROME, [ + 'goog:chromeOptions' => [ + 'w3c' => false, + 'args' => [ + '--no-sandbox', + '--ignore-certificate-errors', + '--allow-insecure-localhost', + '--headless', + ], + ], + ]); + } + catch (\Exception $e) { + if ($this->isWebDriverDown($e)) { + $this->fail("{$this->testWebDriverName} server not running: {$e->getMessage()}"); + } + throw $e; + } + } + + /** + * Test driver status + */ + public function testStatus() + { + $this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready'); + $this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready'); + $this->assertNotEmpty($this->status['os'], 'OS info not detected'); + $this->assertFalse($this->driver->isW3c()); + } +} diff --git a/test/Test/WebDriver/ChromeDriverTest.php b/test/Test/WebDriver/ChromeDriverTest.php index bb2e074..0bca2bd 100644 --- a/test/Test/WebDriver/ChromeDriverTest.php +++ b/test/Test/WebDriver/ChromeDriverTest.php @@ -71,5 +71,6 @@ public function testStatus() $this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready'); $this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready'); $this->assertNotEmpty($this->status['os'], 'OS info not detected'); + $this->assertTrue($this->driver->isW3c()); } } diff --git a/test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php b/test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php new file mode 100644 index 0000000..980a866 --- /dev/null +++ b/test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php @@ -0,0 +1,46 @@ +status = $this->driver->status(); + $this->session = $this->driver->session(Browser::CHROME, [ + 'goog:chromeOptions' => [ + 'w3c' => false, + 'args' => [ + '--no-sandbox', + '--ignore-certificate-errors', + '--allow-insecure-localhost', + '--headless', + ], + ], + ]); + } + catch (\Exception $e) { + if ($this->isWebDriverDown($e)) { + $this->fail("{$this->testWebDriverName} server not running: {$e->getMessage()}"); + } + throw $e; + } + } +} diff --git a/test/Test/WebDriver/SeleniumWebDriverTestBase.php b/test/Test/WebDriver/SeleniumWebDriverTestBase.php index 240b1f0..3119db7 100644 --- a/test/Test/WebDriver/SeleniumWebDriverTestBase.php +++ b/test/Test/WebDriver/SeleniumWebDriverTestBase.php @@ -14,6 +14,7 @@ abstract class SeleniumWebDriverTestBase extends WebDriverTestBase protected $testWebDriverRootUrl = ''; protected $testWebDriverName = 'selenium'; protected $status = null; + protected $w3c = true; /** * Test driver status @@ -23,5 +24,6 @@ public function testStatus() $this->assertEquals(1, $this->status['ready'], 'Selenium is not ready'); $this->assertEquals('Selenium Grid ready.', $this->status['message'], 'Selenium is not ready'); $this->assertNotEmpty($this->status['nodes'][0]['osInfo'], 'OS info not detected'); + $this->assertSame($this->w3c, $this->driver->isW3c()); } } From edb6aeb9ec01451573e51c371d34a9387c1fe3e3 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 3 Jul 2024 14:54:06 +0100 Subject: [PATCH 2/4] Fix code --- lib/WebDriver/WebDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/WebDriver/WebDriver.php b/lib/WebDriver/WebDriver.php index d6f7744..7e03494 100644 --- a/lib/WebDriver/WebDriver.php +++ b/lib/WebDriver/WebDriver.php @@ -63,7 +63,7 @@ public function session($browserName = Browser::FIREFOX, $desiredCapabilities = $w3c_mode = true; if ( (isset($desiredCapabilities['w3c']) && $desiredCapabilities['w3c'] === false) || - (isset($desiredCapabilities['goog:chromeOptions']['w3c']) && $desiredCapabilities['goog:chromeOptions']['w3c'] === false) || + (isset($desiredCapabilities['goog:chromeOptions']['w3c']) && $desiredCapabilities['goog:chromeOptions']['w3c'] === false) ) { $w3c_mode = false; } From 2d243b84ce552a4507984494f632924500527816 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 3 Jul 2024 16:17:33 +0100 Subject: [PATCH 3/4] Less duplicate code --- .../Test/WebDriver/ChromeDriverNonW3CTest.php | 45 +------------------ test/Test/WebDriver/ChromeDriverTest.php | 5 ++- 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/test/Test/WebDriver/ChromeDriverNonW3CTest.php b/test/Test/WebDriver/ChromeDriverNonW3CTest.php index 7aa8636..7a0de7d 100644 --- a/test/Test/WebDriver/ChromeDriverNonW3CTest.php +++ b/test/Test/WebDriver/ChromeDriverNonW3CTest.php @@ -22,10 +22,6 @@ namespace Test\WebDriver; -use Test\WebDriver\WebDriverTestBase; -use WebDriver\Browser; -use WebDriver\Session; - /** * ChromeDriver * @@ -33,44 +29,7 @@ * * @group Functional */ -class ChromeDriverNonW3CTest extends WebDriverTestBase +class ChromeDriverNonW3CTest extends ChromeDriverTest { - protected $testWebDriverRootUrl = 'http://localhost:9515'; - protected $testWebDriverName = 'chromedriver'; - - protected function setUp(): void - { - parent::setUp(); - try { - $this->status = $this->driver->status(); - $this->session = $this->driver->session(Browser::CHROME, [ - 'goog:chromeOptions' => [ - 'w3c' => false, - 'args' => [ - '--no-sandbox', - '--ignore-certificate-errors', - '--allow-insecure-localhost', - '--headless', - ], - ], - ]); - } - catch (\Exception $e) { - if ($this->isWebDriverDown($e)) { - $this->fail("{$this->testWebDriverName} server not running: {$e->getMessage()}"); - } - throw $e; - } - } - - /** - * Test driver status - */ - public function testStatus() - { - $this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready'); - $this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready'); - $this->assertNotEmpty($this->status['os'], 'OS info not detected'); - $this->assertFalse($this->driver->isW3c()); - } + protected $w3c = false; } diff --git a/test/Test/WebDriver/ChromeDriverTest.php b/test/Test/WebDriver/ChromeDriverTest.php index 0bca2bd..f731eeb 100644 --- a/test/Test/WebDriver/ChromeDriverTest.php +++ b/test/Test/WebDriver/ChromeDriverTest.php @@ -37,6 +37,7 @@ class ChromeDriverTest extends WebDriverTestBase { protected $testWebDriverRootUrl = 'http://localhost:9515'; protected $testWebDriverName = 'chromedriver'; + protected $w3c = true; protected function setUp(): void { @@ -45,7 +46,7 @@ protected function setUp(): void $this->status = $this->driver->status(); $this->session = $this->driver->session(Browser::CHROME, [ 'goog:chromeOptions' => [ - 'w3c' => true, + 'w3c' => $this->w3c, 'args' => [ '--no-sandbox', '--ignore-certificate-errors', @@ -71,6 +72,6 @@ public function testStatus() $this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready'); $this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready'); $this->assertNotEmpty($this->status['os'], 'OS info not detected'); - $this->assertTrue($this->driver->isW3c()); + $this->assertSame($this->w3c, $this->driver->isW3c()); } } From 0efaf28d5b36921f0fb800734a84acf8f6b33f92 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 3 Jul 2024 22:37:17 +0100 Subject: [PATCH 4/4] Test on 4.8 selenium --- .ddev/docker-compose.selenium.yaml | 18 +++++++- test/Test/WebDriver/ChromeDriverTest.php | 2 +- .../Selenium4ChromeWebDriverNonW3CTest.php | 46 ------------------- .../Selenium4ChromeWebDriverTest.php | 2 +- ...Selenium4dot8ChromeWebDriverNonW3CTest.php | 17 +++++++ .../Selenium4dot8ChromeWebDriverTest.php | 17 +++++++ .../WebDriver/SeleniumWebDriverTestBase.php | 2 +- 7 files changed, 54 insertions(+), 50 deletions(-) delete mode 100644 test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php create mode 100644 test/Test/WebDriver/Selenium4dot8ChromeWebDriverNonW3CTest.php create mode 100644 test/Test/WebDriver/Selenium4dot8ChromeWebDriverTest.php diff --git a/.ddev/docker-compose.selenium.yaml b/.ddev/docker-compose.selenium.yaml index 0da85cd..0e1c129 100644 --- a/.ddev/docker-compose.selenium.yaml +++ b/.ddev/docker-compose.selenium.yaml @@ -19,7 +19,23 @@ services: - ddev-global-cache:/mnt/ddev-global-cache chrome: container_name: ddev-${DDEV_SITENAME}-chrome - image: selenium/standalone-chrome:latest + image: selenium/standalone-chromium:latest + labels: + com.ddev.site-name: ${DDEV_SITENAME} + com.ddev.approot: $DDEV_APPROOT + shm_size: 2gb + environment: + - VIRTUAL_HOST=$DDEV_HOSTNAME + links: + - web:web + external_links: + - ddev-router:${DDEV_SITENAME}.${DDEV_TLD} + volumes: + - ".:/mnt/ddev_config:ro" + - ddev-global-cache:/mnt/ddev-global-cache + oldchrome: + container_name: ddev-${DDEV_SITENAME}-oldchrome + image: selenium/standalone-chrome:4.8 labels: com.ddev.site-name: ${DDEV_SITENAME} com.ddev.approot: $DDEV_APPROOT diff --git a/test/Test/WebDriver/ChromeDriverTest.php b/test/Test/WebDriver/ChromeDriverTest.php index f731eeb..387fcac 100644 --- a/test/Test/WebDriver/ChromeDriverTest.php +++ b/test/Test/WebDriver/ChromeDriverTest.php @@ -72,6 +72,6 @@ public function testStatus() $this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready'); $this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready'); $this->assertNotEmpty($this->status['os'], 'OS info not detected'); - $this->assertSame($this->w3c, $this->driver->isW3c()); + $this->assertSame($this->w3c, $this->session->isW3c()); } } diff --git a/test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php b/test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php deleted file mode 100644 index 980a866..0000000 --- a/test/Test/WebDriver/Selenium4ChromeWebDriverNonW3CTest.php +++ /dev/null @@ -1,46 +0,0 @@ -status = $this->driver->status(); - $this->session = $this->driver->session(Browser::CHROME, [ - 'goog:chromeOptions' => [ - 'w3c' => false, - 'args' => [ - '--no-sandbox', - '--ignore-certificate-errors', - '--allow-insecure-localhost', - '--headless', - ], - ], - ]); - } - catch (\Exception $e) { - if ($this->isWebDriverDown($e)) { - $this->fail("{$this->testWebDriverName} server not running: {$e->getMessage()}"); - } - throw $e; - } - } -} diff --git a/test/Test/WebDriver/Selenium4ChromeWebDriverTest.php b/test/Test/WebDriver/Selenium4ChromeWebDriverTest.php index 9f20bcb..dcf8e0f 100644 --- a/test/Test/WebDriver/Selenium4ChromeWebDriverTest.php +++ b/test/Test/WebDriver/Selenium4ChromeWebDriverTest.php @@ -25,7 +25,7 @@ protected function setUp(): void $this->status = $this->driver->status(); $this->session = $this->driver->session(Browser::CHROME, [ 'goog:chromeOptions' => [ - 'w3c' => true, + 'w3c' => $this->w3c, 'args' => [ '--no-sandbox', '--ignore-certificate-errors', diff --git a/test/Test/WebDriver/Selenium4dot8ChromeWebDriverNonW3CTest.php b/test/Test/WebDriver/Selenium4dot8ChromeWebDriverNonW3CTest.php new file mode 100644 index 0000000..651f86a --- /dev/null +++ b/test/Test/WebDriver/Selenium4dot8ChromeWebDriverNonW3CTest.php @@ -0,0 +1,17 @@ +assertEquals(1, $this->status['ready'], 'Selenium is not ready'); $this->assertEquals('Selenium Grid ready.', $this->status['message'], 'Selenium is not ready'); $this->assertNotEmpty($this->status['nodes'][0]['osInfo'], 'OS info not detected'); - $this->assertSame($this->w3c, $this->driver->isW3c()); + $this->assertSame($this->w3c, $this->session->isW3c()); } }