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\Exception\ElementNotFoundException;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Traversable element.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
19 */
|
Chris@0
|
20 abstract class TraversableElement extends Element
|
Chris@0
|
21 {
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Finds element by its id.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param string $id element id
|
Chris@0
|
26 *
|
Chris@0
|
27 * @return NodeElement|null
|
Chris@0
|
28 */
|
Chris@0
|
29 public function findById($id)
|
Chris@0
|
30 {
|
Chris@0
|
31 return $this->find('named', array('id', $id));
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Checks whether element has a link with specified locator.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param string $locator link id, title, text or image alt
|
Chris@0
|
38 *
|
Chris@0
|
39 * @return Boolean
|
Chris@0
|
40 */
|
Chris@0
|
41 public function hasLink($locator)
|
Chris@0
|
42 {
|
Chris@0
|
43 return null !== $this->findLink($locator);
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Finds link with specified locator.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @param string $locator link id, title, text or image alt
|
Chris@0
|
50 *
|
Chris@0
|
51 * @return NodeElement|null
|
Chris@0
|
52 */
|
Chris@0
|
53 public function findLink($locator)
|
Chris@0
|
54 {
|
Chris@0
|
55 return $this->find('named', array('link', $locator));
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Clicks link with specified locator.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @param string $locator link id, title, text or image alt
|
Chris@0
|
62 *
|
Chris@0
|
63 * @throws ElementNotFoundException
|
Chris@0
|
64 */
|
Chris@0
|
65 public function clickLink($locator)
|
Chris@0
|
66 {
|
Chris@0
|
67 $link = $this->findLink($locator);
|
Chris@0
|
68
|
Chris@0
|
69 if (null === $link) {
|
Chris@0
|
70 throw new ElementNotFoundException($this->getDriver(), 'link', 'id|title|alt|text', $locator);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 $link->click();
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Checks whether element has a button (input[type=submit|image|button|reset], button) with specified locator.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @param string $locator button id, value or alt
|
Chris@0
|
80 *
|
Chris@0
|
81 * @return Boolean
|
Chris@0
|
82 */
|
Chris@0
|
83 public function hasButton($locator)
|
Chris@0
|
84 {
|
Chris@0
|
85 return null !== $this->findButton($locator);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Finds button (input[type=submit|image|button|reset], button) with specified locator.
|
Chris@0
|
90 *
|
Chris@0
|
91 * @param string $locator button id, value or alt
|
Chris@0
|
92 *
|
Chris@0
|
93 * @return NodeElement|null
|
Chris@0
|
94 */
|
Chris@0
|
95 public function findButton($locator)
|
Chris@0
|
96 {
|
Chris@0
|
97 return $this->find('named', array('button', $locator));
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Presses button (input[type=submit|image|button|reset], button) with specified locator.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @param string $locator button id, value or alt
|
Chris@0
|
104 *
|
Chris@0
|
105 * @throws ElementNotFoundException
|
Chris@0
|
106 */
|
Chris@0
|
107 public function pressButton($locator)
|
Chris@0
|
108 {
|
Chris@0
|
109 $button = $this->findButton($locator);
|
Chris@0
|
110
|
Chris@0
|
111 if (null === $button) {
|
Chris@0
|
112 throw new ElementNotFoundException($this->getDriver(), 'button', 'id|name|title|alt|value', $locator);
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 $button->press();
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Checks whether element has a field (input, textarea, select) with specified locator.
|
Chris@0
|
120 *
|
Chris@0
|
121 * @param string $locator input id, name or label
|
Chris@0
|
122 *
|
Chris@0
|
123 * @return Boolean
|
Chris@0
|
124 */
|
Chris@0
|
125 public function hasField($locator)
|
Chris@0
|
126 {
|
Chris@0
|
127 return null !== $this->findField($locator);
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 /**
|
Chris@0
|
131 * Finds field (input, textarea, select) with specified locator.
|
Chris@0
|
132 *
|
Chris@0
|
133 * @param string $locator input id, name or label
|
Chris@0
|
134 *
|
Chris@0
|
135 * @return NodeElement|null
|
Chris@0
|
136 */
|
Chris@0
|
137 public function findField($locator)
|
Chris@0
|
138 {
|
Chris@0
|
139 return $this->find('named', array('field', $locator));
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * Fills in field (input, textarea, select) with specified locator.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @param string $locator input id, name or label
|
Chris@0
|
146 * @param string $value value
|
Chris@0
|
147 *
|
Chris@0
|
148 * @throws ElementNotFoundException
|
Chris@0
|
149 *
|
Chris@0
|
150 * @see NodeElement::setValue
|
Chris@0
|
151 */
|
Chris@0
|
152 public function fillField($locator, $value)
|
Chris@0
|
153 {
|
Chris@0
|
154 $field = $this->findField($locator);
|
Chris@0
|
155
|
Chris@0
|
156 if (null === $field) {
|
Chris@0
|
157 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value|placeholder', $locator);
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 $field->setValue($value);
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@0
|
164 * Checks whether element has a checkbox with specified locator, which is checked.
|
Chris@0
|
165 *
|
Chris@0
|
166 * @param string $locator input id, name or label
|
Chris@0
|
167 *
|
Chris@0
|
168 * @return Boolean
|
Chris@0
|
169 *
|
Chris@0
|
170 * @see NodeElement::isChecked
|
Chris@0
|
171 */
|
Chris@0
|
172 public function hasCheckedField($locator)
|
Chris@0
|
173 {
|
Chris@0
|
174 $field = $this->findField($locator);
|
Chris@0
|
175
|
Chris@0
|
176 return null !== $field && $field->isChecked();
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@0
|
180 * Checks whether element has a checkbox with specified locator, which is unchecked.
|
Chris@0
|
181 *
|
Chris@0
|
182 * @param string $locator input id, name or label
|
Chris@0
|
183 *
|
Chris@0
|
184 * @return Boolean
|
Chris@0
|
185 *
|
Chris@0
|
186 * @see NodeElement::isChecked
|
Chris@0
|
187 */
|
Chris@0
|
188 public function hasUncheckedField($locator)
|
Chris@0
|
189 {
|
Chris@0
|
190 $field = $this->findField($locator);
|
Chris@0
|
191
|
Chris@0
|
192 return null !== $field && !$field->isChecked();
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Checks checkbox with specified locator.
|
Chris@0
|
197 *
|
Chris@0
|
198 * @param string $locator input id, name or label
|
Chris@0
|
199 *
|
Chris@0
|
200 * @throws ElementNotFoundException
|
Chris@0
|
201 */
|
Chris@0
|
202 public function checkField($locator)
|
Chris@0
|
203 {
|
Chris@0
|
204 $field = $this->findField($locator);
|
Chris@0
|
205
|
Chris@0
|
206 if (null === $field) {
|
Chris@0
|
207 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 $field->check();
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * Unchecks checkbox with specified locator.
|
Chris@0
|
215 *
|
Chris@0
|
216 * @param string $locator input id, name or label
|
Chris@0
|
217 *
|
Chris@0
|
218 * @throws ElementNotFoundException
|
Chris@0
|
219 */
|
Chris@0
|
220 public function uncheckField($locator)
|
Chris@0
|
221 {
|
Chris@0
|
222 $field = $this->findField($locator);
|
Chris@0
|
223
|
Chris@0
|
224 if (null === $field) {
|
Chris@0
|
225 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 $field->uncheck();
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 /**
|
Chris@0
|
232 * Checks whether element has a select field with specified locator.
|
Chris@0
|
233 *
|
Chris@0
|
234 * @param string $locator select id, name or label
|
Chris@0
|
235 *
|
Chris@0
|
236 * @return Boolean
|
Chris@0
|
237 */
|
Chris@0
|
238 public function hasSelect($locator)
|
Chris@0
|
239 {
|
Chris@0
|
240 return $this->has('named', array('select', $locator));
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * Selects option from select field with specified locator.
|
Chris@0
|
245 *
|
Chris@0
|
246 * @param string $locator input id, name or label
|
Chris@0
|
247 * @param string $value option value
|
Chris@0
|
248 * @param Boolean $multiple select multiple options
|
Chris@0
|
249 *
|
Chris@0
|
250 * @throws ElementNotFoundException
|
Chris@0
|
251 *
|
Chris@0
|
252 * @see NodeElement::selectOption
|
Chris@0
|
253 */
|
Chris@0
|
254 public function selectFieldOption($locator, $value, $multiple = false)
|
Chris@0
|
255 {
|
Chris@0
|
256 $field = $this->findField($locator);
|
Chris@0
|
257
|
Chris@0
|
258 if (null === $field) {
|
Chris@0
|
259 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
|
Chris@0
|
260 }
|
Chris@0
|
261
|
Chris@0
|
262 $field->selectOption($value, $multiple);
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * Checks whether element has a table with specified locator.
|
Chris@0
|
267 *
|
Chris@0
|
268 * @param string $locator table id or caption
|
Chris@0
|
269 *
|
Chris@0
|
270 * @return Boolean
|
Chris@0
|
271 */
|
Chris@0
|
272 public function hasTable($locator)
|
Chris@0
|
273 {
|
Chris@0
|
274 return $this->has('named', array('table', $locator));
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 /**
|
Chris@0
|
278 * Attach file to file field with specified locator.
|
Chris@0
|
279 *
|
Chris@0
|
280 * @param string $locator input id, name or label
|
Chris@0
|
281 * @param string $path path to file
|
Chris@0
|
282 *
|
Chris@0
|
283 * @throws ElementNotFoundException
|
Chris@0
|
284 *
|
Chris@0
|
285 * @see NodeElement::attachFile
|
Chris@0
|
286 */
|
Chris@0
|
287 public function attachFileToField($locator, $path)
|
Chris@0
|
288 {
|
Chris@0
|
289 $field = $this->findField($locator);
|
Chris@0
|
290
|
Chris@0
|
291 if (null === $field) {
|
Chris@0
|
292 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 $field->attachFile($path);
|
Chris@0
|
296 }
|
Chris@0
|
297 }
|