Chris@14
|
1 <?php
|
Chris@14
|
2 /**
|
Chris@14
|
3 * Copyright 2004-2017 Facebook. All Rights Reserved.
|
Chris@14
|
4 *
|
Chris@14
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
Chris@14
|
6 * you may not use this file except in compliance with the License.
|
Chris@14
|
7 * You may obtain a copy of the License at
|
Chris@14
|
8 *
|
Chris@14
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
Chris@14
|
10 *
|
Chris@14
|
11 * Unless required by applicable law or agreed to in writing, software
|
Chris@14
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
Chris@14
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
Chris@14
|
14 * See the License for the specific language governing permissions and
|
Chris@14
|
15 * limitations under the License.
|
Chris@14
|
16 *
|
Chris@14
|
17 * @package WebDriver
|
Chris@14
|
18 *
|
Chris@14
|
19 * @author Justin Bishop <jubishop@gmail.com>
|
Chris@14
|
20 * @author Anthon Pang <apang@softwaredevelopment.ca>
|
Chris@14
|
21 */
|
Chris@14
|
22
|
Chris@14
|
23 namespace WebDriver;
|
Chris@14
|
24
|
Chris@14
|
25 /**
|
Chris@14
|
26 * WebDriver\Session class
|
Chris@14
|
27 *
|
Chris@14
|
28 * @package WebDriver
|
Chris@14
|
29 *
|
Chris@14
|
30 * @method string window_handle() Retrieve the current window handle.
|
Chris@14
|
31 * @method array window_handles() Retrieve the list of all window handles available to the session.
|
Chris@14
|
32 * @method string url() Retrieve the URL of the current page
|
Chris@14
|
33 * @method void postUrl($jsonUrl) Navigate to a new URL
|
Chris@14
|
34 * @method void forward() Navigates forward in the browser history, if possible.
|
Chris@14
|
35 * @method void back() Navigates backward in the browser history, if possible.
|
Chris@14
|
36 * @method void refresh() Refresh the current page.
|
Chris@14
|
37 * @method mixed execute($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous)
|
Chris@14
|
38 * @method mixed execute_async($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous)
|
Chris@14
|
39 * @method string screenshot() Take a screenshot of the current page.
|
Chris@14
|
40 * @method array getCookie() Retrieve all cookies visible to the current page.
|
Chris@14
|
41 * @method array postCookie($jsonCookie) Set a cookie.
|
Chris@14
|
42 * @method string source() Get the current page source.
|
Chris@14
|
43 * @method string title() Get the current page title.
|
Chris@14
|
44 * @method void keys($jsonKeys) Send a sequence of key strokes to the active element.
|
Chris@14
|
45 * @method string getOrientation() Get the current browser orientation.
|
Chris@14
|
46 * @method void postOrientation($jsonOrientation) Set the current browser orientation.
|
Chris@14
|
47 * @method string getAlert_text() Gets the text of the currently displayed JavaScript alert(), confirm(), or prompt() dialog.
|
Chris@14
|
48 * @method void postAlert_text($jsonText) Sends keystrokes to a JavaScript prompt() dialog.
|
Chris@14
|
49 * @method void accept_alert() Accepts the currently displayed alert dialog.
|
Chris@14
|
50 * @method void dismiss_alert() Dismisses the currently displayed alert dialog.
|
Chris@14
|
51 * @method void moveto($jsonCoordinates) Move the mouse by an offset of the specified element (or current mouse cursor).
|
Chris@14
|
52 * @method void click($jsonButton) Click any mouse button (at the coordinates set by the last moveto command).
|
Chris@14
|
53 * @method void buttondown() Click and hold the left mouse button (at the coordinates set by the last moveto command).
|
Chris@14
|
54 * @method void buttonup() Releases the mouse button previously held (where the mouse is currently at).
|
Chris@14
|
55 * @method void doubleclick() Double-clicks at the current mouse coordinates (set by moveto).
|
Chris@14
|
56 * @method array execute_sql($jsonQuery) Execute SQL.
|
Chris@14
|
57 * @method array getLocation() Get the current geo location.
|
Chris@14
|
58 * @method void postLocation($jsonCoordinates) Set the current geo location.
|
Chris@14
|
59 * @method boolean getBrowser_connection() Is browser online?
|
Chris@14
|
60 * @method void postBrowser_connection($jsonState) Set browser online.
|
Chris@14
|
61 */
|
Chris@14
|
62 final class Session extends Container
|
Chris@14
|
63 {
|
Chris@14
|
64 /**
|
Chris@14
|
65 * @var array
|
Chris@14
|
66 */
|
Chris@14
|
67 private $capabilities = null;
|
Chris@14
|
68
|
Chris@14
|
69 /**
|
Chris@14
|
70 * {@inheritdoc}
|
Chris@14
|
71 */
|
Chris@14
|
72 protected function methods()
|
Chris@14
|
73 {
|
Chris@14
|
74 return array(
|
Chris@14
|
75 'window_handle' => array('GET'),
|
Chris@14
|
76 'window_handles' => array('GET'),
|
Chris@14
|
77 'url' => array('GET', 'POST'), // alternate for POST, use open($url)
|
Chris@14
|
78 'forward' => array('POST'),
|
Chris@14
|
79 'back' => array('POST'),
|
Chris@14
|
80 'refresh' => array('POST'),
|
Chris@14
|
81 'execute' => array('POST'),
|
Chris@14
|
82 'execute_async' => array('POST'),
|
Chris@14
|
83 'screenshot' => array('GET'),
|
Chris@14
|
84 'cookie' => array('GET', 'POST'), // for DELETE, use deleteAllCookies()
|
Chris@14
|
85 'source' => array('GET'),
|
Chris@14
|
86 'title' => array('GET'),
|
Chris@14
|
87 'keys' => array('POST'),
|
Chris@14
|
88 'orientation' => array('GET', 'POST'),
|
Chris@14
|
89 'alert_text' => array('GET', 'POST'),
|
Chris@14
|
90 'accept_alert' => array('POST'),
|
Chris@14
|
91 'dismiss_alert' => array('POST'),
|
Chris@14
|
92 'moveto' => array('POST'),
|
Chris@14
|
93 'click' => array('POST'),
|
Chris@14
|
94 'buttondown' => 'POST',
|
Chris@14
|
95 'buttonup' => array('POST'),
|
Chris@14
|
96 'doubleclick' => array('POST'),
|
Chris@14
|
97 'execute_sql' => array('POST'),
|
Chris@14
|
98 'location' => array('GET', 'POST'),
|
Chris@14
|
99 'browser_connection' => array('GET', 'POST'),
|
Chris@14
|
100
|
Chris@14
|
101 // specific to Java SeleniumServer
|
Chris@14
|
102 'file' => array('POST'),
|
Chris@14
|
103 );
|
Chris@14
|
104 }
|
Chris@14
|
105
|
Chris@14
|
106 /**
|
Chris@14
|
107 * {@inheritdoc}
|
Chris@14
|
108 */
|
Chris@14
|
109 protected function obsoleteMethods()
|
Chris@14
|
110 {
|
Chris@14
|
111 return array(
|
Chris@14
|
112 'modifier' => array('POST'),
|
Chris@14
|
113 'speed' => array('GET', 'POST'),
|
Chris@14
|
114 'alert' => array('GET'),
|
Chris@14
|
115 'visible' => array('GET', 'POST'),
|
Chris@14
|
116 );
|
Chris@14
|
117 }
|
Chris@14
|
118
|
Chris@14
|
119 /**
|
Chris@14
|
120 * Open URL: /session/:sessionId/url (POST)
|
Chris@14
|
121 * An alternative to $session->url($url);
|
Chris@14
|
122 *
|
Chris@14
|
123 * @param string $url
|
Chris@14
|
124 *
|
Chris@14
|
125 * @return \WebDriver\Session
|
Chris@14
|
126 */
|
Chris@14
|
127 public function open($url)
|
Chris@14
|
128 {
|
Chris@14
|
129 $this->curl('POST', '/url', array('url' => $url));
|
Chris@14
|
130
|
Chris@14
|
131 return $this;
|
Chris@14
|
132 }
|
Chris@14
|
133
|
Chris@14
|
134 /**
|
Chris@14
|
135 * Get browser capabilities: /session/:sessionId (GET)
|
Chris@14
|
136 *
|
Chris@14
|
137 * @return mixed
|
Chris@14
|
138 */
|
Chris@14
|
139 public function capabilities()
|
Chris@14
|
140 {
|
Chris@14
|
141 if (! isset($this->capabilities)) {
|
Chris@14
|
142 $result = $this->curl('GET', '');
|
Chris@14
|
143
|
Chris@14
|
144 $this->capabilities = $result['value'];
|
Chris@14
|
145 }
|
Chris@14
|
146
|
Chris@14
|
147 return $this->capabilities;
|
Chris@14
|
148 }
|
Chris@14
|
149
|
Chris@14
|
150 /**
|
Chris@14
|
151 * Close session: /session/:sessionId (DELETE)
|
Chris@14
|
152 *
|
Chris@14
|
153 * @return mixed
|
Chris@14
|
154 */
|
Chris@14
|
155 public function close()
|
Chris@14
|
156 {
|
Chris@14
|
157 $result = $this->curl('DELETE', '');
|
Chris@14
|
158
|
Chris@14
|
159 return $result['value'];
|
Chris@14
|
160 }
|
Chris@14
|
161
|
Chris@14
|
162 // There's a limit to our ability to exploit the dynamic nature of PHP when it
|
Chris@14
|
163 // comes to the cookie methods because GET and DELETE request methods are indistinguishable
|
Chris@14
|
164 // from each other: neither takes parameters.
|
Chris@14
|
165
|
Chris@14
|
166 /**
|
Chris@14
|
167 * Get all cookies: /session/:sessionId/cookie (GET)
|
Chris@14
|
168 * Alternative to: $session->cookie();
|
Chris@14
|
169 *
|
Chris@14
|
170 * Note: get cookie by name not implemented in API
|
Chris@14
|
171 *
|
Chris@14
|
172 * @return mixed
|
Chris@14
|
173 */
|
Chris@14
|
174 public function getAllCookies()
|
Chris@14
|
175 {
|
Chris@14
|
176 $result = $this->curl('GET', '/cookie');
|
Chris@14
|
177
|
Chris@14
|
178 return $result['value'];
|
Chris@14
|
179 }
|
Chris@14
|
180
|
Chris@14
|
181 /**
|
Chris@14
|
182 * Set cookie: /session/:sessionId/cookie (POST)
|
Chris@14
|
183 * Alternative to: $session->cookie($cookie_json);
|
Chris@14
|
184 *
|
Chris@14
|
185 * @param array $cookieJson
|
Chris@14
|
186 *
|
Chris@14
|
187 * @return \WebDriver\Session
|
Chris@14
|
188 */
|
Chris@14
|
189 public function setCookie($cookieJson)
|
Chris@14
|
190 {
|
Chris@14
|
191 $this->curl('POST', '/cookie', array('cookie' => $cookieJson));
|
Chris@14
|
192
|
Chris@14
|
193 return $this;
|
Chris@14
|
194 }
|
Chris@14
|
195
|
Chris@14
|
196 /**
|
Chris@14
|
197 * Delete all cookies: /session/:sessionId/cookie (DELETE)
|
Chris@14
|
198 *
|
Chris@14
|
199 * @return \WebDriver\Session
|
Chris@14
|
200 */
|
Chris@14
|
201 public function deleteAllCookies()
|
Chris@14
|
202 {
|
Chris@14
|
203 $this->curl('DELETE', '/cookie');
|
Chris@14
|
204
|
Chris@14
|
205 return $this;
|
Chris@14
|
206 }
|
Chris@14
|
207
|
Chris@14
|
208 /**
|
Chris@14
|
209 * Delete a cookie: /session/:sessionId/cookie/:name (DELETE)
|
Chris@14
|
210 *
|
Chris@14
|
211 * @param string $cookieName
|
Chris@14
|
212 *
|
Chris@14
|
213 * @return \WebDriver\Session
|
Chris@14
|
214 */
|
Chris@14
|
215 public function deleteCookie($cookieName)
|
Chris@14
|
216 {
|
Chris@14
|
217 $this->curl('DELETE', '/cookie/' . $cookieName);
|
Chris@14
|
218
|
Chris@14
|
219 return $this;
|
Chris@14
|
220 }
|
Chris@14
|
221
|
Chris@14
|
222 /**
|
Chris@14
|
223 * window methods: /session/:sessionId/window (POST, DELETE)
|
Chris@14
|
224 * - $session->window() - close current window
|
Chris@14
|
225 * - $session->window($name) - set focus
|
Chris@14
|
226 * - $session->window($window_handle)->method() - chaining
|
Chris@14
|
227 *
|
Chris@14
|
228 * @return \WebDriver\Window|\WebDriver\Session
|
Chris@14
|
229 */
|
Chris@14
|
230 public function window()
|
Chris@14
|
231 {
|
Chris@14
|
232 // close current window
|
Chris@14
|
233 if (func_num_args() === 0) {
|
Chris@14
|
234 $this->curl('DELETE', '/window');
|
Chris@14
|
235
|
Chris@14
|
236 return $this;
|
Chris@14
|
237 }
|
Chris@14
|
238
|
Chris@14
|
239 // set focus
|
Chris@14
|
240 $arg = func_get_arg(0); // window handle or name attribute
|
Chris@14
|
241
|
Chris@14
|
242 if (is_array($arg)) {
|
Chris@14
|
243 $this->curl('POST', '/window', $arg);
|
Chris@14
|
244
|
Chris@14
|
245 return $this;
|
Chris@14
|
246 }
|
Chris@14
|
247
|
Chris@14
|
248 // chaining
|
Chris@14
|
249 return new Window($this->url . '/window', $arg);
|
Chris@14
|
250 }
|
Chris@14
|
251
|
Chris@14
|
252 /**
|
Chris@14
|
253 * Delete window: /session/:sessionId/window (DELETE)
|
Chris@14
|
254 *
|
Chris@14
|
255 * @return \WebDriver\Session
|
Chris@14
|
256 */
|
Chris@14
|
257 public function deleteWindow()
|
Chris@14
|
258 {
|
Chris@14
|
259 $this->curl('DELETE', '/window');
|
Chris@14
|
260
|
Chris@14
|
261 return $this;
|
Chris@14
|
262 }
|
Chris@14
|
263
|
Chris@14
|
264 /**
|
Chris@14
|
265 * Set focus to window: /session/:sessionId/window (POST)
|
Chris@14
|
266 *
|
Chris@14
|
267 * @param mixed $name window handler or name attribute
|
Chris@14
|
268 *
|
Chris@14
|
269 * @return \WebDriver\Session
|
Chris@14
|
270 */
|
Chris@14
|
271 public function focusWindow($name)
|
Chris@14
|
272 {
|
Chris@14
|
273 $this->curl('POST', '/window', array('name' => $name));
|
Chris@14
|
274
|
Chris@14
|
275 return $this;
|
Chris@14
|
276 }
|
Chris@14
|
277
|
Chris@14
|
278 /**
|
Chris@14
|
279 * frame methods: /session/:sessionId/frame (POST)
|
Chris@14
|
280 * - $session->frame($json) - change focus to another frame on the page
|
Chris@14
|
281 * - $session->frame()->method() - chaining
|
Chris@14
|
282 *
|
Chris@14
|
283 * @return \WebDriver\Session|\WebDriver\Frame
|
Chris@14
|
284 */
|
Chris@14
|
285 public function frame()
|
Chris@14
|
286 {
|
Chris@14
|
287 if (func_num_args() === 1) {
|
Chris@14
|
288 $arg = func_get_arg(0); // json
|
Chris@14
|
289
|
Chris@14
|
290 $this->curl('POST', '/frame', $arg);
|
Chris@14
|
291
|
Chris@14
|
292 return $this;
|
Chris@14
|
293 }
|
Chris@14
|
294
|
Chris@14
|
295 // chaining
|
Chris@14
|
296 return new Frame($this->url . '/frame');
|
Chris@14
|
297 }
|
Chris@14
|
298
|
Chris@14
|
299 /**
|
Chris@14
|
300 * timeouts methods: /session/:sessionId/timeouts (POST)
|
Chris@14
|
301 * - $session->timeouts($json) - set timeout for an operation
|
Chris@14
|
302 * - $session->timeouts()->method() - chaining
|
Chris@14
|
303 *
|
Chris@14
|
304 * @return \WebDriver\Session|\WebDriver\Timeouts
|
Chris@14
|
305 */
|
Chris@14
|
306 public function timeouts()
|
Chris@14
|
307 {
|
Chris@14
|
308 // set timeouts
|
Chris@14
|
309 if (func_num_args() === 1) {
|
Chris@14
|
310 $arg = func_get_arg(0); // json
|
Chris@14
|
311
|
Chris@14
|
312 $this->curl('POST', '/timeouts', $arg);
|
Chris@14
|
313
|
Chris@14
|
314 return $this;
|
Chris@14
|
315 }
|
Chris@14
|
316
|
Chris@14
|
317 if (func_num_args() === 2) {
|
Chris@14
|
318 $arg = array(
|
Chris@14
|
319 'type' => func_get_arg(0), // 'script' or 'implicit'
|
Chris@14
|
320 'ms' => func_get_arg(1), // timeout in milliseconds
|
Chris@14
|
321 );
|
Chris@14
|
322
|
Chris@14
|
323 $this->curl('POST', '/timeouts', $arg);
|
Chris@14
|
324
|
Chris@14
|
325 return $this;
|
Chris@14
|
326 }
|
Chris@14
|
327
|
Chris@14
|
328 // chaining
|
Chris@14
|
329 return new Timeouts($this->url . '/timeouts');
|
Chris@14
|
330 }
|
Chris@14
|
331
|
Chris@14
|
332 /**
|
Chris@14
|
333 * ime method chaining, e.g.,
|
Chris@14
|
334 * - $session->ime()->method()
|
Chris@14
|
335 *
|
Chris@14
|
336 * @return \WebDriver\Ime
|
Chris@14
|
337 */
|
Chris@14
|
338 public function ime()
|
Chris@14
|
339 {
|
Chris@14
|
340 return new Ime($this->url . '/ime');
|
Chris@14
|
341 }
|
Chris@14
|
342
|
Chris@14
|
343 /**
|
Chris@14
|
344 * Get active element (i.e., has focus): /session/:sessionId/element/active (POST)
|
Chris@14
|
345 * - $session->activeElement()
|
Chris@14
|
346 *
|
Chris@14
|
347 * @return mixed
|
Chris@14
|
348 */
|
Chris@14
|
349 public function activeElement()
|
Chris@14
|
350 {
|
Chris@14
|
351 $result = $this->curl('POST', '/element/active');
|
Chris@14
|
352
|
Chris@14
|
353 return $this->webDriverElement($result['value']);
|
Chris@14
|
354 }
|
Chris@14
|
355
|
Chris@14
|
356 /**
|
Chris@14
|
357 * touch method chaining, e.g.,
|
Chris@14
|
358 * - $session->touch()->method()
|
Chris@14
|
359 *
|
Chris@14
|
360 * @return \WebDriver\Touch
|
Chris@14
|
361 *
|
Chris@14
|
362 */
|
Chris@14
|
363 public function touch()
|
Chris@14
|
364 {
|
Chris@14
|
365 return new Touch($this->url . '/touch');
|
Chris@14
|
366 }
|
Chris@14
|
367
|
Chris@14
|
368 /**
|
Chris@14
|
369 * local_storage method chaining, e.g.,
|
Chris@14
|
370 * - $session->local_storage()->method()
|
Chris@14
|
371 *
|
Chris@14
|
372 * @return \WebDriver\Storage
|
Chris@14
|
373 */
|
Chris@14
|
374 public function local_storage()
|
Chris@14
|
375 {
|
Chris@14
|
376 return Storage::factory('local', $this->url . '/local_storage');
|
Chris@14
|
377 }
|
Chris@14
|
378
|
Chris@14
|
379 /**
|
Chris@14
|
380 * session_storage method chaining, e.g.,
|
Chris@14
|
381 * - $session->session_storage()->method()
|
Chris@14
|
382 *
|
Chris@14
|
383 * @return \WebDriver\Storage
|
Chris@14
|
384 */
|
Chris@14
|
385 public function session_storage()
|
Chris@14
|
386 {
|
Chris@14
|
387 return Storage::factory('session', $this->url . '/session_storage');
|
Chris@14
|
388 }
|
Chris@14
|
389
|
Chris@14
|
390 /**
|
Chris@14
|
391 * application cache chaining, e.g.,
|
Chris@14
|
392 * - $session->application_cache()->status()
|
Chris@14
|
393 *
|
Chris@14
|
394 * @return \WebDriver\ApplicationCache
|
Chris@14
|
395 */
|
Chris@14
|
396 public function application_cache()
|
Chris@14
|
397 {
|
Chris@14
|
398 return new ApplicationCache($this->url . '/application_cache');
|
Chris@14
|
399 }
|
Chris@14
|
400
|
Chris@14
|
401 /**
|
Chris@14
|
402 * log methods: /session/:sessionId/log (POST)
|
Chris@14
|
403 * - $session->log($type) - get log for given log type
|
Chris@14
|
404 * - $session->log()->method() - chaining
|
Chris@14
|
405 *
|
Chris@14
|
406 * @return mixed
|
Chris@14
|
407 */
|
Chris@14
|
408 public function log()
|
Chris@14
|
409 {
|
Chris@14
|
410 // get log for given log type
|
Chris@14
|
411 if (func_num_args() === 1) {
|
Chris@14
|
412 $arg = func_get_arg(0);
|
Chris@14
|
413
|
Chris@14
|
414 if (is_string($arg)) {
|
Chris@14
|
415 $arg = array(
|
Chris@14
|
416 'type' => $arg,
|
Chris@14
|
417 );
|
Chris@14
|
418 }
|
Chris@14
|
419
|
Chris@14
|
420 $result = $this->curl('POST', '/log', $arg);
|
Chris@14
|
421
|
Chris@14
|
422 return $result['value'];
|
Chris@14
|
423 }
|
Chris@14
|
424
|
Chris@14
|
425 // chaining
|
Chris@14
|
426 return new Log($this->url . '/log');
|
Chris@14
|
427 }
|
Chris@14
|
428
|
Chris@14
|
429 /**
|
Chris@14
|
430 * {@inheritdoc}
|
Chris@14
|
431 */
|
Chris@14
|
432 protected function getElementPath($elementId)
|
Chris@14
|
433 {
|
Chris@14
|
434 return sprintf('%s/element/%s', $this->url, $elementId);
|
Chris@14
|
435 }
|
Chris@14
|
436 }
|