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\Session;
14 use Behat\Mink\Exception\ElementNotFoundException;
19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 class NodeElement extends TraversableElement
26 * Initializes node element.
28 * @param string $xpath element xpath
29 * @param Session $session session instance
31 public function __construct($xpath, Session $session)
33 $this->xpath = $xpath;
35 parent::__construct($session);
39 * Returns XPath for handled element.
43 public function getXpath()
49 * Returns parent element to the current one.
53 public function getParent()
55 return $this->find('xpath', '..');
59 * Returns current node tag name.
61 * The value is always returned in lowercase to allow an easy comparison.
65 public function getTagName()
67 return strtolower($this->getDriver()->getTagName($this->getXpath()));
71 * Returns the value of the form field or option element.
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).
85 * Calling this method on other elements than form fields or option elements is not allowed.
87 * @return string|bool|array
89 public function getValue()
91 return $this->getDriver()->getValue($this->getXpath());
95 * Sets the value of the form field.
97 * Calling this method on other elements than form fields is not allowed.
99 * @param string|bool|array $value
101 * @see NodeElement::getValue for the format of the value for each type of field
103 public function setValue($value)
105 $this->getDriver()->setValue($this->getXpath(), $value);
109 * Checks whether element has attribute with specified name.
111 * @param string $name
115 public function hasAttribute($name)
117 return null !== $this->getDriver()->getAttribute($this->getXpath(), $name);
121 * Returns specified attribute value.
123 * @param string $name
125 * @return string|null
127 public function getAttribute($name)
129 return $this->getDriver()->getAttribute($this->getXpath(), $name);
133 * Checks whether an element has a named CSS class.
135 * @param string $className Name of the class
139 public function hasClass($className)
141 if ($this->hasAttribute('class')) {
142 return in_array($className, preg_split('/\s+/', $this->getAttribute('class')));
149 * Clicks current node.
151 public function click()
153 $this->getDriver()->click($this->getXpath());
157 * Presses current button.
159 public function press()
165 * Double-clicks current node.
167 public function doubleClick()
169 $this->getDriver()->doubleClick($this->getXpath());
173 * Right-clicks current node.
175 public function rightClick()
177 $this->getDriver()->rightClick($this->getXpath());
181 * Checks current node if it's a checkbox field.
183 public function check()
185 $this->getDriver()->check($this->getXpath());
189 * Unchecks current node if it's a checkbox field.
191 public function uncheck()
193 $this->getDriver()->uncheck($this->getXpath());
197 * Checks whether current node is checked if it's a checkbox or radio field.
199 * Calling this method on any other elements is not allowed.
203 public function isChecked()
205 return (Boolean) $this->getDriver()->isChecked($this->getXpath());
209 * Selects specified option for select field or specified radio button in the group.
211 * If the current node is a select box, this selects the option found by its value or
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.
216 * Calling this method on any other elements is not allowed.
218 * @param string $option
219 * @param Boolean $multiple whether the option should be added to the selection for multiple selects
221 * @throws ElementNotFoundException when the option is not found in the select box
223 public function selectOption($option, $multiple = false)
225 if ('select' !== $this->getTagName()) {
226 $this->getDriver()->selectOption($this->getXpath(), $option, $multiple);
231 $opt = $this->find('named', array('option', $option));
234 throw new ElementNotFoundException($this->getDriver(), 'select option', 'value|text', $option);
237 $this->getDriver()->selectOption($this->getXpath(), $opt->getValue(), $multiple);
241 * Checks whether current node is selected if it's a option field.
243 * Calling this method on any other elements is not allowed.
247 public function isSelected()
249 return (Boolean) $this->getDriver()->isSelected($this->getXpath());
253 * Attach file to current node if it's a file input.
255 * Calling this method on any other elements than file input is not allowed.
257 * @param string $path path to file (local)
259 public function attachFile($path)
261 $this->getDriver()->attachFile($this->getXpath(), $path);
265 * Checks whether current node is visible on page.
269 public function isVisible()
271 return (Boolean) $this->getDriver()->isVisible($this->getXpath());
275 * Simulates a mouse over on the element.
277 public function mouseOver()
279 $this->getDriver()->mouseOver($this->getXpath());
283 * Drags current node onto other node.
285 * @param ElementInterface $destination other node
287 public function dragTo(ElementInterface $destination)
289 $this->getDriver()->dragTo($this->getXpath(), $destination->getXpath());
293 * Brings focus to element.
295 public function focus()
297 $this->getDriver()->focus($this->getXpath());
301 * Removes focus from element.
303 public function blur()
305 $this->getDriver()->blur($this->getXpath());
309 * Presses specific keyboard key.
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')
314 public function keyPress($char, $modifier = null)
316 $this->getDriver()->keyPress($this->getXpath(), $char, $modifier);
320 * Pressed down specific keyboard key.
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')
325 public function keyDown($char, $modifier = null)
327 $this->getDriver()->keyDown($this->getXpath(), $char, $modifier);
331 * Pressed up specific keyboard key.
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')
336 public function keyUp($char, $modifier = null)
338 $this->getDriver()->keyUp($this->getXpath(), $char, $modifier);
344 * Calling this method on anything else than form elements is not allowed.
346 public function submit()
348 $this->getDriver()->submitForm($this->getXpath());