Commit | Line | Data |
---|---|---|
786ea937 DM |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * General use steps definitions. | |
19 | * | |
20 | * @package core | |
21 | * @category test | |
22 | * @copyright 2012 David Monllaó | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. | |
27 | ||
28 | require_once(__DIR__ . '/../../behat/behat_base.php'); | |
29 | ||
ca4f33a7 DM |
30 | use Behat\Mink\Exception\ExpectationException as ExpectationException, |
31 | Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; | |
786ea937 DM |
32 | |
33 | /** | |
34 | * Cross component steps definitions. | |
35 | * | |
36 | * Basic web application definitions from MinkExtension and | |
37 | * BehatchExtension. Definitions modified according to our needs | |
38 | * when necessary and including only the ones we need to avoid | |
39 | * overlapping and confusion. | |
40 | * | |
41 | * @package core | |
42 | * @category test | |
43 | * @copyright 2012 David Monllaó | |
44 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
45 | */ | |
46 | class behat_general extends behat_base { | |
47 | ||
48 | /** | |
49 | * Opens Moodle homepage. | |
50 | * | |
786ea937 DM |
51 | * @Given /^I am on homepage$/ |
52 | */ | |
53 | public function i_am_on_homepage() { | |
40923977 | 54 | $this->getSession()->visit($this->locate_path('/')); |
786ea937 DM |
55 | } |
56 | ||
18c84063 DM |
57 | /** |
58 | * Reloads the current page. | |
59 | * | |
60 | * @Given /^I reload the page$/ | |
61 | */ | |
62 | public function reload() { | |
63 | $this->getSession()->reload(); | |
64 | } | |
65 | ||
1303eb29 DM |
66 | /** |
67 | * Switches to the specified window. Useful when interacting with popup windows. | |
68 | * | |
69 | * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/ | |
70 | * @param string $windowname | |
71 | */ | |
72 | public function switch_to_window($windowname) { | |
73 | $this->getSession()->switchToWindow($windowname); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Switches to the main Moodle window. Useful when you finish interacting with popup windows. | |
78 | * | |
79 | * @Given /^I switch to the main window$/ | |
80 | */ | |
81 | public function switch_to_the_main_window() { | |
82 | $this->getSession()->switchToWindow(); | |
83 | } | |
84 | ||
786ea937 DM |
85 | /** |
86 | * Clicks link with specified id|title|alt|text. | |
87 | * | |
786ea937 | 88 | * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/ |
1f9ffbdb | 89 | * @throws ElementNotFoundException Thrown by behat_base::find |
40923977 | 90 | * @param string $link |
786ea937 DM |
91 | */ |
92 | public function click_link($link) { | |
1f9ffbdb DM |
93 | |
94 | $linknode = $this->find_link($link); | |
95 | $linknode->click(); | |
786ea937 DM |
96 | } |
97 | ||
98 | /** | |
99 | * Waits X seconds. Required after an action that requires data from an AJAX request. | |
100 | * | |
101 | * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/ | |
102 | * @param int $seconds | |
103 | */ | |
104 | public function i_wait_seconds($seconds) { | |
105 | $this->getSession()->wait($seconds * 1000, false); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Waits until the page is completely loaded. This step is auto-executed after every step. | |
110 | * | |
111 | * @Given /^I wait until the page is ready$/ | |
112 | */ | |
113 | public function wait_until_the_page_is_ready() { | |
114 | $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")'); | |
115 | } | |
116 | ||
117 | /** | |
40923977 | 118 | * Generic mouse over action. Mouse over a element of the specified type. |
786ea937 | 119 | * |
40923977 DM |
120 | * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/ |
121 | * @param string $element Element we look for | |
122 | * @param string $selectortype The type of what we look for | |
786ea937 | 123 | */ |
40923977 | 124 | public function i_hover($element, $selectortype) { |
1f9ffbdb | 125 | |
40923977 DM |
126 | // Gets the node based on the requested selector type and locator. |
127 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
128 | $node->mouseOver(); |
129 | } | |
130 | ||
40923977 DM |
131 | /** |
132 | * Generic click action. Click on the element of the specified type. | |
133 | * | |
134 | * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/ | |
135 | * @param string $element Element we look for | |
136 | * @param string $selectortype The type of what we look for | |
137 | */ | |
138 | public function i_click_on($element, $selectortype) { | |
139 | ||
140 | // Gets the node based on the requested selector type and locator. | |
141 | $node = $this->get_selected_node($selectortype, $element); | |
142 | $node->click(); | |
143 | } | |
144 | ||
072f67fc DM |
145 | /** |
146 | * Click on the element of the specified type which is located inside the second element. | |
147 | * | |
148 | * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ | |
149 | * @param string $element Element we look for | |
150 | * @param string $selectortype The type of what we look for | |
151 | * @param string $nodeelement Element we look in | |
152 | * @param string $nodeselectortype The type of selector where we look in | |
153 | */ | |
154 | public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) { | |
155 | ||
156 | $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement); | |
157 | $node->click(); | |
158 | } | |
159 | ||
f9d3667e DM |
160 | /** |
161 | * Click on the specified element inside a table row containing the specified text. | |
162 | * | |
163 | * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<row_text_string>(?:[^"]|\\")*)" table row$/ | |
164 | * @throws ElementNotFoundException | |
165 | * @param string $element Element we look for | |
166 | * @param string $selectortype The type of what we look for | |
167 | * @param string $tablerowtext The table row text | |
168 | */ | |
169 | public function i_click_on_in_the_table_row($element, $selectortype, $tablerowtext) { | |
170 | ||
171 | // The table row container. | |
172 | $nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text '); | |
173 | $tablerowtext = str_replace("'", "\'", $tablerowtext); | |
174 | $rownode = $this->find('xpath', "//tr[contains(., '" . $tablerowtext . "')]", $nocontainerexception); | |
175 | ||
176 | // Looking for the element DOM node inside the specified row. | |
177 | list($selector, $locator) = $this->transform_selector($selectortype, $element); | |
178 | $elementnode = $this->find($selector, $locator, false, $rownode); | |
179 | $elementnode->click(); | |
180 | } | |
181 | ||
786ea937 DM |
182 | /** |
183 | * Checks, that page contains specified text. | |
184 | * | |
185 | * @see Behat\MinkExtension\Context\MinkContext | |
186 | * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/ | |
40923977 | 187 | * @param string $text |
786ea937 DM |
188 | */ |
189 | public function assert_page_contains_text($text) { | |
02ca6219 | 190 | $this->assertSession()->pageTextContains($text); |
786ea937 DM |
191 | } |
192 | ||
193 | /** | |
194 | * Checks, that page doesn't contain specified text. | |
195 | * | |
196 | * @see Behat\MinkExtension\Context\MinkContext | |
197 | * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/ | |
40923977 | 198 | * @param string $text |
786ea937 DM |
199 | */ |
200 | public function assert_page_not_contains_text($text) { | |
02ca6219 | 201 | $this->assertSession()->pageTextNotContains($text); |
786ea937 DM |
202 | } |
203 | ||
204 | /** | |
40923977 | 205 | * Checks, that element with specified CSS selector or XPath contains specified text. |
786ea937 | 206 | * |
40923977 DM |
207 | * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ |
208 | * @param string $text | |
209 | * @param string $element Element we look in. | |
210 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 211 | */ |
40923977 DM |
212 | public function assert_element_contains_text($text, $element, $selectortype) { |
213 | ||
214 | // Transforming from steps definitions selector/locator format to Mink format. | |
072f67fc | 215 | list($selector, $locator) = $this->transform_text_selector($selectortype, $element); |
40923977 | 216 | $this->assertSession()->elementTextContains($selector, $locator, $text); |
786ea937 DM |
217 | } |
218 | ||
219 | /** | |
40923977 | 220 | * Checks, that element with specified CSS selector or XPath doesn't contain specified text. |
786ea937 | 221 | * |
40923977 DM |
222 | * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ |
223 | * @param string $text | |
224 | * @param string $element Element we look in. | |
225 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 226 | */ |
40923977 DM |
227 | public function assert_element_not_contains_text($text, $element, $selectortype) { |
228 | ||
229 | // Transforming from steps definitions selector/locator format to mink format. | |
072f67fc | 230 | list($selector, $locator) = $this->transform_text_selector($selectortype, $element); |
40923977 | 231 | $this->assertSession()->elementTextNotContains($selector, $locator, $text); |
786ea937 DM |
232 | } |
233 | ||
60054942 DM |
234 | /** |
235 | * Checks, that the first specified element appears before the second one. | |
236 | * | |
237 | * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
238 | * @throws ExpectationException | |
239 | * @param string $preelement The locator of the preceding element | |
240 | * @param string $preselectortype The locator of the preceding element | |
241 | * @param string $postelement The locator of the latest element | |
242 | * @param string $postselectortype The selector type of the latest element | |
243 | */ | |
244 | public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) { | |
245 | ||
246 | // We allow postselectortype as a non-text based selector. | |
247 | list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement); | |
248 | list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement); | |
249 | ||
250 | $prexpath = $this->find($preselector, $prelocator)->getXpath(); | |
251 | $postxpath = $this->find($postselector, $postlocator)->getXpath(); | |
252 | ||
253 | // Using following xpath axe to find it. | |
254 | $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"'; | |
255 | $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]'; | |
256 | if (!$this->getSession()->getDriver()->find($xpath)) { | |
257 | throw new ExpectationException($msg, $this->getSession()); | |
258 | } | |
259 | } | |
260 | ||
261 | /** | |
262 | * Checks, that the first specified element appears after the second one. | |
263 | * | |
264 | * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
265 | * @throws ExpectationException | |
266 | * @param string $postelement The locator of the latest element | |
267 | * @param string $postselectortype The selector type of the latest element | |
268 | * @param string $preelement The locator of the preceding element | |
269 | * @param string $preselectortype The locator of the preceding element | |
270 | */ | |
271 | public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) { | |
272 | ||
273 | // We allow postselectortype as a non-text based selector. | |
274 | list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement); | |
275 | list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement); | |
276 | ||
277 | $postxpath = $this->find($postselector, $postlocator)->getXpath(); | |
278 | $prexpath = $this->find($preselector, $prelocator)->getXpath(); | |
279 | ||
280 | // Using preceding xpath axe to find it. | |
281 | $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"'; | |
282 | $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]'; | |
283 | if (!$this->getSession()->getDriver()->find($xpath)) { | |
284 | throw new ExpectationException($msg, $this->getSession()); | |
285 | } | |
286 | } | |
287 | ||
786ea937 | 288 | /** |
40923977 | 289 | * Checks, that element of specified type is disabled. |
786ea937 | 290 | * |
40923977 | 291 | * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/ |
1f9ffbdb | 292 | * @throws ExpectationException Thrown by behat_base::find |
40923977 DM |
293 | * @param string $element Element we look in |
294 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 295 | */ |
40923977 | 296 | public function the_element_should_be_disabled($element, $selectortype) { |
786ea937 | 297 | |
40923977 DM |
298 | // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement. |
299 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
300 | |
301 | if (!$node->hasAttribute('disabled')) { | |
302 | throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession()); | |
303 | } | |
304 | } | |
305 | ||
306 | /** | |
40923977 | 307 | * Checks, that element of specified type is enabled. |
786ea937 | 308 | * |
40923977 | 309 | * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/ |
1f9ffbdb | 310 | * @throws ExpectationException Thrown by behat_base::find |
40923977 DM |
311 | * @param string $element Element we look on |
312 | * @param string $selectortype The type of where we look | |
786ea937 | 313 | */ |
40923977 | 314 | public function the_element_should_be_enabled($element, $selectortype) { |
1f9ffbdb | 315 | |
40923977 DM |
316 | // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement. |
317 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
318 | |
319 | if ($node->hasAttribute('disabled')) { | |
320 | throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession()); | |
321 | } | |
322 | } | |
323 | ||
ca4f33a7 | 324 | /** |
62eb5c46 EL |
325 | * Checks the provided element and selector type exists in the current page. |
326 | * | |
327 | * This step is for advanced users, use it if you don't find anything else suitable for what you need. | |
ca4f33a7 DM |
328 | * |
329 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/ | |
330 | * @throws ElementNotFoundException Thrown by behat_base::find | |
331 | * @param string $element The locator of the specified selector | |
332 | * @param string $selectortype The selector type | |
333 | */ | |
334 | public function should_exists($element, $selectortype) { | |
335 | ||
336 | // Getting Mink selector and locator. | |
337 | list($selector, $locator) = $this->transform_selector($selectortype, $element); | |
338 | ||
339 | // Will throw an ElementNotFoundException if it does not exist. | |
340 | $this->find($selector, $locator); | |
341 | } | |
342 | ||
343 | /** | |
62eb5c46 EL |
344 | * Checks that the provided element and selector type not exists in the current page. |
345 | * | |
346 | * This step is for advanced users, use it if you don't find anything else suitable for what you need. | |
ca4f33a7 DM |
347 | * |
348 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/ | |
349 | * @throws ExpectationException | |
350 | * @param string $element The locator of the specified selector | |
351 | * @param string $selectortype The selector type | |
352 | */ | |
353 | public function should_not_exists($element, $selectortype) { | |
354 | ||
355 | try { | |
356 | $this->should_exists($element, $selectortype); | |
357 | throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession()); | |
62eb5c46 | 358 | } catch (ElementNotFoundException $e) { |
ca4f33a7 DM |
359 | // It passes. |
360 | return; | |
361 | } | |
362 | } | |
363 | ||
786ea937 | 364 | } |