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