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\Exception\ElementNotFoundException;
16 * Traversable element.
18 * @author Konstantin Kudryashov <ever.zet@gmail.com>
20 abstract class TraversableElement extends Element
23 * Finds element by its id.
25 * @param string $id element id
27 * @return NodeElement|null
29 public function findById($id)
31 return $this->find('named', array('id', $id));
35 * Checks whether element has a link with specified locator.
37 * @param string $locator link id, title, text or image alt
41 public function hasLink($locator)
43 return null !== $this->findLink($locator);
47 * Finds link with specified locator.
49 * @param string $locator link id, title, text or image alt
51 * @return NodeElement|null
53 public function findLink($locator)
55 return $this->find('named', array('link', $locator));
59 * Clicks link with specified locator.
61 * @param string $locator link id, title, text or image alt
63 * @throws ElementNotFoundException
65 public function clickLink($locator)
67 $link = $this->findLink($locator);
70 throw new ElementNotFoundException($this->getDriver(), 'link', 'id|title|alt|text', $locator);
77 * Checks whether element has a button (input[type=submit|image|button|reset], button) with specified locator.
79 * @param string $locator button id, value or alt
83 public function hasButton($locator)
85 return null !== $this->findButton($locator);
89 * Finds button (input[type=submit|image|button|reset], button) with specified locator.
91 * @param string $locator button id, value or alt
93 * @return NodeElement|null
95 public function findButton($locator)
97 return $this->find('named', array('button', $locator));
101 * Presses button (input[type=submit|image|button|reset], button) with specified locator.
103 * @param string $locator button id, value or alt
105 * @throws ElementNotFoundException
107 public function pressButton($locator)
109 $button = $this->findButton($locator);
111 if (null === $button) {
112 throw new ElementNotFoundException($this->getDriver(), 'button', 'id|name|title|alt|value', $locator);
119 * Checks whether element has a field (input, textarea, select) with specified locator.
121 * @param string $locator input id, name or label
125 public function hasField($locator)
127 return null !== $this->findField($locator);
131 * Finds field (input, textarea, select) with specified locator.
133 * @param string $locator input id, name or label
135 * @return NodeElement|null
137 public function findField($locator)
139 return $this->find('named', array('field', $locator));
143 * Fills in field (input, textarea, select) with specified locator.
145 * @param string $locator input id, name or label
146 * @param string $value value
148 * @throws ElementNotFoundException
150 * @see NodeElement::setValue
152 public function fillField($locator, $value)
154 $field = $this->findField($locator);
156 if (null === $field) {
157 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value|placeholder', $locator);
160 $field->setValue($value);
164 * Checks whether element has a checkbox with specified locator, which is checked.
166 * @param string $locator input id, name or label
170 * @see NodeElement::isChecked
172 public function hasCheckedField($locator)
174 $field = $this->findField($locator);
176 return null !== $field && $field->isChecked();
180 * Checks whether element has a checkbox with specified locator, which is unchecked.
182 * @param string $locator input id, name or label
186 * @see NodeElement::isChecked
188 public function hasUncheckedField($locator)
190 $field = $this->findField($locator);
192 return null !== $field && !$field->isChecked();
196 * Checks checkbox with specified locator.
198 * @param string $locator input id, name or label
200 * @throws ElementNotFoundException
202 public function checkField($locator)
204 $field = $this->findField($locator);
206 if (null === $field) {
207 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
214 * Unchecks checkbox with specified locator.
216 * @param string $locator input id, name or label
218 * @throws ElementNotFoundException
220 public function uncheckField($locator)
222 $field = $this->findField($locator);
224 if (null === $field) {
225 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
232 * Checks whether element has a select field with specified locator.
234 * @param string $locator select id, name or label
238 public function hasSelect($locator)
240 return $this->has('named', array('select', $locator));
244 * Selects option from select field with specified locator.
246 * @param string $locator input id, name or label
247 * @param string $value option value
248 * @param Boolean $multiple select multiple options
250 * @throws ElementNotFoundException
252 * @see NodeElement::selectOption
254 public function selectFieldOption($locator, $value, $multiple = false)
256 $field = $this->findField($locator);
258 if (null === $field) {
259 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
262 $field->selectOption($value, $multiple);
266 * Checks whether element has a table with specified locator.
268 * @param string $locator table id or caption
272 public function hasTable($locator)
274 return $this->has('named', array('table', $locator));
278 * Attach file to file field with specified locator.
280 * @param string $locator input id, name or label
281 * @param string $path path to file
283 * @throws ElementNotFoundException
285 * @see NodeElement::attachFile
287 public function attachFileToField($locator, $path)
289 $field = $this->findField($locator);
291 if (null === $field) {
292 throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
295 $field->attachFile($path);