weekly on-sync release 2.6dev
[moodle.git] / lib / tests / behat / behat_general.php
CommitLineData
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
28require_once(__DIR__ . '/../../behat/behat_base.php');
29
ca4f33a7
DM
30use 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 */
46class 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
563514b1 85 /**
7daab401 86 * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
563514b1
DM
87 * @Given /^I accept the currently displayed dialog$/
88 */
89 public function accept_currently_displayed_alert_dialog() {
90 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
91 }
92
786ea937
DM
93 /**
94 * Clicks link with specified id|title|alt|text.
95 *
786ea937 96 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
1f9ffbdb 97 * @throws ElementNotFoundException Thrown by behat_base::find
40923977 98 * @param string $link
786ea937
DM
99 */
100 public function click_link($link) {
1f9ffbdb
DM
101
102 $linknode = $this->find_link($link);
103 $linknode->click();
786ea937
DM
104 }
105
106 /**
107 * Waits X seconds. Required after an action that requires data from an AJAX request.
108 *
109 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
110 * @param int $seconds
111 */
112 public function i_wait_seconds($seconds) {
113 $this->getSession()->wait($seconds * 1000, false);
114 }
115
116 /**
117 * Waits until the page is completely loaded. This step is auto-executed after every step.
118 *
119 * @Given /^I wait until the page is ready$/
120 */
121 public function wait_until_the_page_is_ready() {
122 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
123 }
124
125 /**
40923977 126 * Generic mouse over action. Mouse over a element of the specified type.
786ea937 127 *
40923977
DM
128 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
129 * @param string $element Element we look for
130 * @param string $selectortype The type of what we look for
786ea937 131 */
40923977 132 public function i_hover($element, $selectortype) {
1f9ffbdb 133
40923977
DM
134 // Gets the node based on the requested selector type and locator.
135 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
136 $node->mouseOver();
137 }
138
40923977
DM
139 /**
140 * Generic click action. Click on the element of the specified type.
141 *
142 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
143 * @param string $element Element we look for
144 * @param string $selectortype The type of what we look for
145 */
146 public function i_click_on($element, $selectortype) {
147
148 // Gets the node based on the requested selector type and locator.
149 $node = $this->get_selected_node($selectortype, $element);
150 $node->click();
151 }
152
072f67fc
DM
153 /**
154 * Click on the element of the specified type which is located inside the second element.
155 *
156 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
157 * @param string $element Element we look for
158 * @param string $selectortype The type of what we look for
159 * @param string $nodeelement Element we look in
160 * @param string $nodeselectortype The type of selector where we look in
161 */
162 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
163
164 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
165 $node->click();
166 }
167
f9d3667e
DM
168 /**
169 * Click on the specified element inside a table row containing the specified text.
170 *
171 * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<row_text_string>(?:[^"]|\\")*)" table row$/
172 * @throws ElementNotFoundException
173 * @param string $element Element we look for
174 * @param string $selectortype The type of what we look for
175 * @param string $tablerowtext The table row text
176 */
177 public function i_click_on_in_the_table_row($element, $selectortype, $tablerowtext) {
178
179 // The table row container.
180 $nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text ');
181 $tablerowtext = str_replace("'", "\'", $tablerowtext);
182 $rownode = $this->find('xpath', "//tr[contains(., '" . $tablerowtext . "')]", $nocontainerexception);
183
184 // Looking for the element DOM node inside the specified row.
185 list($selector, $locator) = $this->transform_selector($selectortype, $element);
186 $elementnode = $this->find($selector, $locator, false, $rownode);
187 $elementnode->click();
188 }
189
563514b1 190 /**
7daab401 191 * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
563514b1
DM
192 *
193 * The steps definitions calling this step as part of them should
194 * manage the wait times by themselves as the times and when the
195 * waits should be done depends on what is being dragged & dropper.
196 *
197 * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
198 * @param string $element
199 * @param string $selectortype
200 * @param string $containerelement
201 * @param string $containerselectortype
202 */
203 public function i_drag_and_i_drop_it_in($element, $selectortype, $containerelement, $containerselectortype) {
204
205 list($sourceselector, $sourcelocator) = $this->transform_selector($selectortype, $element);
206 $sourcexpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($sourceselector, $sourcelocator);
207
208 list($containerselector, $containerlocator) = $this->transform_selector($containerselectortype, $containerelement);
209 $destinationxpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($containerselector, $containerlocator);
210
211 $this->getSession()->getDriver()->dragTo($sourcexpath, $destinationxpath);
212 }
213
786ea937
DM
214 /**
215 * Checks, that page contains specified text.
216 *
217 * @see Behat\MinkExtension\Context\MinkContext
218 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
40923977 219 * @param string $text
786ea937
DM
220 */
221 public function assert_page_contains_text($text) {
02ca6219 222 $this->assertSession()->pageTextContains($text);
786ea937
DM
223 }
224
225 /**
226 * Checks, that page doesn't contain specified text.
227 *
228 * @see Behat\MinkExtension\Context\MinkContext
229 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
40923977 230 * @param string $text
786ea937
DM
231 */
232 public function assert_page_not_contains_text($text) {
02ca6219 233 $this->assertSession()->pageTextNotContains($text);
786ea937
DM
234 }
235
236 /**
40923977 237 * Checks, that element with specified CSS selector or XPath contains specified text.
786ea937 238 *
40923977
DM
239 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
240 * @param string $text
241 * @param string $element Element we look in.
242 * @param string $selectortype The type of element where we are looking in.
786ea937 243 */
40923977
DM
244 public function assert_element_contains_text($text, $element, $selectortype) {
245
246 // Transforming from steps definitions selector/locator format to Mink format.
072f67fc 247 list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
40923977 248 $this->assertSession()->elementTextContains($selector, $locator, $text);
786ea937
DM
249 }
250
251 /**
40923977 252 * Checks, that element with specified CSS selector or XPath doesn't contain specified text.
786ea937 253 *
40923977
DM
254 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
255 * @param string $text
256 * @param string $element Element we look in.
257 * @param string $selectortype The type of element where we are looking in.
786ea937 258 */
40923977
DM
259 public function assert_element_not_contains_text($text, $element, $selectortype) {
260
261 // Transforming from steps definitions selector/locator format to mink format.
072f67fc 262 list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
40923977 263 $this->assertSession()->elementTextNotContains($selector, $locator, $text);
786ea937
DM
264 }
265
60054942
DM
266 /**
267 * Checks, that the first specified element appears before the second one.
268 *
269 * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
270 * @throws ExpectationException
271 * @param string $preelement The locator of the preceding element
272 * @param string $preselectortype The locator of the preceding element
273 * @param string $postelement The locator of the latest element
274 * @param string $postselectortype The selector type of the latest element
275 */
276 public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
277
278 // We allow postselectortype as a non-text based selector.
279 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
280 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
281
282 $prexpath = $this->find($preselector, $prelocator)->getXpath();
283 $postxpath = $this->find($postselector, $postlocator)->getXpath();
284
285 // Using following xpath axe to find it.
286 $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
287 $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
288 if (!$this->getSession()->getDriver()->find($xpath)) {
289 throw new ExpectationException($msg, $this->getSession());
290 }
291 }
292
293 /**
294 * Checks, that the first specified element appears after the second one.
295 *
296 * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
297 * @throws ExpectationException
298 * @param string $postelement The locator of the latest element
299 * @param string $postselectortype The selector type of the latest element
300 * @param string $preelement The locator of the preceding element
301 * @param string $preselectortype The locator of the preceding element
302 */
303 public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
304
305 // We allow postselectortype as a non-text based selector.
306 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
307 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
308
309 $postxpath = $this->find($postselector, $postlocator)->getXpath();
310 $prexpath = $this->find($preselector, $prelocator)->getXpath();
311
312 // Using preceding xpath axe to find it.
313 $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
314 $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
315 if (!$this->getSession()->getDriver()->find($xpath)) {
316 throw new ExpectationException($msg, $this->getSession());
317 }
318 }
319
786ea937 320 /**
40923977 321 * Checks, that element of specified type is disabled.
786ea937 322 *
40923977 323 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 324 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
325 * @param string $element Element we look in
326 * @param string $selectortype The type of element where we are looking in.
786ea937 327 */
40923977 328 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 329
40923977
DM
330 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
331 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
332
333 if (!$node->hasAttribute('disabled')) {
334 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
335 }
336 }
337
338 /**
40923977 339 * Checks, that element of specified type is enabled.
786ea937 340 *
40923977 341 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 342 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
343 * @param string $element Element we look on
344 * @param string $selectortype The type of where we look
786ea937 345 */
40923977 346 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 347
40923977
DM
348 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
349 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
350
351 if ($node->hasAttribute('disabled')) {
352 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
353 }
354 }
355
ca4f33a7 356 /**
62eb5c46
EL
357 * Checks the provided element and selector type exists in the current page.
358 *
359 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
360 *
361 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
362 * @throws ElementNotFoundException Thrown by behat_base::find
363 * @param string $element The locator of the specified selector
364 * @param string $selectortype The selector type
365 */
366 public function should_exists($element, $selectortype) {
367
368 // Getting Mink selector and locator.
369 list($selector, $locator) = $this->transform_selector($selectortype, $element);
370
371 // Will throw an ElementNotFoundException if it does not exist.
372 $this->find($selector, $locator);
373 }
374
375 /**
62eb5c46
EL
376 * Checks that the provided element and selector type not exists in the current page.
377 *
378 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
379 *
380 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
381 * @throws ExpectationException
382 * @param string $element The locator of the specified selector
383 * @param string $selectortype The selector type
384 */
385 public function should_not_exists($element, $selectortype) {
386
387 try {
388 $this->should_exists($element, $selectortype);
389 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
62eb5c46 390 } catch (ElementNotFoundException $e) {
ca4f33a7
DM
391 // It passes.
392 return;
393 }
394 }
395
786ea937 396}