Skip to content

Commit 94a594b

Browse files
committed
Fix w3c false issues
1 parent ca66013 commit 94a594b

File tree

5 files changed

+146
-25
lines changed

5 files changed

+146
-25
lines changed

lib/WebDriver/WebDriver.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public function session($browserName = Browser::FIREFOX, $desiredCapabilities =
6161
};
6262

6363
$w3c_mode = true;
64-
if (isset($desiredCapabilities['w3c']) && $desiredCapabilities['w3c'] === false) {
64+
if (
65+
(isset($desiredCapabilities['w3c']) && $desiredCapabilities['w3c'] === false) ||
66+
(isset($desiredCapabilities['goog:chromeOptions']['w3c']) && $desiredCapabilities['goog:chromeOptions']['w3c'] === false) ||
67+
) {
6568
$w3c_mode = false;
6669
}
6770

@@ -77,32 +80,25 @@ public function session($browserName = Browser::FIREFOX, $desiredCapabilities =
7780
$parameters['capabilities']['alwaysMatch'] = $requiredCapabilities;
7881
}
7982

80-
try {
81-
$result = $this->curl(
82-
'POST',
83-
'/session',
84-
$parameters,
85-
array(CURLOPT_FOLLOWLOCATION => true)
86-
);
87-
} catch (\Exception $e) {
88-
// fallback to legacy JSON Wire Protocol
89-
$capabilities = $desiredCapabilities ?: array();
90-
$capabilities[Capability::BROWSER_NAME] = $browserName;
91-
92-
$parameters = array('desiredCapabilities' => $capabilities);
93-
94-
if (is_array($requiredCapabilities) && count($requiredCapabilities)) {
95-
$parameters['requiredCapabilities'] = $requiredCapabilities;
96-
}
97-
98-
$result = $this->curl(
99-
'POST',
100-
'/session',
101-
$parameters,
102-
array(CURLOPT_FOLLOWLOCATION => true)
103-
);
83+
if (!$w3c_mode) {
84+
// fallback to legacy JSON Wire Protocol
85+
$capabilities = $desiredCapabilities ?: array();
86+
$capabilities[Capability::BROWSER_NAME] = $browserName;
87+
88+
$parameters = array('desiredCapabilities' => $capabilities);
89+
90+
if (is_array($requiredCapabilities) && count($requiredCapabilities)) {
91+
$parameters['requiredCapabilities'] = $requiredCapabilities;
92+
}
10493
}
10594

95+
$result = $this->curl(
96+
'POST',
97+
'/session',
98+
$parameters,
99+
array(CURLOPT_FOLLOWLOCATION => true)
100+
);
101+
106102
$this->capabilities = isset($result['value']['capabilities']) ? $result['value']['capabilities'] : null;
107103
$session = new Session($result['sessionUrl'], $this->capabilities);
108104

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2021-2022 Anthon Pang. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package WebDriver
19+
*
20+
* @author Anthon Pang <apang@softwaredevelopment.ca>
21+
*/
22+
23+
namespace Test\WebDriver;
24+
25+
use Test\WebDriver\WebDriverTestBase;
26+
use WebDriver\Browser;
27+
use WebDriver\Session;
28+
29+
/**
30+
* ChromeDriver
31+
*
32+
* @package WebDriver
33+
*
34+
* @group Functional
35+
*/
36+
class ChromeDriverNonW3CTest extends WebDriverTestBase
37+
{
38+
protected $testWebDriverRootUrl = 'http://localhost:9515';
39+
protected $testWebDriverName = 'chromedriver';
40+
41+
protected function setUp(): void
42+
{
43+
parent::setUp();
44+
try {
45+
$this->status = $this->driver->status();
46+
$this->session = $this->driver->session(Browser::CHROME, [
47+
'goog:chromeOptions' => [
48+
'w3c' => false,
49+
'args' => [
50+
'--no-sandbox',
51+
'--ignore-certificate-errors',
52+
'--allow-insecure-localhost',
53+
'--headless',
54+
],
55+
],
56+
]);
57+
}
58+
catch (\Exception $e) {
59+
if ($this->isWebDriverDown($e)) {
60+
$this->fail("{$this->testWebDriverName} server not running: {$e->getMessage()}");
61+
}
62+
throw $e;
63+
}
64+
}
65+
66+
/**
67+
* Test driver status
68+
*/
69+
public function testStatus()
70+
{
71+
$this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready');
72+
$this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready');
73+
$this->assertNotEmpty($this->status['os'], 'OS info not detected');
74+
$this->assertFalse($this->driver->isW3c());
75+
}
76+
}

test/Test/WebDriver/ChromeDriverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ public function testStatus()
7171
$this->assertEquals(1, $this->status['ready'], 'Chromedriver is not ready');
7272
$this->assertEquals('ChromeDriver ready for new sessions.', $this->status['message'], 'Chromedriver is not ready');
7373
$this->assertNotEmpty($this->status['os'], 'OS info not detected');
74+
$this->assertTrue($this->driver->isW3c());
7475
}
7576
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Test\WebDriver;
4+
5+
use WebDriver\Browser;
6+
7+
/**
8+
* Selenium WebDriver
9+
*
10+
* @package WebDriver
11+
*
12+
* @group Functional
13+
*/
14+
class Selenium4ChromeWebDriverNonW3CTest extends SeleniumWebDriverTestBase
15+
{
16+
protected $testWebDriverRootUrl = 'http://chrome:4444';
17+
protected $w3c = FALSE;
18+
19+
/**
20+
* Run before each test.
21+
*/
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
try {
26+
$this->status = $this->driver->status();
27+
$this->session = $this->driver->session(Browser::CHROME, [
28+
'goog:chromeOptions' => [
29+
'w3c' => false,
30+
'args' => [
31+
'--no-sandbox',
32+
'--ignore-certificate-errors',
33+
'--allow-insecure-localhost',
34+
'--headless',
35+
],
36+
],
37+
]);
38+
}
39+
catch (\Exception $e) {
40+
if ($this->isWebDriverDown($e)) {
41+
$this->fail("{$this->testWebDriverName} server not running: {$e->getMessage()}");
42+
}
43+
throw $e;
44+
}
45+
}
46+
}

test/Test/WebDriver/SeleniumWebDriverTestBase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ abstract class SeleniumWebDriverTestBase extends WebDriverTestBase
1414
protected $testWebDriverRootUrl = '';
1515
protected $testWebDriverName = 'selenium';
1616
protected $status = null;
17+
protected $w3c = true;
1718

1819
/**
1920
* Test driver status
@@ -23,5 +24,6 @@ public function testStatus()
2324
$this->assertEquals(1, $this->status['ready'], 'Selenium is not ready');
2425
$this->assertEquals('Selenium Grid ready.', $this->status['message'], 'Selenium is not ready');
2526
$this->assertNotEmpty($this->status['nodes'][0]['osInfo'], 'OS info not detected');
27+
$this->assertSame($this->w3c, $this->driver->isW3c());
2628
}
2729
}

0 commit comments

Comments
 (0)