Version 1
[yaffs-website] / vendor / behat / mink / src / Session.php
1 <?php
2
3 /*
4  * This file is part of the Mink package.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Mink;
12
13 use Behat\Mink\Driver\DriverInterface;
14 use Behat\Mink\Selector\SelectorsHandler;
15 use Behat\Mink\Element\DocumentElement;
16
17 /**
18  * Mink session.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 class Session
23 {
24     private $driver;
25     private $page;
26     private $selectorsHandler;
27
28     /**
29      * Initializes session.
30      *
31      * @param DriverInterface  $driver
32      * @param SelectorsHandler $selectorsHandler
33      */
34     public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler = null)
35     {
36         $driver->setSession($this);
37
38         if (null === $selectorsHandler) {
39             $selectorsHandler = new SelectorsHandler();
40         }
41
42         $this->driver = $driver;
43         $this->selectorsHandler = $selectorsHandler;
44         $this->page = new DocumentElement($this);
45     }
46
47     /**
48      * Checks whether session (driver) was started.
49      *
50      * @return Boolean
51      */
52     public function isStarted()
53     {
54         return $this->driver->isStarted();
55     }
56
57     /**
58      * Starts session driver.
59      *
60      * Calling any action before visiting a page is an undefined behavior.
61      * The only supported method calls on a fresh driver are
62      * - visit()
63      * - setRequestHeader()
64      * - setBasicAuth()
65      * - reset()
66      * - stop()
67      */
68     public function start()
69     {
70         $this->driver->start();
71     }
72
73     /**
74      * Stops session driver.
75      */
76     public function stop()
77     {
78         $this->driver->stop();
79     }
80
81     /**
82      * Restart session driver.
83      */
84     public function restart()
85     {
86         $this->driver->stop();
87         $this->driver->start();
88     }
89
90     /**
91      * Reset session driver state.
92      *
93      * Calling any action before visiting a page is an undefined behavior.
94      * The only supported method calls on a fresh driver are
95      * - visit()
96      * - setRequestHeader()
97      * - setBasicAuth()
98      * - reset()
99      * - stop()
100      */
101     public function reset()
102     {
103         $this->driver->reset();
104     }
105
106     /**
107      * Returns session driver.
108      *
109      * @return DriverInterface
110      */
111     public function getDriver()
112     {
113         return $this->driver;
114     }
115
116     /**
117      * Returns page element.
118      *
119      * @return DocumentElement
120      */
121     public function getPage()
122     {
123         return $this->page;
124     }
125
126     /**
127      * Returns selectors handler.
128      *
129      * @return SelectorsHandler
130      */
131     public function getSelectorsHandler()
132     {
133         return $this->selectorsHandler;
134     }
135
136     /**
137      * Visit specified URL.
138      *
139      * @param string $url url of the page
140      */
141     public function visit($url)
142     {
143         $this->driver->visit($url);
144     }
145
146     /**
147      * Sets HTTP Basic authentication parameters.
148      *
149      * @param string|Boolean $user     user name or false to disable authentication
150      * @param string         $password password
151      */
152     public function setBasicAuth($user, $password = '')
153     {
154         $this->driver->setBasicAuth($user, $password);
155     }
156
157     /**
158      * Sets specific request header.
159      *
160      * @param string $name
161      * @param string $value
162      */
163     public function setRequestHeader($name, $value)
164     {
165         $this->driver->setRequestHeader($name, $value);
166     }
167
168     /**
169      * Returns all response headers.
170      *
171      * @return array
172      */
173     public function getResponseHeaders()
174     {
175         return $this->driver->getResponseHeaders();
176     }
177
178     /**
179      * Returns specific response header.
180      *
181      * @param string $name
182      *
183      * @return string|null
184      */
185     public function getResponseHeader($name)
186     {
187         $headers = $this->driver->getResponseHeaders();
188
189         $name = strtolower($name);
190         $headers = array_change_key_case($headers, CASE_LOWER);
191
192         if (!isset($headers[$name])) {
193             return null;
194         }
195
196         return is_array($headers[$name]) ? $headers[$name][0] : $headers[$name];
197     }
198
199     /**
200      * Sets cookie.
201      *
202      * @param string $name
203      * @param string $value
204      */
205     public function setCookie($name, $value = null)
206     {
207         $this->driver->setCookie($name, $value);
208     }
209
210     /**
211      * Returns cookie by name.
212      *
213      * @param string $name
214      *
215      * @return string|null
216      */
217     public function getCookie($name)
218     {
219         return $this->driver->getCookie($name);
220     }
221
222     /**
223      * Returns response status code.
224      *
225      * @return int
226      */
227     public function getStatusCode()
228     {
229         return $this->driver->getStatusCode();
230     }
231
232     /**
233      * Returns current URL address.
234      *
235      * @return string
236      */
237     public function getCurrentUrl()
238     {
239         return $this->driver->getCurrentUrl();
240     }
241
242     /**
243      * Capture a screenshot of the current window.
244      *
245      * @return string screenshot of MIME type image/* depending
246      *                on driver (e.g., image/png, image/jpeg)
247      */
248     public function getScreenshot()
249     {
250         return $this->driver->getScreenshot();
251     }
252
253     /**
254      * Return the names of all open windows.
255      *
256      * @return array Array of all open window's names.
257      */
258     public function getWindowNames()
259     {
260         return $this->driver->getWindowNames();
261     }
262
263     /**
264      * Return the name of the currently active window.
265      *
266      * @return string The name of the current window.
267      */
268     public function getWindowName()
269     {
270         return $this->driver->getWindowName();
271     }
272
273     /**
274      * Reloads current session page.
275      */
276     public function reload()
277     {
278         $this->driver->reload();
279     }
280
281     /**
282      * Moves backward 1 page in history.
283      */
284     public function back()
285     {
286         $this->driver->back();
287     }
288
289     /**
290      * Moves forward 1 page in history.
291      */
292     public function forward()
293     {
294         $this->driver->forward();
295     }
296
297     /**
298      * Switches to specific browser window.
299      *
300      * @param string $name window name (null for switching back to main window)
301      */
302     public function switchToWindow($name = null)
303     {
304         $this->driver->switchToWindow($name);
305     }
306
307     /**
308      * Switches to specific iFrame.
309      *
310      * @param string $name iframe name (null for switching back)
311      */
312     public function switchToIFrame($name = null)
313     {
314         $this->driver->switchToIFrame($name);
315     }
316
317     /**
318      * Execute JS in browser.
319      *
320      * @param string $script javascript
321      */
322     public function executeScript($script)
323     {
324         $this->driver->executeScript($script);
325     }
326
327     /**
328      * Execute JS in browser and return it's response.
329      *
330      * @param string $script javascript
331      *
332      * @return string
333      */
334     public function evaluateScript($script)
335     {
336         return $this->driver->evaluateScript($script);
337     }
338
339     /**
340      * Waits some time or until JS condition turns true.
341      *
342      * @param int    $time      time in milliseconds
343      * @param string $condition JS condition
344      *
345      * @return bool
346      */
347     public function wait($time, $condition = 'false')
348     {
349         return $this->driver->wait($time, $condition);
350     }
351
352     /**
353      * Set the dimensions of the window.
354      *
355      * @param int    $width  set the window width, measured in pixels
356      * @param int    $height set the window height, measured in pixels
357      * @param string $name   window name (null for the main window)
358      */
359     public function resizeWindow($width, $height, $name = null)
360     {
361         $this->driver->resizeWindow($width, $height, $name);
362     }
363
364     /**
365      * Maximize the window if it is not maximized already.
366      *
367      * @param string $name window name (null for the main window)
368      */
369     public function maximizeWindow($name = null)
370     {
371         $this->driver->maximizeWindow($name);
372     }
373 }