/
Session.php
429 lines (387 loc) · 9.07 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
<?php
/*
* This file is part of the Mink package.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\Mink;
use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Element\ElementFinder;
use Behat\Mink\Selector\SelectorsHandler;
use Behat\Mink\Element\DocumentElement;
/**
* Mink session.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class Session
{
/**
* @var DriverInterface
*/
private $driver;
/**
* @var DocumentElement
*/
private $page;
/**
* @var ElementFinder
*/
private $elementFinder;
/**
* @var SelectorsHandler
*/
private $selectorsHandler;
public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler = null)
{
$this->driver = $driver;
$this->selectorsHandler = $selectorsHandler ?? new SelectorsHandler();
$this->elementFinder = new ElementFinder($driver, $this->selectorsHandler);
$this->page = new DocumentElement($this);
$driver->setSession($this);
}
/**
* Checks whether session (driver) was started.
*
* @return bool
*/
public function isStarted()
{
return $this->driver->isStarted();
}
/**
* Starts session driver.
*
* Calling any action before visiting a page is an undefined behavior.
* The only supported method calls on a fresh driver are
* - visit()
* - setRequestHeader()
* - setBasicAuth()
* - reset()
* - stop()
*
* @return void
*/
public function start()
{
$this->driver->start();
}
/**
* Stops session driver.
*
* @return void
*/
public function stop()
{
$this->driver->stop();
}
/**
* Restart session driver.
*
* @return void
*/
public function restart()
{
$this->driver->stop();
$this->driver->start();
}
/**
* Reset session driver state.
*
* Calling any action before visiting a page is an undefined behavior.
* The only supported method calls on a fresh driver are
* - visit()
* - setRequestHeader()
* - setBasicAuth()
* - reset()
* - stop()
*
* @return void
*/
public function reset()
{
$this->driver->reset();
}
/**
* Returns session driver.
*
* @return DriverInterface
*/
public function getDriver()
{
return $this->driver;
}
/**
* Returns page element.
*
* @return DocumentElement
*/
public function getPage()
{
return $this->page;
}
/**
* @internal
*/
public function getElementFinder(): ElementFinder
{
return $this->elementFinder;
}
/**
* Returns selectors handler.
*
* @return SelectorsHandler
*
* @deprecated since 1.11
*/
public function getSelectorsHandler()
{
@trigger_error(sprintf('The method %s is deprecated as of 1.11 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
return $this->selectorsHandler;
}
/**
* Visit specified URL and automatically start session if not already running.
*
* @param string $url url of the page
*
* @return void
*/
public function visit(string $url)
{
// start session if needed
if (!$this->isStarted()) {
$this->start();
}
$this->driver->visit($url);
}
/**
* Sets HTTP Basic authentication parameters.
*
* @param string|false $user user name or false to disable authentication
* @param string $password password
*
* @return void
*/
public function setBasicAuth($user, string $password = '')
{
$this->driver->setBasicAuth($user, $password);
}
/**
* Sets specific request header.
*
* @param string $name
* @param string $value
*
* @return void
*/
public function setRequestHeader(string $name, string $value)
{
$this->driver->setRequestHeader($name, $value);
}
/**
* Returns all response headers.
*
* @return array<string, string|string[]>
*/
public function getResponseHeaders()
{
return $this->driver->getResponseHeaders();
}
/**
* Returns specific response header.
*
* @param string $name
*
* @return string|null
*/
public function getResponseHeader(string $name)
{
$headers = $this->driver->getResponseHeaders();
$name = strtolower($name);
$headers = array_change_key_case($headers, CASE_LOWER);
if (!isset($headers[$name])) {
return null;
}
return is_array($headers[$name]) ? $headers[$name][0] : $headers[$name];
}
/**
* Sets cookie.
*
* Passing null as value will delete the cookie.
*
* @param string $name
* @param string|null $value
*
* @return void
*/
public function setCookie(string $name, ?string $value = null)
{
$this->driver->setCookie($name, $value);
}
/**
* Returns cookie by name.
*
* @param string $name
*
* @return string|null
*/
public function getCookie(string $name)
{
return $this->driver->getCookie($name);
}
/**
* Returns response status code.
*
* @return int
*/
public function getStatusCode()
{
return $this->driver->getStatusCode();
}
/**
* Returns current URL address.
*
* @return string
*/
public function getCurrentUrl()
{
return $this->driver->getCurrentUrl();
}
/**
* Capture a screenshot of the current window.
*
* @return string screenshot of MIME type image/* depending
* on driver (e.g., image/png, image/jpeg)
*/
public function getScreenshot()
{
return $this->driver->getScreenshot();
}
/**
* Return the names of all open windows.
*
* @return string[] Array of all open window's names.
*/
public function getWindowNames()
{
return $this->driver->getWindowNames();
}
/**
* Return the name of the currently active window.
*
* @return string The name of the current window.
*/
public function getWindowName()
{
return $this->driver->getWindowName();
}
/**
* Reloads current session page.
*
* @return void
*/
public function reload()
{
$this->driver->reload();
}
/**
* Moves backward 1 page in history.
*
* @return void
*/
public function back()
{
$this->driver->back();
}
/**
* Moves forward 1 page in history.
*
* @return void
*/
public function forward()
{
$this->driver->forward();
}
/**
* Switches to specific browser window.
*
* @param string|null $name window name (null for switching back to main window)
*
* @return void
*/
public function switchToWindow(?string $name = null)
{
$this->driver->switchToWindow($name);
}
/**
* Switches to specific iFrame.
*
* @param string|null $name iframe name (null for switching back)
*
* @return void
*/
public function switchToIFrame(?string $name = null)
{
$this->driver->switchToIFrame($name);
}
/**
* Execute JS in browser.
*
* @param string $script javascript
*
* @return void
*/
public function executeScript(string $script)
{
$this->driver->executeScript($script);
}
/**
* Execute JS in browser and return its response.
*
* @param string $script javascript
*
* @return mixed
*/
public function evaluateScript(string $script)
{
return $this->driver->evaluateScript($script);
}
/**
* Waits some time or until JS condition turns true.
*
* @param int $time time in milliseconds
* @param string $condition JS condition
*
* @return bool
*/
public function wait(int $time, string $condition = 'false')
{
return $this->driver->wait($time, $condition);
}
/**
* Set the dimensions of the window.
*
* @param int $width set the window width, measured in pixels
* @param int $height set the window height, measured in pixels
* @param string|null $name window name (null for the main window)
*
* @return void
*/
public function resizeWindow(int $width, int $height, ?string $name = null)
{
$this->driver->resizeWindow($width, $height, $name);
}
/**
* Maximize the window if it is not maximized already.
*
* @param string|null $name window name (null for the main window)
*
* @return void
*/
public function maximizeWindow(?string $name = null)
{
$this->driver->maximizeWindow($name);
}
}