Version 1
[yaffs-website] / vendor / behat / mink / src / Element / NodeElement.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\Session;
14 use Behat\Mink\Exception\ElementNotFoundException;
15
16 /**
17  * Page element node.
18  *
19  * @author Konstantin Kudryashov <ever.zet@gmail.com>
20  */
21 class NodeElement extends TraversableElement
22 {
23     private $xpath;
24
25     /**
26      * Initializes node element.
27      *
28      * @param string  $xpath   element xpath
29      * @param Session $session session instance
30      */
31     public function __construct($xpath, Session $session)
32     {
33         $this->xpath = $xpath;
34
35         parent::__construct($session);
36     }
37
38     /**
39      * Returns XPath for handled element.
40      *
41      * @return string
42      */
43     public function getXpath()
44     {
45         return $this->xpath;
46     }
47
48     /**
49      * Returns parent element to the current one.
50      *
51      * @return NodeElement
52      */
53     public function getParent()
54     {
55         return $this->find('xpath', '..');
56     }
57
58     /**
59      * Returns current node tag name.
60      *
61      * The value is always returned in lowercase to allow an easy comparison.
62      *
63      * @return string
64      */
65     public function getTagName()
66     {
67         return strtolower($this->getDriver()->getTagName($this->getXpath()));
68     }
69
70     /**
71      * Returns the value of the form field or option element.
72      *
73      * For checkbox fields, the value is a boolean indicating whether the checkbox is checked.
74      * For radio buttons, the value is the value of the selected button in the radio group
75      *      or null if no button is selected.
76      * For single select boxes, the value is the value of the selected option.
77      * For multiple select boxes, the value is an array of selected option values.
78      * for file inputs, the return value is undefined given that browsers don't allow accessing
79      *      the value of file inputs for security reasons. Some drivers may allow accessing the
80      *      path of the file set in the field, but this is not required if it cannot be implemented.
81      * For textarea elements and all textual fields, the value is the content of the field.
82      * Form option elements, the value is the value of the option (the value attribute or the text
83      *      content if the attribute is not set).
84      *
85      * Calling this method on other elements than form fields or option elements is not allowed.
86      *
87      * @return string|bool|array
88      */
89     public function getValue()
90     {
91         return $this->getDriver()->getValue($this->getXpath());
92     }
93
94     /**
95      * Sets the value of the form field.
96      *
97      * Calling this method on other elements than form fields is not allowed.
98      *
99      * @param string|bool|array $value
100      *
101      * @see NodeElement::getValue for the format of the value for each type of field
102      */
103     public function setValue($value)
104     {
105         $this->getDriver()->setValue($this->getXpath(), $value);
106     }
107
108     /**
109      * Checks whether element has attribute with specified name.
110      *
111      * @param string $name
112      *
113      * @return Boolean
114      */
115     public function hasAttribute($name)
116     {
117         return null !== $this->getDriver()->getAttribute($this->getXpath(), $name);
118     }
119
120     /**
121      * Returns specified attribute value.
122      *
123      * @param string $name
124      *
125      * @return string|null
126      */
127     public function getAttribute($name)
128     {
129         return $this->getDriver()->getAttribute($this->getXpath(), $name);
130     }
131
132     /**
133      * Checks whether an element has a named CSS class.
134      *
135      * @param string $className Name of the class
136      *
137      * @return bool
138      */
139     public function hasClass($className)
140     {
141         if ($this->hasAttribute('class')) {
142             return in_array($className, preg_split('/\s+/', $this->getAttribute('class')));
143         }
144
145         return false;
146     }
147
148     /**
149      * Clicks current node.
150      */
151     public function click()
152     {
153         $this->getDriver()->click($this->getXpath());
154     }
155
156     /**
157      * Presses current button.
158      */
159     public function press()
160     {
161         $this->click();
162     }
163
164     /**
165      * Double-clicks current node.
166      */
167     public function doubleClick()
168     {
169         $this->getDriver()->doubleClick($this->getXpath());
170     }
171
172     /**
173      * Right-clicks current node.
174      */
175     public function rightClick()
176     {
177         $this->getDriver()->rightClick($this->getXpath());
178     }
179
180     /**
181      * Checks current node if it's a checkbox field.
182      */
183     public function check()
184     {
185         $this->getDriver()->check($this->getXpath());
186     }
187
188     /**
189      * Unchecks current node if it's a checkbox field.
190      */
191     public function uncheck()
192     {
193         $this->getDriver()->uncheck($this->getXpath());
194     }
195
196     /**
197      * Checks whether current node is checked if it's a checkbox or radio field.
198      *
199      * Calling this method on any other elements is not allowed.
200      *
201      * @return Boolean
202      */
203     public function isChecked()
204     {
205         return (Boolean) $this->getDriver()->isChecked($this->getXpath());
206     }
207
208     /**
209      * Selects specified option for select field or specified radio button in the group.
210      *
211      * If the current node is a select box, this selects the option found by its value or
212      * its text.
213      * If the current node is a radio button, this selects the radio button with the given
214      * value in the radio button group of the current node.
215      *
216      * Calling this method on any other elements is not allowed.
217      *
218      * @param string  $option
219      * @param Boolean $multiple whether the option should be added to the selection for multiple selects
220      *
221      * @throws ElementNotFoundException when the option is not found in the select box
222      */
223     public function selectOption($option, $multiple = false)
224     {
225         if ('select' !== $this->getTagName()) {
226             $this->getDriver()->selectOption($this->getXpath(), $option, $multiple);
227
228             return;
229         }
230
231         $opt = $this->find('named', array('option', $option));
232
233         if (null === $opt) {
234             throw new ElementNotFoundException($this->getDriver(), 'select option', 'value|text', $option);
235         }
236
237         $this->getDriver()->selectOption($this->getXpath(), $opt->getValue(), $multiple);
238     }
239
240     /**
241      * Checks whether current node is selected if it's a option field.
242      *
243      * Calling this method on any other elements is not allowed.
244      *
245      * @return Boolean
246      */
247     public function isSelected()
248     {
249         return (Boolean) $this->getDriver()->isSelected($this->getXpath());
250     }
251
252     /**
253      * Attach file to current node if it's a file input.
254      *
255      * Calling this method on any other elements than file input is not allowed.
256      *
257      * @param string $path path to file (local)
258      */
259     public function attachFile($path)
260     {
261         $this->getDriver()->attachFile($this->getXpath(), $path);
262     }
263
264     /**
265      * Checks whether current node is visible on page.
266      *
267      * @return Boolean
268      */
269     public function isVisible()
270     {
271         return (Boolean) $this->getDriver()->isVisible($this->getXpath());
272     }
273
274     /**
275      * Simulates a mouse over on the element.
276      */
277     public function mouseOver()
278     {
279         $this->getDriver()->mouseOver($this->getXpath());
280     }
281
282     /**
283      * Drags current node onto other node.
284      *
285      * @param ElementInterface $destination other node
286      */
287     public function dragTo(ElementInterface $destination)
288     {
289         $this->getDriver()->dragTo($this->getXpath(), $destination->getXpath());
290     }
291
292     /**
293      * Brings focus to element.
294      */
295     public function focus()
296     {
297         $this->getDriver()->focus($this->getXpath());
298     }
299
300     /**
301      * Removes focus from element.
302      */
303     public function blur()
304     {
305         $this->getDriver()->blur($this->getXpath());
306     }
307
308     /**
309      * Presses specific keyboard key.
310      *
311      * @param string|int $char     could be either char ('b') or char-code (98)
312      * @param string     $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
313      */
314     public function keyPress($char, $modifier = null)
315     {
316         $this->getDriver()->keyPress($this->getXpath(), $char, $modifier);
317     }
318
319     /**
320      * Pressed down specific keyboard key.
321      *
322      * @param string|int $char     could be either char ('b') or char-code (98)
323      * @param string     $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
324      */
325     public function keyDown($char, $modifier = null)
326     {
327         $this->getDriver()->keyDown($this->getXpath(), $char, $modifier);
328     }
329
330     /**
331      * Pressed up specific keyboard key.
332      *
333      * @param string|int $char     could be either char ('b') or char-code (98)
334      * @param string     $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
335      */
336     public function keyUp($char, $modifier = null)
337     {
338         $this->getDriver()->keyUp($this->getXpath(), $char, $modifier);
339     }
340
341     /**
342      * Submits the form.
343      *
344      * Calling this method on anything else than form elements is not allowed.
345      */
346     public function submit()
347     {
348         $this->getDriver()->submitForm($this->getXpath());
349     }
350 }