4 * This file is part of the Mink package.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
13 use Behat\Mink\Driver\DriverInterface;
14 use Behat\Mink\Selector\SelectorsHandler;
15 use Behat\Mink\Element\DocumentElement;
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 private $selectorsHandler;
29 * Initializes session.
31 * @param DriverInterface $driver
32 * @param SelectorsHandler $selectorsHandler
34 public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler = null)
36 $driver->setSession($this);
38 if (null === $selectorsHandler) {
39 $selectorsHandler = new SelectorsHandler();
42 $this->driver = $driver;
43 $this->selectorsHandler = $selectorsHandler;
44 $this->page = new DocumentElement($this);
48 * Checks whether session (driver) was started.
52 public function isStarted()
54 return $this->driver->isStarted();
58 * Starts session driver.
60 * Calling any action before visiting a page is an undefined behavior.
61 * The only supported method calls on a fresh driver are
63 * - setRequestHeader()
68 public function start()
70 $this->driver->start();
74 * Stops session driver.
76 public function stop()
78 $this->driver->stop();
82 * Restart session driver.
84 public function restart()
86 $this->driver->stop();
87 $this->driver->start();
91 * Reset session driver state.
93 * Calling any action before visiting a page is an undefined behavior.
94 * The only supported method calls on a fresh driver are
96 * - setRequestHeader()
101 public function reset()
103 $this->driver->reset();
107 * Returns session driver.
109 * @return DriverInterface
111 public function getDriver()
113 return $this->driver;
117 * Returns page element.
119 * @return DocumentElement
121 public function getPage()
127 * Returns selectors handler.
129 * @return SelectorsHandler
131 public function getSelectorsHandler()
133 return $this->selectorsHandler;
137 * Visit specified URL.
139 * @param string $url url of the page
141 public function visit($url)
143 $this->driver->visit($url);
147 * Sets HTTP Basic authentication parameters.
149 * @param string|Boolean $user user name or false to disable authentication
150 * @param string $password password
152 public function setBasicAuth($user, $password = '')
154 $this->driver->setBasicAuth($user, $password);
158 * Sets specific request header.
160 * @param string $name
161 * @param string $value
163 public function setRequestHeader($name, $value)
165 $this->driver->setRequestHeader($name, $value);
169 * Returns all response headers.
173 public function getResponseHeaders()
175 return $this->driver->getResponseHeaders();
179 * Returns specific response header.
181 * @param string $name
183 * @return string|null
185 public function getResponseHeader($name)
187 $headers = $this->driver->getResponseHeaders();
189 $name = strtolower($name);
190 $headers = array_change_key_case($headers, CASE_LOWER);
192 if (!isset($headers[$name])) {
196 return is_array($headers[$name]) ? $headers[$name][0] : $headers[$name];
202 * @param string $name
203 * @param string $value
205 public function setCookie($name, $value = null)
207 $this->driver->setCookie($name, $value);
211 * Returns cookie by name.
213 * @param string $name
215 * @return string|null
217 public function getCookie($name)
219 return $this->driver->getCookie($name);
223 * Returns response status code.
227 public function getStatusCode()
229 return $this->driver->getStatusCode();
233 * Returns current URL address.
237 public function getCurrentUrl()
239 return $this->driver->getCurrentUrl();
243 * Capture a screenshot of the current window.
245 * @return string screenshot of MIME type image/* depending
246 * on driver (e.g., image/png, image/jpeg)
248 public function getScreenshot()
250 return $this->driver->getScreenshot();
254 * Return the names of all open windows.
256 * @return array Array of all open window's names.
258 public function getWindowNames()
260 return $this->driver->getWindowNames();
264 * Return the name of the currently active window.
266 * @return string The name of the current window.
268 public function getWindowName()
270 return $this->driver->getWindowName();
274 * Reloads current session page.
276 public function reload()
278 $this->driver->reload();
282 * Moves backward 1 page in history.
284 public function back()
286 $this->driver->back();
290 * Moves forward 1 page in history.
292 public function forward()
294 $this->driver->forward();
298 * Switches to specific browser window.
300 * @param string $name window name (null for switching back to main window)
302 public function switchToWindow($name = null)
304 $this->driver->switchToWindow($name);
308 * Switches to specific iFrame.
310 * @param string $name iframe name (null for switching back)
312 public function switchToIFrame($name = null)
314 $this->driver->switchToIFrame($name);
318 * Execute JS in browser.
320 * @param string $script javascript
322 public function executeScript($script)
324 $this->driver->executeScript($script);
328 * Execute JS in browser and return it's response.
330 * @param string $script javascript
334 public function evaluateScript($script)
336 return $this->driver->evaluateScript($script);
340 * Waits some time or until JS condition turns true.
342 * @param int $time time in milliseconds
343 * @param string $condition JS condition
347 public function wait($time, $condition = 'false')
349 return $this->driver->wait($time, $condition);
353 * Set the dimensions of the window.
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)
359 public function resizeWindow($width, $height, $name = null)
361 $this->driver->resizeWindow($width, $height, $name);
365 * Maximize the window if it is not maximized already.
367 * @param string $name window name (null for the main window)
369 public function maximizeWindow($name = null)
371 $this->driver->maximizeWindow($name);