2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * General use steps definitions.
22 * @copyright 2012 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../behat/behat_base.php');
30 use Behat\Mink\Exception\ExpectationException as ExpectationException,
31 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
32 Behat\Mink\Exception\DriverException as DriverException,
33 WebDriver\Exception\NoSuchElement as NoSuchElement,
34 WebDriver\Exception\StaleElementReference as StaleElementReference;
37 * Cross component steps definitions.
39 * Basic web application definitions from MinkExtension and
40 * BehatchExtension. Definitions modified according to our needs
41 * when necessary and including only the ones we need to avoid
42 * overlapping and confusion.
46 * @copyright 2012 David Monllaó
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 class behat_general extends behat_base {
52 * Opens Moodle homepage.
54 * @Given /^I am on homepage$/
56 public function i_am_on_homepage() {
57 $this->getSession()->visit($this->locate_path('/'));
61 * Reloads the current page.
63 * @Given /^I reload the page$/
65 public function reload() {
66 $this->getSession()->reload();
70 * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection
72 * @Given /^I wait to be redirected$/
74 public function i_wait_to_be_redirected() {
76 // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and
77 // moodle_page::$periodicrefreshdelay possible values.
78 if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) {
79 // We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
83 // Wrapped in try & catch in case the redirection has already been executed.
85 $content = $metarefresh->getAttribute('content');
86 } catch (NoSuchElement $e) {
88 } catch (StaleElementReference $e) {
92 // Getting the refresh time and the url if present.
93 if (strstr($content, 'url') != false) {
95 list($waittime, $url) = explode(';', $content);
97 // Cleaning the URL value.
98 $url = trim(substr($url, strpos($url, 'http')));
102 $waittime = $content;
106 // Wait until the URL change is executed.
107 if ($this->running_javascript()) {
108 $this->getSession()->wait($waittime * 1000, false);
110 } else if (!empty($url)) {
111 // We redirect directly as we can not wait for an automatic redirection.
112 $this->getSession()->getDriver()->getClient()->request('get', $url);
115 // Reload the page if no URL was provided.
116 $this->getSession()->getDriver()->reload();
121 * Switches to the specified iframe.
123 * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" iframe$/
124 * @param string $iframename
126 public function switch_to_iframe($iframename) {
127 $this->getSession()->switchToIFrame($iframename);
131 * Switches to the main Moodle frame.
133 * @Given /^I switch to the main frame$/
135 public function switch_to_the_main_frame() {
136 $this->getSession()->switchToIFrame();
140 * Switches to the specified window. Useful when interacting with popup windows.
142 * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
143 * @param string $windowname
145 public function switch_to_window($windowname) {
146 $this->getSession()->switchToWindow($windowname);
150 * Switches to the main Moodle window. Useful when you finish interacting with popup windows.
152 * @Given /^I switch to the main window$/
154 public function switch_to_the_main_window() {
155 $this->getSession()->switchToWindow();
159 * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
160 * @Given /^I accept the currently displayed dialog$/
162 public function accept_currently_displayed_alert_dialog() {
163 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
167 * Clicks link with specified id|title|alt|text.
169 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
170 * @throws ElementNotFoundException Thrown by behat_base::find
171 * @param string $link
173 public function click_link($link) {
175 $linknode = $this->find_link($link);
180 * Waits X seconds. Required after an action that requires data from an AJAX request.
182 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
183 * @param int $seconds
185 public function i_wait_seconds($seconds) {
187 if (!$this->running_javascript()) {
188 throw new DriverException('Waits are disabled in scenarios without Javascript support');
191 $this->getSession()->wait($seconds * 1000, false);
195 * Waits until the page is completely loaded. This step is auto-executed after every step.
197 * @Given /^I wait until the page is ready$/
199 public function wait_until_the_page_is_ready() {
201 if (!$this->running_javascript()) {
202 throw new DriverException('Waits are disabled in scenarios without Javascript support');
205 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
209 * Generic mouse over action. Mouse over a element of the specified type.
211 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
212 * @param string $element Element we look for
213 * @param string $selectortype The type of what we look for
215 public function i_hover($element, $selectortype) {
217 // Gets the node based on the requested selector type and locator.
218 $node = $this->get_selected_node($selectortype, $element);
223 * Generic click action. Click on the element of the specified type.
225 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
226 * @param string $element Element we look for
227 * @param string $selectortype The type of what we look for
229 public function i_click_on($element, $selectortype) {
231 // Gets the node based on the requested selector type and locator.
232 $node = $this->get_selected_node($selectortype, $element);
237 * Click on the element of the specified type which is located inside the second element.
239 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
240 * @param string $element Element we look for
241 * @param string $selectortype The type of what we look for
242 * @param string $nodeelement Element we look in
243 * @param string $nodeselectortype The type of selector where we look in
245 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
247 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
252 * Click on the specified element inside a table row containing the specified text.
254 * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<row_text_string>(?:[^"]|\\")*)" table row$/
255 * @throws ElementNotFoundException
256 * @param string $element Element we look for
257 * @param string $selectortype The type of what we look for
258 * @param string $tablerowtext The table row text
260 public function i_click_on_in_the_table_row($element, $selectortype, $tablerowtext) {
262 // The table row container.
263 $nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text ');
264 $tablerowtext = $this->getSession()->getSelectorsHandler()->xpathLiteral($tablerowtext);
265 $rownode = $this->find('xpath', "//tr[contains(., $tablerowtext)]", $nocontainerexception);
267 // Looking for the element DOM node inside the specified row.
268 list($selector, $locator) = $this->transform_selector($selectortype, $element);
269 $elementnode = $this->find($selector, $locator, false, $rownode);
270 $elementnode->click();
274 * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
276 * The steps definitions calling this step as part of them should
277 * manage the wait times by themselves as the times and when the
278 * waits should be done depends on what is being dragged & dropper.
280 * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
281 * @param string $element
282 * @param string $selectortype
283 * @param string $containerelement
284 * @param string $containerselectortype
286 public function i_drag_and_i_drop_it_in($element, $selectortype, $containerelement, $containerselectortype) {
288 list($sourceselector, $sourcelocator) = $this->transform_selector($selectortype, $element);
289 $sourcexpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($sourceselector, $sourcelocator);
291 list($containerselector, $containerlocator) = $this->transform_selector($containerselectortype, $containerelement);
292 $destinationxpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($containerselector, $containerlocator);
294 $this->getSession()->getDriver()->dragTo($sourcexpath, $destinationxpath);
298 * Checks, that the specified element is visible. Only available in tests using Javascript.
300 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
301 * @throws ElementNotFoundException
302 * @throws ExpectationException
303 * @throws DriverException
304 * @param string $element
305 * @param string $selectortype
308 public function should_be_visible($element, $selectortype) {
310 if (!$this->running_javascript()) {
311 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
314 $node = $this->get_selected_node($selectortype, $element);
315 if (!$node->isVisible()) {
316 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
321 * Checks, that the specified element is not visible. Only available in tests using Javascript.
323 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
324 * @throws ElementNotFoundException
325 * @throws ExpectationException
326 * @param string $element
327 * @param string $selectortype
330 public function should_not_be_visible($element, $selectortype) {
333 $this->should_be_visible($element, $selectortype);
334 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
335 } catch (ExpectationException $e) {
341 * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
343 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
344 * @throws ElementNotFoundException
345 * @throws DriverException
346 * @throws ExpectationException
347 * @param string $element Element we look for
348 * @param string $selectortype The type of what we look for
349 * @param string $nodeelement Element we look in
350 * @param string $nodeselectortype The type of selector where we look in
352 public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
354 if (!$this->running_javascript()) {
355 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
358 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
359 if (!$node->isVisible()) {
360 throw new ExpectationException(
361 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
368 * Checks, that the specified element is not visible inside the specified container. Only available in tests using Javascript.
370 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
371 * @throws ElementNotFoundException
372 * @throws ExpectationException
373 * @param string $element Element we look for
374 * @param string $selectortype The type of what we look for
375 * @param string $nodeelement Element we look in
376 * @param string $nodeselectortype The type of selector where we look in
378 public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
381 $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
382 throw new ExpectationException(
383 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
386 } catch (ExpectationException $e) {
392 * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
394 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
395 * @throws ExpectationException
396 * @param string $text
398 public function assert_page_contains_text($text) {
400 // Looking for all the matching nodes without any other descendant matching the
401 // same xpath (we are using contains(., ....).
402 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
403 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
404 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
406 // Wait until it finds the text, otherwise custom exception.
408 $nodes = $this->find_all('xpath', $xpath);
410 // We also check for the element visibility when running JS tests.
411 if ($this->running_javascript()) {
412 foreach ($nodes as $node) {
413 if ($node->isVisible()) {
418 throw new ExpectationException("'{$text}' text was found but was not visible", $this->getSession());
421 } catch (ElementNotFoundException $e) {
422 throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
427 * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
429 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
430 * @throws ExpectationException
431 * @param string $text
433 public function assert_page_not_contains_text($text) {
435 // Delegating the process to assert_page_contains_text.
437 $this->assert_page_contains_text($text);
438 } catch (ExpectationException $e) {
439 // It should not appear, so this is good.
443 // If the page contains the text this is failing.
444 throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
448 * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
450 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
451 * @throws ElementNotFoundException
452 * @throws ExpectationException
453 * @param string $text
454 * @param string $element Element we look in.
455 * @param string $selectortype The type of element where we are looking in.
457 public function assert_element_contains_text($text, $element, $selectortype) {
459 // Getting the container where the text should be found.
460 $container = $this->get_selected_node($selectortype, $element);
462 // Looking for all the matching nodes without any other descendant matching the
463 // same xpath (we are using contains(., ....).
464 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
465 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
466 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
468 // Wait until it finds the text inside the container, otherwise custom exception.
470 $nodes = $this->find_all('xpath', $xpath, false, $container);
472 // We also check for the element visibility when running JS tests.
473 if ($this->running_javascript()) {
474 foreach ($nodes as $node) {
475 if ($node->isVisible()) {
480 throw new ExpectationException("'{$text}' text was found in the {$element} element but was not visible", $this->getSession());
483 } catch (ElementNotFoundException $e) {
484 throw new ExpectationException('"' . $text . '" text was not found in the ' . $element . ' element', $this->getSession());
490 * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
492 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
493 * @throws ElementNotFoundException
494 * @throws ExpectationException
495 * @param string $text
496 * @param string $element Element we look in.
497 * @param string $selectortype The type of element where we are looking in.
499 public function assert_element_not_contains_text($text, $element, $selectortype) {
501 // Delegating the process to assert_element_contains_text.
503 $this->assert_element_contains_text($text, $element, $selectortype);
504 } catch (ExpectationException $e) {
505 // It should not appear, so this is good.
506 // We only catch ExpectationException as ElementNotFoundException
507 // will be thrown if the container does not exist.
511 // If the element contains the text this is failing.
512 throw new ExpectationException('"' . $text . '" text was found in the ' . $element . ' element', $this->getSession());
516 * Checks, that the first specified element appears before the second one.
518 * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
519 * @throws ExpectationException
520 * @param string $preelement The locator of the preceding element
521 * @param string $preselectortype The locator of the preceding element
522 * @param string $postelement The locator of the latest element
523 * @param string $postselectortype The selector type of the latest element
525 public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
527 // We allow postselectortype as a non-text based selector.
528 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
529 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
531 $prexpath = $this->find($preselector, $prelocator)->getXpath();
532 $postxpath = $this->find($postselector, $postlocator)->getXpath();
534 // Using following xpath axe to find it.
535 $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
536 $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
537 if (!$this->getSession()->getDriver()->find($xpath)) {
538 throw new ExpectationException($msg, $this->getSession());
543 * Checks, that the first specified element appears after the second one.
545 * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
546 * @throws ExpectationException
547 * @param string $postelement The locator of the latest element
548 * @param string $postselectortype The selector type of the latest element
549 * @param string $preelement The locator of the preceding element
550 * @param string $preselectortype The locator of the preceding element
552 public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
554 // We allow postselectortype as a non-text based selector.
555 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
556 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
558 $postxpath = $this->find($postselector, $postlocator)->getXpath();
559 $prexpath = $this->find($preselector, $prelocator)->getXpath();
561 // Using preceding xpath axe to find it.
562 $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
563 $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
564 if (!$this->getSession()->getDriver()->find($xpath)) {
565 throw new ExpectationException($msg, $this->getSession());
570 * Checks, that element of specified type is disabled.
572 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
573 * @throws ExpectationException Thrown by behat_base::find
574 * @param string $element Element we look in
575 * @param string $selectortype The type of element where we are looking in.
577 public function the_element_should_be_disabled($element, $selectortype) {
579 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
580 $node = $this->get_selected_node($selectortype, $element);
582 if (!$node->hasAttribute('disabled')) {
583 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
588 * Checks, that element of specified type is enabled.
590 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
591 * @throws ExpectationException Thrown by behat_base::find
592 * @param string $element Element we look on
593 * @param string $selectortype The type of where we look
595 public function the_element_should_be_enabled($element, $selectortype) {
597 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
598 $node = $this->get_selected_node($selectortype, $element);
600 if ($node->hasAttribute('disabled')) {
601 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
606 * Checks the provided element and selector type exists in the current page.
608 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
610 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
611 * @throws ElementNotFoundException Thrown by behat_base::find
612 * @param string $element The locator of the specified selector
613 * @param string $selectortype The selector type
615 public function should_exists($element, $selectortype) {
617 // Getting Mink selector and locator.
618 list($selector, $locator) = $this->transform_selector($selectortype, $element);
620 // Will throw an ElementNotFoundException if it does not exist.
621 $this->find($selector, $locator);
625 * Checks that the provided element and selector type not exists in the current page.
627 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
629 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
630 * @throws ExpectationException
631 * @param string $element The locator of the specified selector
632 * @param string $selectortype The selector type
634 public function should_not_exists($element, $selectortype) {
637 $this->should_exists($element, $selectortype);
638 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
639 } catch (ElementNotFoundException $e) {
646 * This step triggers cron like a user would do going to admin/cron.php.
648 * @Given /^I trigger cron$/
650 public function i_trigger_cron() {
651 $this->getSession()->visit($this->locate_path('/admin/cron.php'));