Skip to content

Commit ac4c46c

Browse files
author
whhone
committed
DesiredCapabilities + WebDriverPlatform
Make desired capabilities object-oriented. To start a RemoteWebDriver instance, we can use pre-defineded DesiredCapabilities instead of constructing an array everytime. For example, to start Chrome. ``` $driver = RemoteWebDriver::create($url, DesiredCapabilities::chrome()); ```
1 parent 73e204a commit ac4c46c

File tree

6 files changed

+273
-3
lines changed

6 files changed

+273
-3
lines changed

example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// start Firefox with 5 second timeout
77
$host = 'http://localhost:4444/wd/hub'; // this is the default
8-
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'firefox');
8+
$capabilities = DesiredCapabilities::firefox();
99
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
1010

1111
// navigate to 'http://docs.seleniumhq.org/'

lib/WebDriverCapabilities.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
interface WebDriverCapabilities {
17+
18+
/**
19+
* @return string The name of the browser.
20+
*/
21+
public function getBrowserName();
22+
23+
/**
24+
* @return mixed The value of a capability.
25+
*/
26+
public function getCapability($name);
27+
28+
/**
29+
* @return string The name of the platform.
30+
*/
31+
public function getPlatform();
32+
33+
/**
34+
* @return string The version of the browser.
35+
*/
36+
public function getVersion();
37+
38+
/**
39+
* @return bool Whether the value is not null and not false.
40+
*/
41+
public function is($capability_name);
42+
43+
/**
44+
* @return bool Whether javascript is enabled.
45+
*/
46+
public function isJavascriptEnabled();
47+
}

lib/WebDriverPlatform.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* The platforms supported by WebDriver.
18+
*/
19+
class WebDriverPlatform {
20+
21+
const ANDROID = 'ANDROID';
22+
const ANY = 'ANY';
23+
const LINUX = 'LINUX';
24+
const MAC = 'MAC';
25+
const UNIX = 'UNIX';
26+
const VISTA = 'VISTA';
27+
const WINDOWS = 'WINDOWS';
28+
const XP = 'XP';
29+
}

lib/__init__.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require_once('WebDriverAction.php');
2323
require_once('WebDriverEventListener.php');
2424
require_once('remote/FileDetector.php');
25+
require_once('WebDriverCapabilities.php');
2526

2627
// abstract class
2728
require_once('interactions/internal/WebDriverKeysRelatedAction.php');
@@ -39,6 +40,7 @@
3940
require_once('WebDriverMouse.php');
4041
require_once('WebDriverKeyboard.php');
4142
require_once('WebDriverOptions.php');
43+
require_once('WebDriverPlatform.php');
4244
require_once('WebDriverPoint.php');
4345
require_once('WebDriverSelect.php');
4446
require_once('WebDriverTargetLocator.php');
@@ -65,6 +67,7 @@
6567
require_once('remote/RemoteWebElement.php');
6668
require_once('remote/WebDriverBrowserType.php');
6769
require_once('remote/WebDriverCapabilityType.php');
70+
require_once('remote/DesiredCapabilities.php');
6871
require_once('remote/HttpCommandExecutor.php');
6972
require_once('interactions/internal/WebDriverSendKeysAction.php');
7073
require_once('interactions/internal/WebDriverKeyDownAction.php');

lib/remote/DesiredCapabilities.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
class DesiredCapabilities implements WebDriverCapabilities {
17+
18+
private $capabilities;
19+
20+
public function __construct(array $capabilities = array()) {
21+
$this->capabilities = $capabilities;
22+
}
23+
24+
/**
25+
* @return string The name of the browser.
26+
*/
27+
public function getBrowserName() {
28+
return $this->get(WebDriverCapabilityType::BROWSER_NAME, '');
29+
}
30+
31+
public function setBrowserName($browser_name) {
32+
$this->set(WebDriverCapabilityType::BROWSER_NAME, $browser_name);
33+
return $this;
34+
}
35+
36+
/**
37+
* @return string The version of the browser.
38+
*/
39+
public function getVersion() {
40+
return $this->get(WebDriverCapabilityType::VERSION, '');
41+
}
42+
43+
public function setVersion($version) {
44+
$this->set(WebDriverCapabilityType::VERSION, $version);
45+
return $this;
46+
}
47+
48+
/**
49+
* @return mixed The value of a capability.
50+
*/
51+
public function getCapability($name) {
52+
$this->get($name);
53+
}
54+
55+
public function setCapability($name, $value) {
56+
$this->set($name, $value);
57+
return $this;
58+
}
59+
60+
/**
61+
* @return string The name of the platform.
62+
*/
63+
public function getPlatform() {
64+
return $this->get(WebDriverCapabilityType::PLATFORM, '');
65+
}
66+
67+
public function setPlatform($platform) {
68+
$this->set(WebDriverCapabilityType::PLATFORM, $platform);
69+
return $this;
70+
}
71+
72+
73+
/**
74+
* @return bool Whether the value is not null and not false.
75+
*/
76+
public function is($capability_name) {
77+
return (bool)$this->get($capability_name);
78+
}
79+
80+
/**
81+
* @return bool Whether javascript is enabled.
82+
*/
83+
public function isJavascriptEnabled() {
84+
return $this->get(WebDriverCapabilityType::JAVASCRIPT_ENABLED, false);
85+
}
86+
87+
public function setJavascriptEnabled($enabled) {
88+
$this->set(WebDriverCapabilityType::JAVASCRIPT_ENABLED, $enabled);
89+
return $this;
90+
}
91+
92+
public function toArray() {
93+
return $this->capabilities;
94+
}
95+
96+
private function set($key, $value) {
97+
$this->capabilities[$key] = $value;
98+
return $this;
99+
}
100+
101+
private function get($key, $default = null) {
102+
return isset($this->capabilities[$key])
103+
? $this->capabilities[$key]
104+
: $default;
105+
}
106+
107+
public static function android() {
108+
return new DesiredCapabilities(array(
109+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::ANDROID,
110+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANDROID,
111+
));
112+
}
113+
114+
public static function chrome() {
115+
return new DesiredCapabilities(array(
116+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::CHROME,
117+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
118+
));
119+
}
120+
121+
public static function firefox() {
122+
return new DesiredCapabilities(array(
123+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::FIREFOX,
124+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
125+
));
126+
}
127+
128+
public static function htmlUnit() {
129+
return new DesiredCapabilities(array(
130+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
131+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
132+
));
133+
}
134+
135+
public static function htmlUnitWithJS() {
136+
$caps = new DesiredCapabilities(array(
137+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::HTMLUNIT,
138+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
139+
));
140+
return $caps->setJavascriptEnabled(true);
141+
}
142+
143+
public static function internetExplorer() {
144+
return new DesiredCapabilities(array(
145+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IE,
146+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::WINDOWS,
147+
));
148+
}
149+
150+
public static function iphone() {
151+
return new DesiredCapabilities(array(
152+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPHONE,
153+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
154+
));
155+
}
156+
157+
public static function ipad() {
158+
return new DesiredCapabilities(array(
159+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::IPAD,
160+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::MAC,
161+
));
162+
}
163+
164+
public static function opera() {
165+
return new DesiredCapabilities(array(
166+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::OPERA,
167+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
168+
));
169+
}
170+
171+
public static function safari() {
172+
return new DesiredCapabilities(array(
173+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::SAFARI,
174+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
175+
));
176+
}
177+
178+
public static function phantomjs() {
179+
return new DesiredCapabilities(array(
180+
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
181+
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
182+
));
183+
}
184+
}

lib/remote/RemoteWebDriver.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ protected function __construct() {}
2626
* Construct the RemoteWebDriver by a desired capabilities.
2727
*
2828
* @param string $url The url of the remote server
29-
* @param array $desired_capabilities The webdriver desired capabilities
29+
* @param DesiredCapabilities $desired_capabilities The desired capabilities
3030
* @param int $timeout_in_ms
3131
* @return RemoteWebDriver
3232
*/
3333
public static function create(
3434
$url = 'http://localhost:4444/wd/hub',
35-
$desired_capabilities = array(),
35+
$desired_capabilities = null,
3636
$timeout_in_ms = 300000
3737
) {
3838
$url = preg_replace('#/+$#', '', $url);
39+
40+
// Passing DesiredCapabilities as $desired_capabilities is encourged but
41+
// array is also accepted for legacy reason.
42+
if ($desired_capabilities instanceof DesiredCapabilities) {
43+
$desired_capabilities = $desired_capabilities->toArray();
44+
}
45+
3946
$command = array(
4047
'url' => $url,
4148
'name' => 'newSession',

0 commit comments

Comments
 (0)