-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathSession.php
565 lines (488 loc) · 15.2 KB
/
Session.php
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
/**
* @copyright 2004 Meta Platforms, Inc.
* @license Apache-2.0
*
* @package WebDriver
*
* @author Justin Bishop <[email protected]>
*/
namespace WebDriver;
/**
* WebDriver\Session class
*
* @package WebDriver
*
* @method void accept_alert() Accepts the currently displayed alert dialog.
* @method array deleteActions() Release actions.
* @method array postActions($json) Perform actions.
* @method string getAlert_text() Gets the text of the currently displayed JavaScript alert(), confirm(), or prompt() dialog.
* @method void postAlert_text($jsonText) Sends keystrokes to a JavaScript prompt() dialog.
* @method void back() Navigates backward in the browser history, if possible.
* @method boolean getBrowser_connection() Is browser online?
* @method void postBrowser_connection($jsonState) Set browser online.
* @method void buttondown() Click and hold the left mouse button (at the coordinates set by the last moveto command).
* @method void buttonup() Releases the mouse button previously held (where the mouse is currently at).
* @method void click($jsonButton) Click any mouse button (at the coordinates set by the last moveto command).
* @method array getCookie() Retrieve all cookies visible to the current page.
* @method array postCookie($jsonCookie) Set a cookie.
* @method void dismiss_alert() Dismisses the currently displayed alert dialog.
* @method void doubleclick() Double-clicks at the current mouse coordinates (set by moveto).
* @method array execute_sql($jsonQuery) Execute SQL.
* @method array file($jsonFile) Upload file.
* @method void forward() Navigates forward in the browser history, if possible.
* @method void keys($jsonKeys) Send a sequence of key strokes to the active element.
* @method array getLocation() Get the current geo location.
* @method void postLocation($jsonCoordinates) Set the current geo location.
* @method string getOrientation() Get the current browser orientation.
* @method void postOrientation($jsonOrientation) Set the current browser orientation.
* @method array print() Print page.
* @method void refresh() Refresh the current page.
* @method string screenshot() Take a screenshot of the current page.
* @method string source() Get the current page source.
* @method string title() Get the current page title.
* @method string url() Retrieve the URL of the current page.
* @method void postUrl($jsonUrl) Navigate to a new URL.
* @method string window_handle() Retrieve the current window handle.
* @method array window_handles() Retrieve the list of all window handles available to the session.
*/
class Session extends Container
{
/**
* @var array
*/
private $capabilities = null;
/**
* @var boolean
*/
private $w3c = null;
/**
* {@inheritdoc}
*/
protected function methods()
{
return array(
'actions' => array('POST', 'DELETE'),
'back' => array('POST'),
'cookie' => array('GET', 'POST'), // for DELETE, use deleteAllCookies()
'forward' => array('POST'),
'print' => array('POST'),
'refresh' => array('POST'),
'screenshot' => array('GET'),
'source' => array('GET'),
'title' => array('GET'),
'url' => array('GET', 'POST'), // alternate for POST, use open($url)
// specific to Java SeleniumServer
'file' => array('POST'),
// Legacy JSON Wire Protocol
'accept_alert' => array('POST'),
'alert_text' => array('GET', 'POST'),
'browser_connection' => array('GET', 'POST'),
'buttondown' => 'POST',
'buttonup' => array('POST'),
'click' => array('POST'),
'dismiss_alert' => array('POST'),
'doubleclick' => array('POST'),
'execute_sql' => array('POST'),
'keys' => array('POST'),
'location' => array('GET', 'POST'),
'orientation' => array('GET', 'POST'),
'window_handle' => array('GET'), // see also getWindowHandle()
'window_handles' => array('GET'),
);
}
/**
* {@inheritdoc}
*/
protected function obsoleteMethods()
{
return array(
'alert' => array('GET'),
'modifier' => array('POST'),
'speed' => array('GET', 'POST'),
'visible' => array('GET', 'POST'),
);
}
/**
* Constructor
*
* @param string $url URL to Selenium server
* @param array|null $capabilities
*/
public function __construct($url, $capabilities)
{
parent::__construct($url);
$this->capabilities = $capabilities;
$this->w3c = !! $capabilities;
}
/**
* Is W3C webdriver?
*
* @return boolean
*/
public function isW3C()
{
return $this->w3c;
}
/**
* Open URL: /session/:sessionId/url (POST)
* An alternative to $session->url($url);
*
* @param string $url
*
* @return \WebDriver\Session
*/
public function open($url)
{
$this->curl('POST', '/url', array('url' => $url));
return $this;
}
/**
* Get browser capabilities: /session/:sessionId (GET)
*
* @return mixed
*/
public function capabilities()
{
if ($this->capabilities === null) {
$result = $this->curl('GET', '');
$this->capabilities = $result['value'];
}
return $this->capabilities;
}
/**
* Close session: /session/:sessionId (DELETE)
*
* @return mixed
*/
public function close()
{
$result = $this->curl('DELETE', '');
return $result['value'];
}
// There's a limit to our ability to exploit the dynamic nature of PHP when it
// comes to the cookie methods because GET and DELETE request methods are indistinguishable
// from each other: neither takes parameters.
/**
* Get all cookies: /session/:sessionId/cookie (GET)
* Alternative to: $session->cookie();
*
* Note: get cookie by name not implemented in API
*
* @return mixed
*/
public function getAllCookies()
{
$result = $this->curl('GET', '/cookie');
return $result['value'];
}
/**
* Set cookie: /session/:sessionId/cookie (POST)
* Alternative to: $session->cookie($cookie_json);
*
* @param array $cookieJson
*
* @return \WebDriver\Session
*/
public function setCookie($cookieJson)
{
$this->curl('POST', '/cookie', array('cookie' => $cookieJson));
return $this;
}
/**
* Delete all cookies: /session/:sessionId/cookie (DELETE)
*
* @return \WebDriver\Session
*/
public function deleteAllCookies()
{
$this->curl('DELETE', '/cookie');
return $this;
}
/**
* Delete a cookie: /session/:sessionId/cookie/:name (DELETE)
*
* @param string $cookieName
*
* @return \WebDriver\Session
*/
public function deleteCookie($cookieName)
{
$this->curl('DELETE', '/cookie/' . $cookieName);
return $this;
}
/**
* Get window handle: /session/:sessionId/window (GET)
* : /session/:sessionId/window_handle (GET)
* - $session->getWindowHandle()
*
* An alternative to $session->window()->getHandle()
*
* @return mixed
*/
public function getWindowHandle()
{
$result = $this->curl('GET', $this->w3c ? '/window' : '/window_handle');
return $result['value'];
}
/**
* New window: /session/:sessionId/window/new (POST)
* - $session->newWindow($type)
*
* @internal "new" is a reserved keyword in PHP, so $session->window()->new() isn't possible
*
* @return \WebDriver\Window
*/
public function newWindow($type)
{
$arg = func_get_arg(0); // json
$result = $this->curl('POST', '/window/new', $arg);
return $result['value'];
}
/**
* window method chaining: /session/:sessionId/window (POST
* - $session->window($jsonHandle) - set focus
* - $session->window($handle)->method() - chaining
* - $session->window()->method() - chaining
*
* @return \WebDriver\Session|\WebDriver\Window|\WebDriver\LegacyWindow
*/
public function window()
{
$arg = null;
// set window focus / switch to window
if (func_num_args() === 1) {
$arg = func_get_arg(0); // window handle or name attribute
if (is_array($arg)) {
$this->curl('POST', '/window', $arg);
return $this;
}
}
// chaining (with optional handle in $arg)
return $this->w3c ? new Window($this->url . '/window', $arg) : new LegacyWindow($this->url . '/window', $arg);
}
/**
* Close window: /session/:sessionId/window (DELETE)
*
* @return \WebDriver\Session
*/
public function deleteWindow()
{
$this->curl('DELETE', '/window');
return $this;
}
/**
* Set focus to window: /session/:sessionId/window (POST)
*
* @param mixed $handle window handle (or legacy name) attribute
*
* @return \WebDriver\Session
*/
public function focusWindow($handle)
{
$this->curl('POST', '/window', array('handle' => $handle, 'name' => $handle));
return $this;
}
/**
* frame methods: /session/:sessionId/frame (POST)
* - $session->frame($json) - change focus to another frame on the page
* - $session->frame()->method() - chaining
*
* @return \WebDriver\Session|\WebDriver\Frame
*/
public function frame()
{
if (func_num_args() === 1) {
$arg = $this->serializeArguments(func_get_arg(0)); // json
$this->curl('POST', '/frame', $arg);
return $this;
}
// chaining
return new Frame($this->url . '/frame');
}
/**
* Get timeouts (W3C): /session/:sessionId/timeouts (GET)
* - $session->getTimeouts()
*
* @return mixed
*/
public function getTimeouts()
{
$result = $this->curl('GET', '/timeouts');
return $result['value'];
}
/**
* timeouts methods: /session/:sessionId/timeouts (POST)
* - $session->timeouts($json) - set timeout for an operation
* - $session->timeouts()->method() - chaining
*
* @return \WebDriver\Session|\WebDriver\Timeouts
*/
public function timeouts()
{
// set timeouts
if (func_num_args() === 1) {
$arg = func_get_arg(0); // json
$this->curl('POST', '/timeouts', $arg);
return $this;
}
if (func_num_args() === 2) {
$type = func_get_arg(0); // 'script', 'implicit', or 'pageLoad' (legacy: 'pageLoad')
$timeout = func_get_arg(1); // timeout in milliseconds
$arg = $this->w3c ? array($type => $timeout) : array('type' => $type, 'ms' => $timeout);
$this->curl('POST', '/timeouts', $arg);
return $this;
}
// chaining
return new Timeouts($this->url . '/timeouts');
}
/**
* ime method chaining, e.g.,
* - $session->ime()->method()
*
* @return \WebDriver\Ime
*/
public function ime()
{
return new Ime($this->url . '/ime');
}
/**
* Get active element (i.e., has focus): /session/:sessionId/element/active (POST)
* - $session->activeElement()
*
* @return mixed
*/
public function activeElement()
{
$result = $this->curl('POST', '/element/active');
return $this->makeElement($result['value']);
}
/**
* touch method chaining, e.g.,
* - $session->touch()->method()
*
* @return \WebDriver\Touch
*
*/
public function touch()
{
return new Touch($this->url . '/touch');
}
/**
* local_storage method chaining, e.g.,
* - $session->local_storage()->method()
*
* @return \WebDriver\Storage\Local
*/
public function localStorage()
{
return new Storage\Local($this->url . '/local_storage');
}
/**
* session_storage method chaining, e.g.,
* - $session->session_storage()->method()
*
* @return \WebDriver\Storage\Session
*/
public function sessionStorage()
{
return new Storage\Session($this->url . '/session_storage');
}
/**
* application cache chaining, e.g.,
* - $session->application_cache()->method() - chaining
*
* @return \WebDriver\ApplicationCache
*/
public function applicationCache()
{
return new ApplicationCache($this->url . '/application_cache');
}
/**
* log methods: /session/:sessionId/log (POST)
* - $session->log($type) - get log for given log type
* - $session->log()->method() - chaining
*
* @return mixed
*/
public function log()
{
// get log for given log type
if (func_num_args() === 1) {
$arg = func_get_arg(0);
if (is_string($arg)) {
$arg = array(
'type' => $arg,
);
}
$result = $this->curl('POST', '/log', $arg);
return $result['value'];
}
// chaining
return new Log($this->url . '/log');
}
/**
* alert method chaining, e.g.,
* - $session->alert()->method() - chaining
*
* @return mixed
*/
public function alert()
{
return new Alert($this->url . '/alert');
}
/**
* script execution method chaining, e.g.,
* - $session->execute($jsonScript) - fallback for legacy JSON Wire Protocol
* - $session->execute()->method() - chaining
*
* @return mixed
*/
public function execute()
{
// execute script
if (func_num_args() > 0) {
$execute = $this->w3c ? new Execute($this->url . '/execute') : new LegacyExecute($this->url);
$result = $execute->sync(func_get_arg(0));
return $result;
}
// W3C method chaining
return new Execute($this->url . '/execute');
}
/**
* async script execution
* - $session->execute_async($jsonScript)
*
* @return mixed
*/
public function executeAsync()
{
$execute = $this->w3c ? new Execute($this->url . '/execute') : new LegacyExecute($this->url);
$result = $execute->async(func_get_arg(0));
return $result;
}
/**
* {@inheritdoc}
*/
protected function getIdentifierPath($identifier)
{
return sprintf('%s/element/%s', $this->url, $identifier);
}
/**
* {@inheritdoc}
*/
public function __call($name, $arguments)
{
$map = [
'application_cache' => 'applicationCache',
'execute_async' => 'executeAsync',
'local_storage' => 'localStorage',
'session_storage' => 'sessionStorage',
];
if (array_key_exists($name, $map)) {
return call_user_func_array([$this, $map[$name]], $arguments);
}
// fallback to executing WebDriver commands
return parent::__call($name, $arguments);
}
}