forked from php-webdriver/php-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteWebDriver.php
More file actions
317 lines (286 loc) · 8.13 KB
/
Copy pathRemoteWebDriver.php
File metadata and controls
317 lines (286 loc) · 8.13 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
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
<?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.
class RemoteWebDriver implements WebDriver {
protected $executor;
protected $mouse;
protected $keyboard;
protected $touch;
public function __construct(
$url = 'http://localhost:4444/wd/hub',
$desired_capabilities = array()) {
$url = preg_replace('#/+$#', '', $url);
$command = array(
'url' => $url,
'name' => 'newSession',
'parameters' => array('desiredCapabilities' => $desired_capabilities),
);
$response = HttpCommandExecutor::remoteExecuteInit($command);
$this->executor = new HttpCommandExecutor(
$url,
$response['sessionId']
);
}
/**
* Close the current window.
*
* @return WebDriver The current instance.
*/
public function close() {
$this->executor->execute('closeCurrentWindow', array());
$this->executor->close();
return $this;
}
/**
* Find the first WebDriverElement using the given mechanism.
*
* @param WebDriverBy $by
* @return WebDriverElement NoSuchElementWebDriverError is thrown in
* HttpCommandExecutor if no element is found.
* @see WebDriverBy
*/
public function findElement(WebDriverBy $by) {
$params = array('using' => $by->getMechanism(), 'value' => $by->getValue());
$raw_element = $this->executor->execute('findElement', $params);
return $this->newElement($raw_element['ELEMENT']);
}
/**
* Find all WebDriverElements within the current page using the given
* mechanism.
*
* @param WebDriverBy $by
* @return array A list of all WebDriverElements, or an empty array if
* nothing matches
* @see WebDriverBy
*/
public function findElements(WebDriverBy $by) {
$params = array('using' => $by->getMechanism(), 'value' => $by->getValue());
$raw_elements = $this->executor->execute('findElements', $params);
$elements = array();
foreach ($raw_elements as $raw_element) {
$elements[] = $this->newElement($raw_element['ELEMENT']);
}
return $elements;
}
/**
* Load a new web page in the current browser window.
*
* @return WebDriver The current instance.
*/
public function get($url) {
$params = array('url' => (string)$url);
$this->executor->execute('get', $params);
return $this;
}
/**
* Get a string representing the current URL that the browser is looking at.
*
* @return string The current URL.
*/
public function getCurrentURL() {
return $this->executor->execute('getCurrentURL');
}
/**
* Get the source of the last loaded page.
*
* @return string The current page source.
*/
public function getPageSource() {
return $this->executor->execute('getPageSource');
}
/**
* Get the title of the current page.
*
* @return string The title of the current page.
*/
public function getTitle() {
return $this->executor->execute('getTitle');
}
/**
* Return an opaque handle to this window that uniquely identifies it within
* this driver instance.
*
* @return string The current window handle.
*/
public function getWindowHandle() {
return $this->executor->execute('getCurrentWindowHandle', array());
}
/**
* Get all window handles available to the current session.
*
* @return array An array of string containing all available window handles.
*/
public function getWindowHandles() {
return $this->executor->execute('getWindowHandles', array());
}
/**
* Quits this driver, closing every associated window.
*
* @return void
*/
public function quit() {
$this->executor->execute('quit');
$this->executor = null;
}
/**
* Prepare arguments for JavaScript injection
*
* @param array $arguments
* @return array
*/
private function prepareScriptArguments(array $arguments) {
$args = array();
foreach ($arguments as $arg) {
if ($arg instanceof WebDriverElement) {
array_push($args, array('ELEMENT' => $arg->getID()));
} else {
if (is_array($arg)) {
$arg = $this->prepareScriptArguments($arg);
}
array_push($args, $arg);
}
}
return $args;
}
/**
* Inject a snippet of JavaScript into the page for execution in the context
* of the currently selected frame. The executed script is assumed to be
* synchronous and the result of evaluating the script will be returned.
*
* @param string $script The script to inject.
* @param array $arguments The arguments of the script.
* @return mixed The return value of the script.
*/
public function executeScript($script, array $arguments = array()) {
$params = array(
'script' => $script,
'args' => $this->prepareScriptArguments($arguments),
);
$response = $this->executor->execute('executeScript', $params);
return $response;
}
/**
* Take a screenshot of the current page.
*
* @param $save_as The path of the screenshot to be saved.
* @return string The screenshot in PNG format.
*/
public function takeScreenshot($save_as = null) {
$screenshot = base64_decode(
$this->executor->execute('takeScreenshot')
);
if ($save_as) {
file_put_contents($save_as, $screenshot);
}
return $screenshot;
}
/**
* Construct a new WebDriverWait by the current WebDriver instance.
* Sample usage:
*
* $driver->wait(20, 1000)->until(
* WebDriverExpectedCondition::titleIs('WebDriver Page')
* );
*
* @return WebDriverWait
*/
public function wait(
$timeout_in_second = 30,
$interval_in_millisecond = 250) {
return new WebDriverWait(
$this, $timeout_in_second, $interval_in_millisecond
);
}
/**
* An abstraction for managing stuff you would do in a browser menu. For
* example, adding and deleting cookies.
*
* @return WebDriverOptions
*/
public function manage() {
return new WebDriverOptions($this->executor);
}
/**
* An abstraction allowing the driver to access the browser's history and to
* navigate to a given URL.
*
* @return WebDriverNavigation
* @see WebDriverNavigation
*/
public function navigate() {
return new WebDriverNavigation($this->executor);
}
/**
* Switch to a different window or frame.
*
* @return WebDriverTargetLocator
* @see WebDriverTargetLocator
*/
public function switchTo() {
return new WebDriverTargetLocator($this->executor, $this);
}
/**
* @return WebDriverMouse
*/
public function getMouse() {
if (!$this->mouse) {
$this->mouse = new RemoteMouse($this->executor);
}
return $this->mouse;
}
/**
* @return WebDriverKeyboard
*/
public function getKeyboard() {
if (!$this->keyboard) {
$this->keyboard = new RemoteKeyboard($this->executor);
}
return $this->keyboard;
}
/**
* @return WebDriverTouchScreen
*/
public function getTouch() {
if (!$this->touch) {
$this->touch = new RemoteTouchScreen($this->executor);
}
return $this->touch;
}
/**
* Construct a new action builder.
*
* @return WebDriverActions
*/
public function action() {
return new WebDriverActions($this);
}
/**
* Get the element on the page that currently has focus.
*
* @return WebDriverElement
*/
public function getActiveElement() {
$response = $this->executor->execute('getActiveElement');
return $this->newElement($response['ELEMENT']);
}
/**
* Return the WebDriverElement with the given id.
*
* @param string $id The id of the element to be created.
* @return WebDriverElement
*/
private function newElement($id) {
return new RemoteWebElement($this->executor, $id);
}
}