/
DesiredCapabilities.php
344 lines (302 loc) · 9 KB
/
DesiredCapabilities.php
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
// Copyright 2004-present Facebook. 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.
namespace Facebook\WebDriver\Remote;
use Exception;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Firefox\FirefoxPreferences;
use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\WebDriverCapabilities;
use Facebook\WebDriver\WebDriverPlatform;
class DesiredCapabilities implements WebDriverCapabilities
{
/**
* @var array
*/
private $capabilities;
public function __construct(array $capabilities = [])
{
$this->capabilities = $capabilities;
}
/**
* @return string The name of the browser.
*/
public function getBrowserName()
{
return $this->get(WebDriverCapabilityType::BROWSER_NAME, '');
}
/**
* @param string $browser_name
* @return DesiredCapabilities
*/
public function setBrowserName($browser_name)
{
$this->set(WebDriverCapabilityType::BROWSER_NAME, $browser_name);
return $this;
}
/**
* @return string The version of the browser.
*/
public function getVersion()
{
return $this->get(WebDriverCapabilityType::VERSION, '');
}
/**
* @param string $version
* @return DesiredCapabilities
*/
public function setVersion($version)
{
$this->set(WebDriverCapabilityType::VERSION, $version);
return $this;
}
/**
* @param string $name
* @return mixed The value of a capability.
*/
public function getCapability($name)
{
return $this->get($name);
}
/**
* @param string $name
* @param mixed $value
* @return DesiredCapabilities
*/
public function setCapability($name, $value)
{
$this->set($name, $value);
return $this;
}
/**
* @return string The name of the platform.
*/
public function getPlatform()
{
return $this->get(WebDriverCapabilityType::PLATFORM, '');
}
/**
* @param string $platform
* @return DesiredCapabilities
*/
public function setPlatform($platform)
{
$this->set(WebDriverCapabilityType::PLATFORM, $platform);
return $this;
}
/**
* @param string $capability_name
* @return bool Whether the value is not null and not false.
*/
public function is($capability_name)
{
return (bool) $this->get($capability_name);
}
/**
* @todo Remove in next major release (BC)
* @deprecated All browsers are always JS enabled except HtmlUnit and it's not meaningful to disable JS execution.
* @return bool Whether javascript is enabled.
*/
public function isJavascriptEnabled()
{
return $this->get(WebDriverCapabilityType::JAVASCRIPT_ENABLED, false);
}
/**
* This is a htmlUnit-only option.
*
* @param bool $enabled
* @throws Exception
* @return DesiredCapabilities
* @see https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#read-write-capabilities
*/
public function setJavascriptEnabled($enabled)
{
$browser = $this->getBrowserName();
if ($browser && $browser !== WebDriverBrowserType::HTMLUNIT) {
throw new Exception(
'isJavascriptEnabled() is a htmlunit-only option. ' .
'See https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#read-write-capabilities.'
);
}
$this->set(WebDriverCapabilityType::JAVASCRIPT_ENABLED, $enabled);
return $this;
}
/**
* @return array
*/
public function toArray()
{
if (isset($this->capabilities[ChromeOptions::CAPABILITY]) &&
$this->capabilities[ChromeOptions::CAPABILITY] instanceof ChromeOptions
) {
$this->capabilities[ChromeOptions::CAPABILITY] =
$this->capabilities[ChromeOptions::CAPABILITY]->toArray();
}
if (isset($this->capabilities[FirefoxDriver::PROFILE]) &&
$this->capabilities[FirefoxDriver::PROFILE] instanceof FirefoxProfile
) {
$this->capabilities[FirefoxDriver::PROFILE] =
$this->capabilities[FirefoxDriver::PROFILE]->encode();
}
return $this->capabilities;
}
/**
* @return static
*/
public static function android()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::ANDROID,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANDROID,
]);
}
/**
* @return static
*/
public static function chrome()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::CHROME,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
}
/**
* @return static
*/
public static function firefox()
{
$caps = new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::FIREFOX,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
// disable the "Reader View" help tooltip, which can hide elements in the window.document
$profile = new FirefoxProfile();
$profile->setPreference(FirefoxPreferences::READER_PARSE_ON_LOAD_ENABLED, false);
$caps->setCapability(FirefoxDriver::PROFILE, $profile);
return $caps;
}
/**
* @return static
*/
public static function htmlUnit()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
}
/**
* @return static
*/
public static function htmlUnitWithJS()
{
$caps = new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
return $caps->setJavascriptEnabled(true);
}
/**
* @return static
*/
public static function internetExplorer()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IE,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
]);
}
/**
* @return static
*/
public static function microsoftEdge()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::MICROSOFT_EDGE,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
]);
}
/**
* @return static
*/
public static function iphone()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPHONE,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
]);
}
/**
* @return static
*/
public static function ipad()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPAD,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
]);
}
/**
* @return static
*/
public static function opera()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::OPERA,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
}
/**
* @return static
*/
public static function safari()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::SAFARI,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
}
/**
* @return static
*/
public static function phantomjs()
{
return new static([
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
}
/**
* @param string $key
* @param mixed $value
* @return DesiredCapabilities
*/
private function set($key, $value)
{
$this->capabilities[$key] = $value;
return $this;
}
/**
* @param string $key
* @param mixed $default
* @return mixed
*/
private function get($key, $default = null)
{
return isset($this->capabilities[$key])
? $this->capabilities[$key]
: $default;
}
}