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\Exception\ElementNotFoundException; Chris@0: Chris@0: /** Chris@0: * Traversable element. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: abstract class TraversableElement extends Element Chris@0: { Chris@0: /** Chris@0: * Finds element by its id. Chris@0: * Chris@0: * @param string $id element id Chris@0: * Chris@0: * @return NodeElement|null Chris@0: */ Chris@0: public function findById($id) Chris@0: { Chris@0: return $this->find('named', array('id', $id)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a link with specified locator. Chris@0: * Chris@0: * @param string $locator link id, title, text or image alt Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function hasLink($locator) Chris@0: { Chris@0: return null !== $this->findLink($locator); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds link with specified locator. Chris@0: * Chris@0: * @param string $locator link id, title, text or image alt Chris@0: * Chris@0: * @return NodeElement|null Chris@0: */ Chris@0: public function findLink($locator) Chris@0: { Chris@0: return $this->find('named', array('link', $locator)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clicks link with specified locator. Chris@0: * Chris@0: * @param string $locator link id, title, text or image alt Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: */ Chris@0: public function clickLink($locator) Chris@0: { Chris@0: $link = $this->findLink($locator); Chris@0: Chris@0: if (null === $link) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'link', 'id|title|alt|text', $locator); Chris@0: } Chris@0: Chris@0: $link->click(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a button (input[type=submit|image|button|reset], button) with specified locator. Chris@0: * Chris@0: * @param string $locator button id, value or alt Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function hasButton($locator) Chris@0: { Chris@0: return null !== $this->findButton($locator); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds button (input[type=submit|image|button|reset], button) with specified locator. Chris@0: * Chris@0: * @param string $locator button id, value or alt Chris@0: * Chris@0: * @return NodeElement|null Chris@0: */ Chris@0: public function findButton($locator) Chris@0: { Chris@0: return $this->find('named', array('button', $locator)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Presses button (input[type=submit|image|button|reset], button) with specified locator. Chris@0: * Chris@0: * @param string $locator button id, value or alt Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: */ Chris@0: public function pressButton($locator) Chris@0: { Chris@0: $button = $this->findButton($locator); Chris@0: Chris@0: if (null === $button) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'button', 'id|name|title|alt|value', $locator); Chris@0: } Chris@0: Chris@0: $button->press(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a field (input, textarea, select) with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function hasField($locator) Chris@0: { Chris@0: return null !== $this->findField($locator); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds field (input, textarea, select) with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * Chris@0: * @return NodeElement|null Chris@0: */ Chris@0: public function findField($locator) Chris@0: { Chris@0: return $this->find('named', array('field', $locator)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Fills in field (input, textarea, select) with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * @param string $value value Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: * Chris@0: * @see NodeElement::setValue Chris@0: */ Chris@0: public function fillField($locator, $value) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: if (null === $field) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value|placeholder', $locator); Chris@0: } Chris@0: Chris@0: $field->setValue($value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a checkbox with specified locator, which is checked. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * Chris@0: * @return Boolean Chris@0: * Chris@0: * @see NodeElement::isChecked Chris@0: */ Chris@0: public function hasCheckedField($locator) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: return null !== $field && $field->isChecked(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a checkbox with specified locator, which is unchecked. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * Chris@0: * @return Boolean Chris@0: * Chris@0: * @see NodeElement::isChecked Chris@0: */ Chris@0: public function hasUncheckedField($locator) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: return null !== $field && !$field->isChecked(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks checkbox with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: */ Chris@0: public function checkField($locator) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: if (null === $field) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator); Chris@0: } Chris@0: Chris@0: $field->check(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unchecks checkbox with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: */ Chris@0: public function uncheckField($locator) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: if (null === $field) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator); Chris@0: } Chris@0: Chris@0: $field->uncheck(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a select field with specified locator. Chris@0: * Chris@0: * @param string $locator select id, name or label Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function hasSelect($locator) Chris@0: { Chris@0: return $this->has('named', array('select', $locator)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Selects option from select field with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * @param string $value option value Chris@0: * @param Boolean $multiple select multiple options Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: * Chris@0: * @see NodeElement::selectOption Chris@0: */ Chris@0: public function selectFieldOption($locator, $value, $multiple = false) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: if (null === $field) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator); Chris@0: } Chris@0: Chris@0: $field->selectOption($value, $multiple); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether element has a table with specified locator. Chris@0: * Chris@0: * @param string $locator table id or caption Chris@0: * Chris@0: * @return Boolean Chris@0: */ Chris@0: public function hasTable($locator) Chris@0: { Chris@0: return $this->has('named', array('table', $locator)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attach file to file field with specified locator. Chris@0: * Chris@0: * @param string $locator input id, name or label Chris@0: * @param string $path path to file Chris@0: * Chris@0: * @throws ElementNotFoundException Chris@0: * Chris@0: * @see NodeElement::attachFile Chris@0: */ Chris@0: public function attachFileToField($locator, $path) Chris@0: { Chris@0: $field = $this->findField($locator); Chris@0: Chris@0: if (null === $field) { Chris@0: throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator); Chris@0: } Chris@0: Chris@0: $field->attachFile($path); Chris@0: } Chris@0: }