Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit a48ed4c

Browse files
author
whhone
committed
Rename WebDriver to RemoteWebDriver extends interface WebDriver
Rename WebDriverElement to RemoteWebElement extends interface WebDriverElement
1 parent ec58891 commit a48ed4c

File tree

7 files changed

+579
-265
lines changed

7 files changed

+579
-265
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ Any complaint, question, idea? You can post it on the user group https://www.fac
3535
* Then when you create a session, be sure to pass the url to where your server is running.
3636

3737
// This would be the url of the host running the server-standalone.jar
38-
$wd_host = 'http://localhost:4444/wd/hub'; // this is the default
38+
$host = 'http://localhost:4444/wd/hub'; // this is the default
3939
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'firefox');
40-
$web_driver = new WebDriver($wd_host, $capabilities);
40+
$driver = new RemoteWebDriver($host, $capabilities);
4141

4242
* The $capabilities array lets you specify (among other things) which browser to use. See https://code.google.com/p/selenium/wiki/DesiredCapabilities for more details.
4343

lib/WebDriver.php

Lines changed: 20 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,17 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
class WebDriver {
17-
18-
protected $executor;
19-
20-
public function __construct(
21-
$url = 'http://localhost:4444/wd/hub',
22-
$desired_capabilities = array()) {
23-
$url = preg_replace('#/+$#', '', $url);
24-
25-
$command = array(
26-
'url' => $url,
27-
'name' => 'newSession',
28-
'parameters' => array('desiredCapabilities' => $desired_capabilities),
29-
);
30-
$response = WebDriverCommandExecutor::remoteExecute($command);
31-
32-
$this->executor = new WebDriverCommandExecutor(
33-
$url,
34-
$response['sessionId']
35-
);
36-
}
16+
/**
17+
* The interface for WebDriver.
18+
*/
19+
interface WebDriver {
3720

3821
/**
3922
* Close the current window.
4023
*
4124
* @return WebDriver The current instance.
4225
*/
43-
public function close() {
44-
$this->executor->execute('closeCurrentWindow', array());
45-
46-
return $this;
47-
}
26+
public function close();
4827

4928
/**
5029
* Find the first WebDriverElement using the given mechanism.
@@ -54,12 +33,7 @@ public function close() {
5433
* WebDriverCommandExecutor if no element is found.
5534
* @see WebDriverBy
5635
*/
57-
public function findElement(WebDriverBy $by) {
58-
$params = array('using' => $by->getMechanism(), 'value' => $by->getValue());
59-
$raw_element = $this->executor->execute('findElement', $params);
60-
61-
return $this->newElement($raw_element['ELEMENT']);
62-
}
36+
public function findElement(WebDriverBy $by);
6337

6438
/**
6539
* Find all WebDriverElements within the current page using the given
@@ -70,84 +44,57 @@ public function findElement(WebDriverBy $by) {
7044
* nothing matches
7145
* @see WebDriverBy
7246
*/
73-
public function findElements(WebDriverBy $by) {
74-
$params = array('using' => $by->getMechanism(), 'value' => $by->getValue());
75-
$raw_elements = $this->executor->execute('findElements', $params);
76-
77-
$elements = array();
78-
foreach ($raw_elements as $raw_element) {
79-
$elements[] = $this->newElement($raw_element['ELEMENT']);
80-
}
81-
return $elements;
82-
}
47+
public function findElements(WebDriverBy $by);
8348

8449
/**
8550
* Load a new web page in the current browser window.
8651
*
8752
* @return WebDriver The current instance.
8853
*/
89-
public function get($url) {
90-
$params = array('url' => (string)$url);
91-
$this->executor->execute('get', $params);
92-
93-
return $this;
94-
}
54+
public function get($url);
9555

9656
/**
9757
* Get a string representing the current URL that the browser is looking at.
9858
*
9959
* @return string The current URL.
10060
*/
101-
public function getCurrentURL() {
102-
return $this->executor->execute('getCurrentURL');
103-
}
61+
public function getCurrentURL();
10462

10563
/**
10664
* Get the source of the last loaded page.
10765
*
10866
* @return string The current page source.
10967
*/
110-
public function getPageSource() {
111-
return $this->executor->execute('getPageSource');
112-
}
68+
public function getPageSource();
11369

11470
/**
11571
* Get the title of the current page.
11672
*
11773
* @return string The title of the current page.
11874
*/
119-
public function getTitle() {
120-
return $this->executor->execute('getTitle');
121-
}
75+
public function getTitle();
12276

12377
/**
12478
* Return an opaque handle to this window that uniquely identifies it within
12579
* this driver instance.
12680
*
12781
* @return string The current window handle.
12882
*/
129-
public function getWindowHandle() {
130-
return $this->executor->execute('getCurrentWindowHandle', array());
131-
}
83+
public function getWindowHandle();
13284

13385
/**
13486
* Get all window handles available to the current session.
13587
*
13688
* @return array An array of string containing all available window handles.
13789
*/
138-
public function getWindowHandles() {
139-
return $this->executor->execute('getWindowHandles', array());
140-
}
90+
public function getWindowHandles();
14191

14292
/**
14393
* Quits this driver, closing every associated window.
14494
*
14595
* @return void
14696
*/
147-
public function quit() {
148-
$this->executor->execute('quit');
149-
$this->executor = null;
150-
}
97+
public function quit();
15198

15299
/**
153100
* Inject a snippet of JavaScript into the page for execution in the context
@@ -158,51 +105,15 @@ public function quit() {
158105
* @param array $arguments The arguments of the script.
159106
* @return mixed The return value of the script.
160107
*/
161-
public function executeScript($script, array $arguments = array()) {
162-
$script = str_replace('"', '\"', $script);
163-
$args = array();
164-
foreach ($arguments as $arg) {
165-
if ($arg instanceof WebDriverElement) {
166-
array_push($args, array('ELEMENT' => $arg->getID()));
167-
} else {
168-
// TODO: Handle the case where arg is a collection
169-
if (is_array($arg)) {
170-
throw new Exception(
171-
"executeScript with collection paramatar is unimplemented"
172-
);
173-
}
174-
array_push($args, $arg);
175-
}
176-
}
177-
178-
$params = array('script' => $script, 'args' => $args);
179-
$response = $this->executor->execute('executeScript', $params);
180-
181-
if (is_array($response)) {
182-
// TODO: Handle this
183-
throw new Exception(
184-
"executeScript with collection response is unimplemented"
185-
);
186-
} else {
187-
return $response;
188-
}
189-
}
108+
public function executeScript($script, array $arguments = array());
190109

191110
/**
192111
* Take a screenshot of the current page.
193112
*
194113
* @param $save_as The path of the screenshot to be saved.
195114
* @return string The screenshot in PNG format.
196115
*/
197-
public function takeScreenshot($save_as = null) {
198-
$screenshot = base64_decode(
199-
$this->executor->execute('takeScreenshot')
200-
);
201-
if ($save_as) {
202-
file_put_contents($save_as, $screenshot);
203-
}
204-
return $screenshot;
205-
}
116+
public function takeScreenshot($save_as = null);
206117

207118
/**
208119
* Construct a new WebDriverWait by the current WebDriver instance.
@@ -216,21 +127,15 @@ public function takeScreenshot($save_as = null) {
216127
*/
217128
public function wait(
218129
$timeout_in_second = 30,
219-
$interval_in_millisecond = 250) {
220-
return new WebDriverWait(
221-
$this, $timeout_in_second, $interval_in_millisecond
222-
);
223-
}
130+
$interval_in_millisecond = 250);
224131

225132
/**
226133
* An abstraction for managing stuff you would do in a browser menu. For
227134
* example, adding and deleting cookies.
228135
*
229136
* @return WebDriverOptions
230137
*/
231-
public function manage() {
232-
return new WebDriverOptions($this->executor);
233-
}
138+
public function manage();
234139

235140
/**
236141
* An abstraction allowing the driver to access the browser's history and to
@@ -239,28 +144,13 @@ public function manage() {
239144
* @return WebDriverNavigation
240145
* @see WebDriverNavigation
241146
*/
242-
public function navigate() {
243-
return new WebDriverNavigation($this->executor);
244-
}
147+
public function navigate();
245148

246149
/**
247150
* Switch to a different window or frame.
248151
*
249152
* @return WebDriverTargetLocator
250153
* @see WebDriverTargetLocator
251154
*/
252-
public function switchTo() {
253-
return new WebDriverTargetLocator($this->executor, $this);
254-
}
255-
256-
/**
257-
* Return the WebDriverElement with the given id.
258-
*
259-
* @param string $id The id of the element to be created.
260-
* @return WebDriverElement
261-
*/
262-
private function newElement($id) {
263-
return new WebDriverElement($this->executor, $id);
264-
}
265-
155+
public function switchTo();
266156
}

0 commit comments

Comments
 (0)