Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Mink package.
|
Chris@0
|
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 namespace Behat\Mink\Element;
|
Chris@0
|
12
|
Chris@0
|
13 use Behat\Mink\Driver\DriverInterface;
|
Chris@0
|
14 use Behat\Mink\Exception\ElementNotFoundException;
|
Chris@0
|
15 use Behat\Mink\Selector\SelectorsHandler;
|
Chris@0
|
16 use Behat\Mink\Selector\Xpath\Manipulator;
|
Chris@0
|
17 use Behat\Mink\Session;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Base element.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 abstract class Element implements ElementInterface
|
Chris@0
|
25 {
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @var Session
|
Chris@0
|
28 */
|
Chris@0
|
29 private $session;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Driver.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var DriverInterface
|
Chris@0
|
35 */
|
Chris@0
|
36 private $driver;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @var SelectorsHandler
|
Chris@0
|
40 */
|
Chris@0
|
41 private $selectorsHandler;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * @var Manipulator
|
Chris@0
|
45 */
|
Chris@0
|
46 private $xpathManipulator;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Initialize element.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param Session $session
|
Chris@0
|
52 */
|
Chris@0
|
53 public function __construct(Session $session)
|
Chris@0
|
54 {
|
Chris@0
|
55 $this->xpathManipulator = new Manipulator();
|
Chris@0
|
56 $this->session = $session;
|
Chris@0
|
57
|
Chris@0
|
58 $this->driver = $session->getDriver();
|
Chris@0
|
59 $this->selectorsHandler = $session->getSelectorsHandler();
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Returns element session.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @return Session
|
Chris@0
|
66 *
|
Chris@0
|
67 * @deprecated Accessing the session from the element is deprecated as of 1.6 and will be impossible in 2.0.
|
Chris@0
|
68 */
|
Chris@0
|
69 public function getSession()
|
Chris@0
|
70 {
|
Chris@0
|
71 @trigger_error(sprintf('The method %s is deprecated as of 1.6 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
|
Chris@0
|
72
|
Chris@0
|
73 return $this->session;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Returns element's driver.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return DriverInterface
|
Chris@0
|
80 */
|
Chris@0
|
81 protected function getDriver()
|
Chris@0
|
82 {
|
Chris@0
|
83 return $this->driver;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Returns selectors handler.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @return SelectorsHandler
|
Chris@0
|
90 *
|
Chris@0
|
91 * @deprecated Accessing the selectors handler in the element is deprecated as of 1.7 and will be impossible in 2.0.
|
Chris@0
|
92 */
|
Chris@0
|
93 protected function getSelectorsHandler()
|
Chris@0
|
94 {
|
Chris@0
|
95 @trigger_error(sprintf('The method %s is deprecated as of 1.7 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
|
Chris@0
|
96
|
Chris@0
|
97 return $this->selectorsHandler;
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * {@inheritdoc}
|
Chris@0
|
102 */
|
Chris@0
|
103 public function has($selector, $locator)
|
Chris@0
|
104 {
|
Chris@0
|
105 return null !== $this->find($selector, $locator);
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * {@inheritdoc}
|
Chris@0
|
110 */
|
Chris@0
|
111 public function isValid()
|
Chris@0
|
112 {
|
Chris@0
|
113 return 1 === count($this->getDriver()->find($this->getXpath()));
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function waitFor($timeout, $callback)
|
Chris@0
|
120 {
|
Chris@0
|
121 if (!is_callable($callback)) {
|
Chris@0
|
122 throw new \InvalidArgumentException('Given callback is not a valid callable');
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 $start = microtime(true);
|
Chris@0
|
126 $end = $start + $timeout;
|
Chris@0
|
127
|
Chris@0
|
128 do {
|
Chris@0
|
129 $result = call_user_func($callback, $this);
|
Chris@0
|
130
|
Chris@0
|
131 if ($result) {
|
Chris@0
|
132 break;
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 usleep(100000);
|
Chris@0
|
136 } while (microtime(true) < $end);
|
Chris@0
|
137
|
Chris@0
|
138 return $result;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * {@inheritdoc}
|
Chris@0
|
143 */
|
Chris@0
|
144 public function find($selector, $locator)
|
Chris@0
|
145 {
|
Chris@0
|
146 $items = $this->findAll($selector, $locator);
|
Chris@0
|
147
|
Chris@0
|
148 return count($items) ? current($items) : null;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * {@inheritdoc}
|
Chris@0
|
153 */
|
Chris@0
|
154 public function findAll($selector, $locator)
|
Chris@0
|
155 {
|
Chris@0
|
156 if ('named' === $selector) {
|
Chris@0
|
157 $items = $this->findAll('named_exact', $locator);
|
Chris@0
|
158 if (empty($items)) {
|
Chris@0
|
159 $items = $this->findAll('named_partial', $locator);
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 return $items;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 $xpath = $this->selectorsHandler->selectorToXpath($selector, $locator);
|
Chris@0
|
166 $xpath = $this->xpathManipulator->prepend($xpath, $this->getXpath());
|
Chris@0
|
167
|
Chris@0
|
168 return $this->getDriver()->find($xpath);
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * {@inheritdoc}
|
Chris@0
|
173 */
|
Chris@0
|
174 public function getText()
|
Chris@0
|
175 {
|
Chris@0
|
176 return $this->getDriver()->getText($this->getXpath());
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@0
|
180 * {@inheritdoc}
|
Chris@0
|
181 */
|
Chris@0
|
182 public function getHtml()
|
Chris@0
|
183 {
|
Chris@0
|
184 return $this->getDriver()->getHtml($this->getXpath());
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * Returns element outer html.
|
Chris@0
|
189 *
|
Chris@0
|
190 * @return string
|
Chris@0
|
191 */
|
Chris@0
|
192 public function getOuterHtml()
|
Chris@0
|
193 {
|
Chris@0
|
194 return $this->getDriver()->getOuterHtml($this->getXpath());
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 /**
|
Chris@0
|
198 * Builds an ElementNotFoundException.
|
Chris@0
|
199 *
|
Chris@0
|
200 * @param string $type
|
Chris@0
|
201 * @param string|null $selector
|
Chris@0
|
202 * @param string|null $locator
|
Chris@0
|
203 *
|
Chris@0
|
204 * @return ElementNotFoundException
|
Chris@0
|
205 *
|
Chris@0
|
206 * @deprecated as of 1.7, to be removed in 2.0
|
Chris@0
|
207 */
|
Chris@0
|
208 protected function elementNotFound($type, $selector = null, $locator = null)
|
Chris@0
|
209 {
|
Chris@0
|
210 @trigger_error(sprintf('The method %s is deprecated as of 1.7 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
|
Chris@0
|
211
|
Chris@0
|
212 return new ElementNotFoundException($this->driver, $type, $selector, $locator);
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|