-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathWebDriverFilterTest.php
More file actions
278 lines (229 loc) · 9.9 KB
/
WebDriverFilterTest.php
File metadata and controls
278 lines (229 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Copyright 2026 Daif Abderrahman. 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 Daif Abderrahman <daif.abderrahman@gmail.com>
*/
namespace Test\WebDriver;
use PHPUnit\Framework\TestCase;
use WebDriver\Browser;
use WebDriver\Capability;
use WebDriver\Service\CurlService;
use WebDriver\ServiceFactory;
use WebDriver\WebDriver;
/**
* Test WebDriver capability filtering in session creation
*
* @package WebDriver
*
* @group Unit
*/
class WebDriverFilterTest extends TestCase
{
/**
* @var array|null
*/
private $capturedParameters;
/**
* Create a WebDriver instance with mocked CurlService that captures request parameters
*
* @return WebDriver
*/
private function createDriverWithMock()
{
$mockCurlService = $this->createMock(CurlService::class);
$mockCurlService->expects($this->any())
->method('execute')
->will($this->returnCallback(function ($requestMethod, $url, $parameters) {
$this->capturedParameters = $parameters;
$info = [
'url' => $url,
'request_method' => $requestMethod,
'http_code' => 200,
];
$result = json_encode([
'value' => [
'sessionId' => 'mock-session-id',
'capabilities' => [
'browserName' => 'chrome',
],
],
]);
return [$result, $info];
}));
ServiceFactory::getInstance()->setService('service.curl', $mockCurlService);
return new WebDriver('http://localhost:4444/wd/hub');
}
/**
* Non-W3C capabilities should be stripped from desiredCapabilities
*/
public function testSessionFiltersNonW3cCapabilities()
{
$driver = $this->createDriverWithMock();
$driver->session(Browser::CHROME, [
Capability::BROWSER_NAME => 'chrome',
'unknownCapability' => 'value',
'anotherInvalidCap' => true,
]);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
$filtered = $firstMatch[0];
$this->assertArrayHasKey(Capability::BROWSER_NAME, $filtered);
$this->assertArrayNotHasKey('unknownCapability', $filtered);
$this->assertArrayNotHasKey('anotherInvalidCap', $filtered);
}
/**
* All W3C standard capabilities should pass through the filter
*/
public function testSessionPreservesW3cCapabilities()
{
$driver = $this->createDriverWithMock();
$capabilities = [
Capability::BROWSER_NAME => 'firefox',
Capability::BROWSER_VERSION => '120.0',
Capability::PLATFORM_NAME => 'linux',
Capability::ACCEPT_INSECURE_CERTS => true,
Capability::PAGE_LOAD_STRATEGY => 'normal',
Capability::SET_WINDOW_RECT => true,
Capability::STRICT_FILE_INTERACTABILITY => false,
Capability::TIMEOUTS => ['implicit' => 5000],
Capability::UNHANDLED_PROMPT_BEHAVIOR => 'dismiss',
];
$driver->session(Browser::FIREFOX, $capabilities);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
$filtered = $firstMatch[0];
foreach ($capabilities as $key => $value) {
$this->assertArrayHasKey($key, $filtered, "W3C capability '$key' should be preserved");
$this->assertSame($value, $filtered[$key], "W3C capability '$key' value should be unchanged");
}
}
/**
* Vendor extension capabilities (containing ':') should pass through the filter
*/
public function testSessionPreservesExtensionCapabilities()
{
$driver = $this->createDriverWithMock();
$chromeOptions = ['args' => ['--headless', '--no-sandbox']];
$firefoxOptions = ['prefs' => ['dom.webnotifications.enabled' => false]];
$driver->session(Browser::CHROME, [
Capability::BROWSER_NAME => 'chrome',
'goog:chromeOptions' => $chromeOptions,
'moz:firefoxOptions' => $firefoxOptions,
'ms:edgeOptions' => ['args' => ['--start-maximized']],
]);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
$filtered = $firstMatch[0];
$this->assertArrayHasKey('goog:chromeOptions', $filtered);
$this->assertSame($chromeOptions, $filtered['goog:chromeOptions']);
$this->assertArrayHasKey('moz:firefoxOptions', $filtered);
$this->assertSame($firefoxOptions, $filtered['moz:firefoxOptions']);
$this->assertArrayHasKey('ms:edgeOptions', $filtered);
}
/**
* Capabilities with falsy values (false, empty string, empty array) must NOT be stripped.
*
* This is critical: a user may explicitly set acceptInsecureCerts => false to disable
* a capability that is enabled by default. The filter must preserve user intent.
*/
public function testSessionPreservesFalsyCapabilityValues()
{
$driver = $this->createDriverWithMock();
$driver->session(Browser::CHROME, [
Capability::ACCEPT_INSECURE_CERTS => false,
Capability::SET_WINDOW_RECT => false,
Capability::STRICT_FILE_INTERACTABILITY => false,
]);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
$filtered = $firstMatch[0];
$this->assertArrayHasKey(Capability::ACCEPT_INSECURE_CERTS, $filtered, 'acceptInsecureCerts => false must not be stripped');
$this->assertFalse($filtered[Capability::ACCEPT_INSECURE_CERTS]);
$this->assertArrayHasKey(Capability::SET_WINDOW_RECT, $filtered, 'setWindowRect => false must not be stripped');
$this->assertFalse($filtered[Capability::SET_WINDOW_RECT]);
$this->assertArrayHasKey(Capability::STRICT_FILE_INTERACTABILITY, $filtered, 'strictFileInteractability => false must not be stripped');
$this->assertFalse($filtered[Capability::STRICT_FILE_INTERACTABILITY]);
}
/**
* Null desiredCapabilities should not cause errors
*/
public function testSessionWithNullCapabilities()
{
$driver = $this->createDriverWithMock();
$session = $driver->session(Browser::CHROME, null);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
// Without capabilities, firstMatch should only contain the default chrome entry
$this->assertCount(1, $firstMatch);
$this->assertEquals([Capability::BROWSER_NAME => Browser::CHROME], $firstMatch[0]);
}
/**
* requiredCapabilities (alwaysMatch) should also be filtered
*/
public function testSessionFiltersRequiredCapabilities()
{
$driver = $this->createDriverWithMock();
$driver->session(Browser::CHROME, null, [
Capability::PLATFORM_NAME => 'linux',
'unknownCap' => 'value',
'goog:chromeOptions' => ['args' => ['--headless']],
]);
$alwaysMatch = $this->capturedParameters['capabilities']['alwaysMatch'];
$this->assertArrayHasKey(Capability::PLATFORM_NAME, $alwaysMatch);
$this->assertArrayNotHasKey('unknownCap', $alwaysMatch);
$this->assertArrayHasKey('goog:chromeOptions', $alwaysMatch);
}
/**
* Legacy JSON Wire Protocol capability names should be remapped to W3C names
*/
public function testSessionRemapsLegacyCapabilities()
{
$driver = $this->createDriverWithMock();
$driver->session(Browser::CHROME, [
Capability::PLATFORM => 'LINUX',
Capability::VERSION => '120.0',
Capability::ACCEPT_SSL_CERTS => true,
]);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
$filtered = $firstMatch[0];
// Legacy keys should be remapped to W3C equivalents
$this->assertArrayHasKey(Capability::PLATFORM_NAME, $filtered);
$this->assertEquals('LINUX', $filtered[Capability::PLATFORM_NAME]);
$this->assertArrayHasKey(Capability::BROWSER_VERSION, $filtered);
$this->assertEquals('120.0', $filtered[Capability::BROWSER_VERSION]);
$this->assertArrayHasKey(Capability::ACCEPT_INSECURE_CERTS, $filtered);
$this->assertTrue($filtered[Capability::ACCEPT_INSECURE_CERTS]);
// Original legacy keys should not remain
$this->assertArrayNotHasKey(Capability::PLATFORM, $filtered);
$this->assertArrayNotHasKey(Capability::VERSION, $filtered);
$this->assertArrayNotHasKey(Capability::ACCEPT_SSL_CERTS, $filtered);
}
/**
* Key-value mapping must be preserved (not reindexed numerically)
*/
public function testSessionPreservesKeyValueMapping()
{
$driver = $this->createDriverWithMock();
$driver->session(Browser::CHROME, [
Capability::BROWSER_NAME => 'chrome',
Capability::PLATFORM_NAME => 'linux',
Capability::ACCEPT_INSECURE_CERTS => true,
]);
$firstMatch = $this->capturedParameters['capabilities']['firstMatch'];
$filtered = $firstMatch[0];
// Keys must be string capability names, not numeric indices
$this->assertIsString(array_key_first($filtered));
$this->assertSame('chrome', $filtered[Capability::BROWSER_NAME]);
$this->assertSame('linux', $filtered[Capability::PLATFORM_NAME]);
}
}