3 * Copyright 2004-2017 Facebook. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @author Justin Bishop <jubishop@gmail.com>
20 * @author Anthon Pang <apang@softwaredevelopment.ca>
26 * WebDriver\Session class
30 * @method string window_handle() Retrieve the current window handle.
31 * @method array window_handles() Retrieve the list of all window handles available to the session.
32 * @method string url() Retrieve the URL of the current page
33 * @method void postUrl($jsonUrl) Navigate to a new URL
34 * @method void forward() Navigates forward in the browser history, if possible.
35 * @method void back() Navigates backward in the browser history, if possible.
36 * @method void refresh() Refresh the current page.
37 * @method mixed execute($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous)
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)
39 * @method string screenshot() Take a screenshot of the current page.
40 * @method array getCookie() Retrieve all cookies visible to the current page.
41 * @method array postCookie($jsonCookie) Set a cookie.
42 * @method string source() Get the current page source.
43 * @method string title() Get the current page title.
44 * @method void keys($jsonKeys) Send a sequence of key strokes to the active element.
45 * @method string getOrientation() Get the current browser orientation.
46 * @method void postOrientation($jsonOrientation) Set the current browser orientation.
47 * @method string getAlert_text() Gets the text of the currently displayed JavaScript alert(), confirm(), or prompt() dialog.
48 * @method void postAlert_text($jsonText) Sends keystrokes to a JavaScript prompt() dialog.
49 * @method void accept_alert() Accepts the currently displayed alert dialog.
50 * @method void dismiss_alert() Dismisses the currently displayed alert dialog.
51 * @method void moveto($jsonCoordinates) Move the mouse by an offset of the specified element (or current mouse cursor).
52 * @method void click($jsonButton) Click any mouse button (at the coordinates set by the last moveto command).
53 * @method void buttondown() Click and hold the left mouse button (at the coordinates set by the last moveto command).
54 * @method void buttonup() Releases the mouse button previously held (where the mouse is currently at).
55 * @method void doubleclick() Double-clicks at the current mouse coordinates (set by moveto).
56 * @method array execute_sql($jsonQuery) Execute SQL.
57 * @method array getLocation() Get the current geo location.
58 * @method void postLocation($jsonCoordinates) Set the current geo location.
59 * @method boolean getBrowser_connection() Is browser online?
60 * @method void postBrowser_connection($jsonState) Set browser online.
62 final class Session extends Container
67 private $capabilities = null;
72 protected function methods()
75 'window_handle' => array('GET'),
76 'window_handles' => array('GET'),
77 'url' => array('GET', 'POST'), // alternate for POST, use open($url)
78 'forward' => array('POST'),
79 'back' => array('POST'),
80 'refresh' => array('POST'),
81 'execute' => array('POST'),
82 'execute_async' => array('POST'),
83 'screenshot' => array('GET'),
84 'cookie' => array('GET', 'POST'), // for DELETE, use deleteAllCookies()
85 'source' => array('GET'),
86 'title' => array('GET'),
87 'keys' => array('POST'),
88 'orientation' => array('GET', 'POST'),
89 'alert_text' => array('GET', 'POST'),
90 'accept_alert' => array('POST'),
91 'dismiss_alert' => array('POST'),
92 'moveto' => array('POST'),
93 'click' => array('POST'),
94 'buttondown' => 'POST',
95 'buttonup' => array('POST'),
96 'doubleclick' => array('POST'),
97 'execute_sql' => array('POST'),
98 'location' => array('GET', 'POST'),
99 'browser_connection' => array('GET', 'POST'),
101 // specific to Java SeleniumServer
102 'file' => array('POST'),
109 protected function obsoleteMethods()
112 'modifier' => array('POST'),
113 'speed' => array('GET', 'POST'),
114 'alert' => array('GET'),
115 'visible' => array('GET', 'POST'),
120 * Open URL: /session/:sessionId/url (POST)
121 * An alternative to $session->url($url);
125 * @return \WebDriver\Session
127 public function open($url)
129 $this->curl('POST', '/url', array('url' => $url));
135 * Get browser capabilities: /session/:sessionId (GET)
139 public function capabilities()
141 if (! isset($this->capabilities)) {
142 $result = $this->curl('GET', '');
144 $this->capabilities = $result['value'];
147 return $this->capabilities;
151 * Close session: /session/:sessionId (DELETE)
155 public function close()
157 $result = $this->curl('DELETE', '');
159 return $result['value'];
162 // There's a limit to our ability to exploit the dynamic nature of PHP when it
163 // comes to the cookie methods because GET and DELETE request methods are indistinguishable
164 // from each other: neither takes parameters.
167 * Get all cookies: /session/:sessionId/cookie (GET)
168 * Alternative to: $session->cookie();
170 * Note: get cookie by name not implemented in API
174 public function getAllCookies()
176 $result = $this->curl('GET', '/cookie');
178 return $result['value'];
182 * Set cookie: /session/:sessionId/cookie (POST)
183 * Alternative to: $session->cookie($cookie_json);
185 * @param array $cookieJson
187 * @return \WebDriver\Session
189 public function setCookie($cookieJson)
191 $this->curl('POST', '/cookie', array('cookie' => $cookieJson));
197 * Delete all cookies: /session/:sessionId/cookie (DELETE)
199 * @return \WebDriver\Session
201 public function deleteAllCookies()
203 $this->curl('DELETE', '/cookie');
209 * Delete a cookie: /session/:sessionId/cookie/:name (DELETE)
211 * @param string $cookieName
213 * @return \WebDriver\Session
215 public function deleteCookie($cookieName)
217 $this->curl('DELETE', '/cookie/' . $cookieName);
223 * window methods: /session/:sessionId/window (POST, DELETE)
224 * - $session->window() - close current window
225 * - $session->window($name) - set focus
226 * - $session->window($window_handle)->method() - chaining
228 * @return \WebDriver\Window|\WebDriver\Session
230 public function window()
232 // close current window
233 if (func_num_args() === 0) {
234 $this->curl('DELETE', '/window');
240 $arg = func_get_arg(0); // window handle or name attribute
242 if (is_array($arg)) {
243 $this->curl('POST', '/window', $arg);
249 return new Window($this->url . '/window', $arg);
253 * Delete window: /session/:sessionId/window (DELETE)
255 * @return \WebDriver\Session
257 public function deleteWindow()
259 $this->curl('DELETE', '/window');
265 * Set focus to window: /session/:sessionId/window (POST)
267 * @param mixed $name window handler or name attribute
269 * @return \WebDriver\Session
271 public function focusWindow($name)
273 $this->curl('POST', '/window', array('name' => $name));
279 * frame methods: /session/:sessionId/frame (POST)
280 * - $session->frame($json) - change focus to another frame on the page
281 * - $session->frame()->method() - chaining
283 * @return \WebDriver\Session|\WebDriver\Frame
285 public function frame()
287 if (func_num_args() === 1) {
288 $arg = func_get_arg(0); // json
290 $this->curl('POST', '/frame', $arg);
296 return new Frame($this->url . '/frame');
300 * timeouts methods: /session/:sessionId/timeouts (POST)
301 * - $session->timeouts($json) - set timeout for an operation
302 * - $session->timeouts()->method() - chaining
304 * @return \WebDriver\Session|\WebDriver\Timeouts
306 public function timeouts()
309 if (func_num_args() === 1) {
310 $arg = func_get_arg(0); // json
312 $this->curl('POST', '/timeouts', $arg);
317 if (func_num_args() === 2) {
319 'type' => func_get_arg(0), // 'script' or 'implicit'
320 'ms' => func_get_arg(1), // timeout in milliseconds
323 $this->curl('POST', '/timeouts', $arg);
329 return new Timeouts($this->url . '/timeouts');
333 * ime method chaining, e.g.,
334 * - $session->ime()->method()
336 * @return \WebDriver\Ime
338 public function ime()
340 return new Ime($this->url . '/ime');
344 * Get active element (i.e., has focus): /session/:sessionId/element/active (POST)
345 * - $session->activeElement()
349 public function activeElement()
351 $result = $this->curl('POST', '/element/active');
353 return $this->webDriverElement($result['value']);
357 * touch method chaining, e.g.,
358 * - $session->touch()->method()
360 * @return \WebDriver\Touch
363 public function touch()
365 return new Touch($this->url . '/touch');
369 * local_storage method chaining, e.g.,
370 * - $session->local_storage()->method()
372 * @return \WebDriver\Storage
374 public function local_storage()
376 return Storage::factory('local', $this->url . '/local_storage');
380 * session_storage method chaining, e.g.,
381 * - $session->session_storage()->method()
383 * @return \WebDriver\Storage
385 public function session_storage()
387 return Storage::factory('session', $this->url . '/session_storage');
391 * application cache chaining, e.g.,
392 * - $session->application_cache()->status()
394 * @return \WebDriver\ApplicationCache
396 public function application_cache()
398 return new ApplicationCache($this->url . '/application_cache');
402 * log methods: /session/:sessionId/log (POST)
403 * - $session->log($type) - get log for given log type
404 * - $session->log()->method() - chaining
408 public function log()
410 // get log for given log type
411 if (func_num_args() === 1) {
412 $arg = func_get_arg(0);
414 if (is_string($arg)) {
420 $result = $this->curl('POST', '/log', $arg);
422 return $result['value'];
426 return new Log($this->url . '/log');
432 protected function getElementPath($elementId)
434 return sprintf('%s/element/%s', $this->url, $elementId);