Newer
Older
Angie Byron
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
* This file is part of the Mink package.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\Mink\Element;
use Behat\Mink\Exception\ElementNotFoundException;
/**
* Traversable element.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
abstract class TraversableElement extends Element
{
/**
* Finds element by its id.
*
* @param string $id element id
*
* @return NodeElement|null
*/
public function findById($id)
{
return $this->find('named', array('id', $id));
}
/**
* Checks whether element has a link with specified locator.
*
* @param string $locator link id, title, text or image alt
*
* @return Boolean
*/
public function hasLink($locator)
{
return null !== $this->findLink($locator);
}
/**
* Finds link with specified locator.
*
* @param string $locator link id, title, text or image alt
*
* @return NodeElement|null
*/
public function findLink($locator)
{
return $this->find('named', array('link', $locator));
Angie Byron
committed
}
/**
* Clicks link with specified locator.
*
* @param string $locator link id, title, text or image alt
*
* @throws ElementNotFoundException
*/
public function clickLink($locator)
{
$link = $this->findLink($locator);
if (null === $link) {
throw new ElementNotFoundException($this->getDriver(), 'link', 'id|title|alt|text', $locator);
Angie Byron
committed
}
$link->click();
}
/**
* Checks whether element has a button (input[type=submit|image|button|reset], button) with specified locator.
*
* @param string $locator button id, value or alt
*
* @return Boolean
*/
public function hasButton($locator)
{
return null !== $this->findButton($locator);
}
/**
* Finds button (input[type=submit|image|button|reset], button) with specified locator.
*
* @param string $locator button id, value or alt
*
* @return NodeElement|null
*/
public function findButton($locator)
{
return $this->find('named', array('button', $locator));
Angie Byron
committed
}
/**
* Presses button (input[type=submit|image|button|reset], button) with specified locator.
*
* @param string $locator button id, value or alt
*
* @throws ElementNotFoundException
*/
public function pressButton($locator)
{
$button = $this->findButton($locator);
if (null === $button) {
throw new ElementNotFoundException($this->getDriver(), 'button', 'id|name|title|alt|value', $locator);
Angie Byron
committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}
$button->press();
}
/**
* Checks whether element has a field (input, textarea, select) with specified locator.
*
* @param string $locator input id, name or label
*
* @return Boolean
*/
public function hasField($locator)
{
return null !== $this->findField($locator);
}
/**
* Finds field (input, textarea, select) with specified locator.
*
* @param string $locator input id, name or label
*
* @return NodeElement|null
*/
public function findField($locator)
{
return $this->find('named', array('field', $locator));
Angie Byron
committed
}
/**
* Fills in field (input, textarea, select) with specified locator.
*
* @param string $locator input id, name or label
* @param string $value value
*
* @throws ElementNotFoundException
*
* @see NodeElement::setValue
*/
public function fillField($locator, $value)
{
$field = $this->findField($locator);
if (null === $field) {
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value|placeholder', $locator);
Angie Byron
committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
}
$field->setValue($value);
}
/**
* Checks whether element has a checkbox with specified locator, which is checked.
*
* @param string $locator input id, name or label
*
* @return Boolean
*
* @see NodeElement::isChecked
*/
public function hasCheckedField($locator)
{
$field = $this->findField($locator);
return null !== $field && $field->isChecked();
}
/**
* Checks whether element has a checkbox with specified locator, which is unchecked.
*
* @param string $locator input id, name or label
*
* @return Boolean
*
* @see NodeElement::isChecked
*/
public function hasUncheckedField($locator)
{
$field = $this->findField($locator);
return null !== $field && !$field->isChecked();
}
/**
* Checks checkbox with specified locator.
*
* @param string $locator input id, name or label
*
* @throws ElementNotFoundException
*/
public function checkField($locator)
{
$field = $this->findField($locator);
if (null === $field) {
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
Angie Byron
committed
}
$field->check();
}
/**
* Unchecks checkbox with specified locator.
*
* @param string $locator input id, name or label
*
* @throws ElementNotFoundException
*/
public function uncheckField($locator)
{
$field = $this->findField($locator);
if (null === $field) {
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
Angie Byron
committed
}
$field->uncheck();
}
/**
* Checks whether element has a select field with specified locator.
*
* @param string $locator select id, name or label
*
* @return Boolean
*/
public function hasSelect($locator)
{
return $this->has('named', array('select', $locator));
Angie Byron
committed
}
/**
* Selects option from select field with specified locator.
*
* @param string $locator input id, name or label
* @param string $value option value
* @param Boolean $multiple select multiple options
*
* @throws ElementNotFoundException
*
* @see NodeElement::selectOption
*/
public function selectFieldOption($locator, $value, $multiple = false)
{
$field = $this->findField($locator);
if (null === $field) {
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
Angie Byron
committed
}
$field->selectOption($value, $multiple);
}
/**
* Checks whether element has a table with specified locator.
*
* @param string $locator table id or caption
*
* @return Boolean
*/
public function hasTable($locator)
{
return $this->has('named', array('table', $locator));
Angie Byron
committed
}
/**
* Attach file to file field with specified locator.
*
* @param string $locator input id, name or label
* @param string $path path to file
*
* @throws ElementNotFoundException
*
* @see NodeElement::attachFile
*/
public function attachFileToField($locator, $path)
{
$field = $this->findField($locator);
if (null === $field) {
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
Angie Byron
committed
}
$field->attachFile($path);
}
}