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