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.
11 namespace Behat\Mink\Element;
13 use Behat\Mink\Driver\DriverInterface;
14 use Behat\Mink\Exception\ElementNotFoundException;
15 use Behat\Mink\Selector\SelectorsHandler;
16 use Behat\Mink\Selector\Xpath\Manipulator;
17 use Behat\Mink\Session;
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 abstract class Element implements ElementInterface
34 * @var DriverInterface
39 * @var SelectorsHandler
41 private $selectorsHandler;
46 private $xpathManipulator;
51 * @param Session $session
53 public function __construct(Session $session)
55 $this->xpathManipulator = new Manipulator();
56 $this->session = $session;
58 $this->driver = $session->getDriver();
59 $this->selectorsHandler = $session->getSelectorsHandler();
63 * Returns element session.
67 * @deprecated Accessing the session from the element is deprecated as of 1.6 and will be impossible in 2.0.
69 public function getSession()
71 @trigger_error(sprintf('The method %s is deprecated as of 1.6 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
73 return $this->session;
77 * Returns element's driver.
79 * @return DriverInterface
81 protected function getDriver()
87 * Returns selectors handler.
89 * @return SelectorsHandler
91 * @deprecated Accessing the selectors handler in the element is deprecated as of 1.7 and will be impossible in 2.0.
93 protected function getSelectorsHandler()
95 @trigger_error(sprintf('The method %s is deprecated as of 1.7 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
97 return $this->selectorsHandler;
103 public function has($selector, $locator)
105 return null !== $this->find($selector, $locator);
111 public function isValid()
113 return 1 === count($this->getDriver()->find($this->getXpath()));
119 public function waitFor($timeout, $callback)
121 if (!is_callable($callback)) {
122 throw new \InvalidArgumentException('Given callback is not a valid callable');
125 $start = microtime(true);
126 $end = $start + $timeout;
129 $result = call_user_func($callback, $this);
136 } while (microtime(true) < $end);
144 public function find($selector, $locator)
146 $items = $this->findAll($selector, $locator);
148 return count($items) ? current($items) : null;
154 public function findAll($selector, $locator)
156 if ('named' === $selector) {
157 $items = $this->findAll('named_exact', $locator);
159 $items = $this->findAll('named_partial', $locator);
165 $xpath = $this->selectorsHandler->selectorToXpath($selector, $locator);
166 $xpath = $this->xpathManipulator->prepend($xpath, $this->getXpath());
168 return $this->getDriver()->find($xpath);
174 public function getText()
176 return $this->getDriver()->getText($this->getXpath());
182 public function getHtml()
184 return $this->getDriver()->getHtml($this->getXpath());
188 * Returns element outer html.
192 public function getOuterHtml()
194 return $this->getDriver()->getOuterHtml($this->getXpath());
198 * Builds an ElementNotFoundException.
200 * @param string $type
201 * @param string|null $selector
202 * @param string|null $locator
204 * @return ElementNotFoundException
206 * @deprecated as of 1.7, to be removed in 2.0
208 protected function elementNotFound($type, $selector = null, $locator = null)
210 @trigger_error(sprintf('The method %s is deprecated as of 1.7 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
212 return new ElementNotFoundException($this->driver, $type, $selector, $locator);