Skip to content

Commit

Permalink
Merge pull request #10 from alexpott/w3c-session-fix
Browse files Browse the repository at this point in the history
Fix w3c false issues
  • Loading branch information
longwave authored Jul 4, 2024
2 parents cc9c601 + 84a2b40 commit 55fb15b
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 28 deletions.
18 changes: 17 additions & 1 deletion .ddev/docker-compose.selenium.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 21 additions & 25 deletions lib/WebDriver/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);

Expand Down
35 changes: 35 additions & 0 deletions test/Test/WebDriver/ChromeDriverNonW3CTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Copyright 2021-2022 Anthon Pang. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package WebDriver
*
* @author Anthon Pang <apang@softwaredevelopment.ca>
*/

namespace Test\WebDriver;

/**
* ChromeDriver
*
* @package WebDriver
*
* @group Functional
*/
class ChromeDriverNonW3CTest extends ChromeDriverTest
{
protected $w3c = false;
}
4 changes: 3 additions & 1 deletion test/Test/WebDriver/ChromeDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ChromeDriverTest extends WebDriverTestBase
{
protected $testWebDriverRootUrl = 'http://localhost:9515';
protected $testWebDriverName = 'chromedriver';
protected $w3c = true;
protected $status = null;

protected function setUp(): void
Expand All @@ -46,7 +47,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',
Expand All @@ -72,5 +73,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->session->isW3c());
}
}
2 changes: 1 addition & 1 deletion test/Test/WebDriver/Selenium4ChromeWebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 17 additions & 0 deletions test/Test/WebDriver/Selenium4dot8ChromeWebDriverNonW3CTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Test\WebDriver;

use WebDriver\Browser;

/**
* Selenium WebDriver
*
* @package WebDriver
*
* @group Functional
*/
class Selenium4dot8ChromeWebDriverNonW3CTest extends Selenium4dot8ChromeWebDriverTest
{
protected $w3c = FALSE;
}
17 changes: 17 additions & 0 deletions test/Test/WebDriver/Selenium4dot8ChromeWebDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Test\WebDriver;

use WebDriver\Browser;

/**
* Selenium WebDriver
*
* @package WebDriver
*
* @group Functional
*/
class Selenium4dot8ChromeWebDriverTest extends Selenium4ChromeWebDriverTest
{
protected $testWebDriverRootUrl = 'http://oldchrome:4444';
}
2 changes: 2 additions & 0 deletions test/Test/WebDriver/SeleniumWebDriverTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ abstract class SeleniumWebDriverTestBase extends WebDriverTestBase
protected $testWebDriverRootUrl = '';
protected $testWebDriverName = 'selenium';
protected $status = null;
protected $w3c = true;

/**
* Test driver status
Expand All @@ -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->session->isW3c());
}
}

0 comments on commit 55fb15b

Please sign in to comment.