Security update for permissions_by_term
[yaffs-website] / vendor / instaclick / php-webdriver / lib / WebDriver / Session.php
1 <?php
2 /**
3  * Copyright 2004-2017 Facebook. All Rights Reserved.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * @package WebDriver
18  *
19  * @author Justin Bishop <jubishop@gmail.com>
20  * @author Anthon Pang <apang@softwaredevelopment.ca>
21  */
22
23 namespace WebDriver;
24
25 /**
26  * WebDriver\Session class
27  *
28  * @package WebDriver
29  *
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.
61  */
62 final class Session extends Container
63 {
64     /**
65      * @var array
66      */
67     private $capabilities = null;
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function methods()
73     {
74         return array(
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'),
100
101             // specific to Java SeleniumServer
102             'file' => array('POST'),
103         );
104     }
105
106     /**
107      * {@inheritdoc}
108      */
109     protected function obsoleteMethods()
110     {
111         return array(
112             'modifier' => array('POST'),
113             'speed' => array('GET', 'POST'),
114             'alert' => array('GET'),
115             'visible' => array('GET', 'POST'),
116         );
117     }
118
119     /**
120      * Open URL: /session/:sessionId/url (POST)
121      * An alternative to $session->url($url);
122      *
123      * @param string $url
124      *
125      * @return \WebDriver\Session
126      */
127     public function open($url)
128     {
129         $this->curl('POST', '/url', array('url' => $url));
130
131         return $this;
132     }
133
134     /**
135      * Get browser capabilities: /session/:sessionId (GET)
136      *
137      * @return mixed
138      */
139     public function capabilities()
140     {
141         if (! isset($this->capabilities)) {
142             $result = $this->curl('GET', '');
143
144             $this->capabilities = $result['value'];
145         }
146
147         return $this->capabilities;
148     }
149
150     /**
151      * Close session: /session/:sessionId (DELETE)
152      *
153      * @return mixed
154      */
155     public function close()
156     {
157         $result = $this->curl('DELETE', '');
158
159         return $result['value'];
160     }
161
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.
165
166     /**
167      * Get all cookies: /session/:sessionId/cookie (GET)
168      * Alternative to: $session->cookie();
169      *
170      * Note: get cookie by name not implemented in API
171      *
172      * @return mixed
173      */
174     public function getAllCookies()
175     {
176         $result = $this->curl('GET', '/cookie');
177
178         return $result['value'];
179     }
180
181     /**
182      * Set cookie: /session/:sessionId/cookie (POST)
183      * Alternative to: $session->cookie($cookie_json);
184      *
185      * @param array $cookieJson
186      *
187      * @return \WebDriver\Session
188      */
189     public function setCookie($cookieJson)
190     {
191         $this->curl('POST', '/cookie', array('cookie' => $cookieJson));
192
193         return $this;
194     }
195
196     /**
197      * Delete all cookies: /session/:sessionId/cookie (DELETE)
198      *
199      * @return \WebDriver\Session
200      */
201     public function deleteAllCookies()
202     {
203         $this->curl('DELETE', '/cookie');
204
205         return $this;
206     }
207
208     /**
209      * Delete a cookie: /session/:sessionId/cookie/:name (DELETE)
210      *
211      * @param string $cookieName
212      *
213      * @return \WebDriver\Session
214      */
215     public function deleteCookie($cookieName)
216     {
217         $this->curl('DELETE', '/cookie/' . $cookieName);
218
219         return $this;
220     }
221
222     /**
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
227      *
228      * @return \WebDriver\Window|\WebDriver\Session
229      */
230     public function window()
231     {
232         // close current window
233         if (func_num_args() === 0) {
234             $this->curl('DELETE', '/window');
235
236             return $this;
237         }
238
239         // set focus
240         $arg = func_get_arg(0); // window handle or name attribute
241
242         if (is_array($arg)) {
243             $this->curl('POST', '/window', $arg);
244
245             return $this;
246         }
247
248         // chaining
249         return new Window($this->url . '/window', $arg);
250     }
251
252     /**
253      * Delete window: /session/:sessionId/window (DELETE)
254      *
255      * @return \WebDriver\Session
256      */
257     public function deleteWindow()
258     {
259         $this->curl('DELETE', '/window');
260
261         return $this;
262     }
263
264     /**
265      * Set focus to window: /session/:sessionId/window (POST)
266      *
267      * @param mixed $name window handler or name attribute
268      *
269      * @return \WebDriver\Session
270      */
271     public function focusWindow($name)
272     {
273         $this->curl('POST', '/window', array('name' => $name));
274
275         return $this;
276     }
277
278     /**
279      * frame methods: /session/:sessionId/frame (POST)
280      * - $session->frame($json) - change focus to another frame on the page
281      * - $session->frame()->method() - chaining
282      *
283      * @return \WebDriver\Session|\WebDriver\Frame
284      */
285     public function frame()
286     {
287         if (func_num_args() === 1) {
288             $arg = func_get_arg(0); // json
289
290             $this->curl('POST', '/frame', $arg);
291
292             return $this;
293         }
294
295         // chaining
296         return new Frame($this->url . '/frame');
297     }
298
299     /**
300      * timeouts methods: /session/:sessionId/timeouts (POST)
301      * - $session->timeouts($json) - set timeout for an operation
302      * - $session->timeouts()->method() - chaining
303      *
304      * @return \WebDriver\Session|\WebDriver\Timeouts
305      */
306     public function timeouts()
307     {
308         // set timeouts
309         if (func_num_args() === 1) {
310             $arg = func_get_arg(0); // json
311
312             $this->curl('POST', '/timeouts', $arg);
313
314             return $this;
315         }
316
317         if (func_num_args() === 2) {
318             $arg = array(
319                 'type' => func_get_arg(0), // 'script' or 'implicit'
320                 'ms' => func_get_arg(1),   // timeout in milliseconds
321             );
322
323             $this->curl('POST', '/timeouts', $arg);
324
325             return $this;
326         }
327
328         // chaining
329         return new Timeouts($this->url . '/timeouts');
330     }
331
332     /**
333      * ime method chaining, e.g.,
334      * - $session->ime()->method()
335      *
336      * @return \WebDriver\Ime
337      */
338     public function ime()
339     {
340         return new Ime($this->url . '/ime');
341     }
342
343     /**
344      * Get active element (i.e., has focus): /session/:sessionId/element/active (POST)
345      * - $session->activeElement()
346      *
347      * @return mixed
348      */
349     public function activeElement()
350     {
351         $result = $this->curl('POST', '/element/active');
352
353         return $this->webDriverElement($result['value']);
354     }
355
356     /**
357      * touch method chaining, e.g.,
358      * - $session->touch()->method()
359      *
360      * @return \WebDriver\Touch
361      *
362      */
363     public function touch()
364     {
365         return new Touch($this->url . '/touch');
366     }
367
368     /**
369      * local_storage method chaining, e.g.,
370      * - $session->local_storage()->method()
371      *
372      * @return \WebDriver\Storage
373      */
374     public function local_storage()
375     {
376         return Storage::factory('local', $this->url . '/local_storage');
377     }
378
379     /**
380      * session_storage method chaining, e.g.,
381      * - $session->session_storage()->method()
382      *
383      * @return \WebDriver\Storage
384      */
385     public function session_storage()
386     {
387         return Storage::factory('session', $this->url . '/session_storage');
388     }
389
390     /**
391      * application cache chaining, e.g.,
392      * - $session->application_cache()->status()
393      *
394      * @return \WebDriver\ApplicationCache
395      */
396     public function application_cache()
397     {
398         return new ApplicationCache($this->url . '/application_cache');
399     }
400
401     /**
402      * log methods: /session/:sessionId/log (POST)
403      * - $session->log($type) - get log for given log type
404      * - $session->log()->method() - chaining
405      *
406      * @return mixed
407      */
408     public function log()
409     {
410         // get log for given log type
411         if (func_num_args() === 1) {
412             $arg = func_get_arg(0);
413
414             if (is_string($arg)) {
415                 $arg = array(
416                     'type' => $arg,
417                 );
418             }
419
420             $result = $this->curl('POST', '/log', $arg);
421
422             return $result['value'];
423         }
424
425         // chaining
426         return new Log($this->url . '/log');
427     }
428
429     /**
430      * {@inheritdoc}
431      */
432     protected function getElementPath($elementId)
433     {
434         return sprintf('%s/element/%s', $this->url, $elementId);
435     }
436 }