1 | <?php |
||
11 | class MinkContext extends BaseMinkContext |
||
12 | { |
||
13 | /** |
||
14 | * @When /^(?:|I )(follow|press) the "(?P<name>[^"]*)" (?P<element>[^"]*)$/ |
||
15 | * @When /^(?:|I )(follow|press) the first "(?P<name>[^"]*)" (?P<element>[^"]*)$/ |
||
16 | * @When /^(?:|I )(follow|press) the (?P<nbr>\d*)(st|nd|rd|th) "(?P<name>[^"]*)" (?P<element>[^"]*)$/ |
||
17 | **/ |
||
18 | public function clickElement($name, $element, $nbr = 1, $filterCallback = null) |
||
22 | |||
23 | /** |
||
24 | * @When /^(?:|I )(?P<state>check|uncheck) the "(?P<name>[^"]*)" checkbox$/ |
||
25 | * @When /^(?:|I )(?P<state>check|uncheck) the first "(?P<name>[^"]*)" checkbox$/ |
||
26 | * @When /^(?:|I )(?P<state>check|uncheck) the (?P<nbr>\d*)(st|nd|rd|th) "(?P<name>[^"]*)" checkbox$/ |
||
27 | **/ |
||
28 | public function checkCheckbox($state, $name, $nbr = 1) |
||
29 | { |
||
30 | $this->elementAction( |
||
31 | $name, |
||
32 | 'field', |
||
33 | $nbr, |
||
34 | function ($e) use ($state) { if ('check' === $state) { $e->check(); } else { $e->uncheck(); } }, |
||
35 | function ($e) { return 'checkbox' === $e->getAttribute('type'); } |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @When /^(?:|I )check the "(?P<name>[^"]*)" radio$/ |
||
41 | * @When /^(?:|I )check the first "(?P<name>[^"]*)" radio$/ |
||
42 | * @When /^(?:|I )check the (?P<nbr>\d*)(st|nd|rd|th) "(?P<name>[^"]*)" radio$/ |
||
43 | **/ |
||
44 | public function checkRadio($name, $nbr = 1) |
||
45 | { |
||
46 | $this->elementAction( |
||
47 | $name, |
||
48 | 'field', |
||
49 | $nbr, |
||
50 | function ($e) { $this->getSession()->getDriver()->click($e->getXPath()); }, |
||
51 | function ($e) { return 'radio' === $e->getAttribute('type'); } |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * @Then /^(?:|I )should(?P<should>| not) see (?P<nbr>\d*) "(?P<name>[^"]*)" (?P<element>link|button|radio|checkbox)$/ |
||
58 | **/ |
||
59 | public function nbrElement($should, $nbr, $name, $element) |
||
78 | |||
79 | /** |
||
80 | * @Then /^(?:|I )should(?P<should>| not) see a "(?P<name>[^"]*)" (?P<element>link|button|radio|checkbox)$/ |
||
81 | **/ |
||
82 | public function seeElement($should, $name, $element) |
||
101 | |||
102 | /** |
||
103 | * @When /^(?:|I )(follow|press) the last "(?P<name>[^"]*)" (?P<element>[^"]*)$/ |
||
104 | **/ |
||
105 | public function clicklastElement($name, $element) |
||
106 | { |
||
107 | $this->clickElement($name, $element, -1); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @When /^(?:|I )follow the link containing "(?P<link>(?:[^"]|\\")*)"$/ |
||
112 | */ |
||
113 | public function clickLinkContaining($link) |
||
117 | |||
118 | public function clickLink($link) |
||
122 | |||
123 | /** |
||
124 | * @When /^(?:|I )fill in the first "(?P<field>(?:[^"]|\\")*)" field with "(?P<value>(?:[^"]|\\")*)"$/ |
||
125 | * @When /^(?:|I )fill in the (?P<nbr>\d*)(st|nd|rd|th) "(?P<field>(?:[^"]|\\")*)" field with "(?P<value>(?:[^"]|\\")*)"$/ |
||
126 | **/ |
||
127 | public function fillTheNthField($field, $value, $nbr = 1) |
||
128 | { |
||
129 | $field = $this->fixStepArgument($field); |
||
130 | $value = $this->fixStepArgument($value); |
||
131 | |||
132 | $this->elementAction( |
||
133 | $field, |
||
134 | 'field', |
||
135 | $nbr, |
||
136 | function ($e) use ($value) { $e->setValue($value); }, |
||
137 | function ($e) { |
||
138 | return in_array($e->getAttribute('type'), array( |
||
139 | 'text', 'password', 'color', 'date', 'datetime', |
||
140 | 'datetime-local', 'email', 'month', 'number', 'range', |
||
141 | 'search', 'tel', 'time', 'url', 'week', |
||
142 | )); |
||
143 | } |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | protected function searchElement($locator, $element, $filterCallback = null, TraversableElement $parent = null) |
||
148 | { |
||
149 | $parent = $parent ?: $this->getSession()->getPage(); |
||
150 | $locator = $this->fixStepArgument($locator); |
||
151 | |||
152 | $elements = $parent->findAll('named', array( |
||
153 | $element, $locator |
||
154 | )); |
||
155 | |||
156 | if (null !== $filterCallback && is_callable($filterCallback)) { |
||
157 | $elements = array_values(array_filter($elements, $filterCallback)); |
||
158 | } |
||
159 | |||
160 | return $elements; |
||
161 | } |
||
162 | |||
163 | protected function elementAction($locator, $element, $nbr = 1, $actionCallback, $filterCallback = null) |
||
164 | { |
||
165 | $elements = $this->searchElement($locator, $element, $filterCallback); |
||
166 | |||
167 | $nbr = is_numeric($nbr) ? intval($nbr) : $nbr; |
||
168 | $nbr = is_string($nbr) ? 1 : (-1 === $nbr ? count($elements) : $nbr); |
||
169 | |||
170 | if ($nbr > count($elements)) { |
||
171 | throw new ElementNotFoundException($this->getSession(), $element, null, $locator); |
||
172 | } |
||
173 | |||
174 | $e = $elements[$nbr - 1]; |
||
175 | |||
176 | $actionCallback($e); |
||
177 | } |
||
178 | |||
179 | protected function getAsserter() |
||
183 | } |
||
184 |
Let’s take a look at an example: