Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Behat\Mink\Element; Chris@0: Chris@0: use Behat\Mink\Session; Chris@0: use Behat\Mink\Exception\ElementNotFoundException; Chris@0: Chris@0: /** Chris@0: * Page element node. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class NodeElement extends TraversableElement Chris@0: { Chris@0: private $xpath; Chris@0: Chris@0: /** Chris@0: * Initializes node element. Chris@0: * Chris@0: * @param string $xpath element xpath Chris@0: * @param Session $session session instance Chris@0: */ Chris@0: public function __construct($xpath, Session $session) Chris@0: { Chris@0: $this->xpath = $xpath; Chris@0: Chris@0: parent::__construct($session); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns XPath for handled element. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getXpath() Chris@0: { Chris@0: return $this->xpath; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns parent element to the current one. Chris@0: * Chris@0: * @return NodeElement Chris@0: */ Chris@0: public function getParent() Chris@0: { Chris@0: return $this->find('xpath', '..'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns current node tag name. Chris@0: * Chris@0: * The value is always returned in lowercase to allow an easy comparison. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getTagName() Chris@0: { Chris@0: return strtolower($this->getDriver()->getTagName($this->getXpath())); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the value of the form field or option element. Chris@0: * Chris@0: * For checkbox fields, the value is a boolean indicating whether the checkbox is checked. Chris@0: * For radio buttons, the value is the value of the selected button in the radio group Chris@0: * or null if no button is selected. Chris@0: * For single select boxes, the value is the value of the selected option. Chris@0: * For multiple select boxes, the value is an array of selected option values. Chris@0: * for file inputs, the return value is undefined given that browsers don't allow accessing Chris@0: * the value of file inputs for security reasons. Some drivers may allow accessing the Chris@0: * path of the file set in the field, but this is not required if it cannot be implemented. Chris@0: * For textarea elements and all textual fields, the value is the content of the field. Chris@0: * Form option elements, the value is the value of the option (the value attribute or the text Chris@0: * content if the attribute is not set). Chris@0: * Chris@0: * Calling this method on other elements than form fields or option elements is not allowed. Chris@0: * Chris@0: * @return string|bool|array Chris@0: */ Chris@0: public function getValue() Chris@0: { Chris@0: return $this->getDriver()->getValue($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value of the form field. Chris@0: * Chris@0: * Calling this method on other elements than form fields is not allowed. Chris@0: * Chris@0: * @param string|bool|array $value Chris@0: * Chris@0: * @see NodeElement::getValue for the format of the value for each type of field Chris@0: */ Chris@0: public function setValue($value) Chris@0: { Chris@0: $this->getDriver()->setValue($this->getXpath(), $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has attribute with specified name. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function hasAttribute($name) Chris@0: { Chris@0: return null !== $this->getDriver()->getAttribute($this->getXpath(), $name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns specified attribute value. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: public function getAttribute($name) Chris@0: { Chris@0: return $this->getDriver()->getAttribute($this->getXpath(), $name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether an element has a named CSS class. Chris@0: * Chris@0: * @param string $className Name of the class Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function hasClass($className) Chris@0: { Chris@0: if ($this->hasAttribute('class')) { Chris@0: return in_array($className, preg_split('/\s+/', $this->getAttribute('class'))); Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clicks current node. Chris@0: */ Chris@0: public function click() Chris@0: { Chris@0: $this->getDriver()->click($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Presses current button. Chris@0: */ Chris@0: public function press() Chris@0: { Chris@0: $this->click(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Double-clicks current node. Chris@0: */ Chris@0: public function doubleClick() Chris@0: { Chris@0: $this->getDriver()->doubleClick($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Right-clicks current node. Chris@0: */ Chris@0: public function rightClick() Chris@0: { Chris@0: $this->getDriver()->rightClick($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks current node if it's a checkbox field. Chris@0: */ Chris@0: public function check() Chris@0: { Chris@0: $this->getDriver()->check($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unchecks current node if it's a checkbox field. Chris@0: */ Chris@0: public function uncheck() Chris@0: { Chris@0: $this->getDriver()->uncheck($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether current node is checked if it's a checkbox or radio field. Chris@0: * Chris@0: * Calling this method on any other elements is not allowed. Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function isChecked() Chris@0: { Chris@0: return (Boolean) $this->getDriver()->isChecked($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Selects specified option for select field or specified radio button in the group. Chris@0: * Chris@0: * If the current node is a select box, this selects the option found by its value or Chris@0: * its text. Chris@0: * If the current node is a radio button, this selects the radio button with the given Chris@0: * value in the radio button group of the current node. Chris@0: * Chris@0: * Calling this method on any other elements is not allowed. Chris@0: * Chris@0: * @param string $option Chris@0: * @param Boolean $multiple whether the option should be added to the selection for multiple selects Chris@0: * Chris@0: * @throws ElementNotFoundException when the option is not found in the select box Chris@0: */ Chris@0: public function selectOption($option, $multiple = false) Chris@0: { Chris@0: if ('select' !== $this->getTagName()) { Chris@0: $this->getDriver()->selectOption($this->getXpath(), $option, $multiple); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: $opt = $this->find('named', array('option', $option)); Chris@0: Chris@0: if (null === $opt) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'select option', 'value|text', $option); Chris@0: } Chris@0: Chris@0: $this->getDriver()->selectOption($this->getXpath(), $opt->getValue(), $multiple); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether current node is selected if it's a option field. Chris@0: * Chris@0: * Calling this method on any other elements is not allowed. Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function isSelected() Chris@0: { Chris@0: return (Boolean) $this->getDriver()->isSelected($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attach file to current node if it's a file input. Chris@0: * Chris@0: * Calling this method on any other elements than file input is not allowed. Chris@0: * Chris@0: * @param string $path path to file (local) Chris@0: */ Chris@0: public function attachFile($path) Chris@0: { Chris@0: $this->getDriver()->attachFile($this->getXpath(), $path); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether current node is visible on page. Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function isVisible() Chris@0: { Chris@0: return (Boolean) $this->getDriver()->isVisible($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Simulates a mouse over on the element. Chris@0: */ Chris@0: public function mouseOver() Chris@0: { Chris@0: $this->getDriver()->mouseOver($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Drags current node onto other node. Chris@0: * Chris@0: * @param ElementInterface $destination other node Chris@0: */ Chris@0: public function dragTo(ElementInterface $destination) Chris@0: { Chris@0: $this->getDriver()->dragTo($this->getXpath(), $destination->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Brings focus to element. Chris@0: */ Chris@0: public function focus() Chris@0: { Chris@0: $this->getDriver()->focus($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes focus from element. Chris@0: */ Chris@0: public function blur() Chris@0: { Chris@0: $this->getDriver()->blur($this->getXpath()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Presses specific keyboard key. Chris@0: * Chris@0: * @param string|int $char could be either char ('b') or char-code (98) Chris@0: * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta') Chris@0: */ Chris@0: public function keyPress($char, $modifier = null) Chris@0: { Chris@0: $this->getDriver()->keyPress($this->getXpath(), $char, $modifier); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pressed down specific keyboard key. Chris@0: * Chris@0: * @param string|int $char could be either char ('b') or char-code (98) Chris@0: * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta') Chris@0: */ Chris@0: public function keyDown($char, $modifier = null) Chris@0: { Chris@0: $this->getDriver()->keyDown($this->getXpath(), $char, $modifier); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pressed up specific keyboard key. Chris@0: * Chris@0: * @param string|int $char could be either char ('b') or char-code (98) Chris@0: * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta') Chris@0: */ Chris@0: public function keyUp($char, $modifier = null) Chris@0: { Chris@0: $this->getDriver()->keyUp($this->getXpath(), $char, $modifier); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Submits the form. Chris@0: * Chris@0: * Calling this method on anything else than form elements is not allowed. Chris@0: */ Chris@0: public function submit() Chris@0: { Chris@0: $this->getDriver()->submitForm($this->getXpath()); Chris@0: } Chris@0: }