Merge branch 'MDL-76207-39' of https://github.com/andrewnicols/moodle into MOODLE_39_...
[moodle.git] / lib / tests / behat / behat_general.php
blob8d89224db91c840560ed8e911d90bf7f820ec344
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.
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/>.
17 /**
18 * General use steps definitions.
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
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\Gherkin\Node\TableNode;
31 use Behat\Mink\Exception\DriverException;
32 use Behat\Mink\Exception\ElementNotFoundException;
33 use Behat\Mink\Exception\ExpectationException;
34 use Facebook\WebDriver\Exception\NoSuchElementException;
35 use Facebook\WebDriver\Exception\StaleElementReferenceException;
37 /**
38 * Cross component steps definitions.
40 * Basic web application definitions from MinkExtension and
41 * BehatchExtension. Definitions modified according to our needs
42 * when necessary and including only the ones we need to avoid
43 * overlapping and confusion.
45 * @package core
46 * @category test
47 * @copyright 2012 David MonllaĆ³
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50 class behat_general extends behat_base {
52 /**
53 * @var string used by {@link switch_to_window()} and
54 * {@link switch_to_the_main_window()} to work-around a Chrome browser issue.
56 const MAIN_WINDOW_NAME = '__moodle_behat_main_window_name';
58 /**
59 * @var string when we want to check whether or not a new page has loaded,
60 * we first write this unique string into the page. Then later, by checking
61 * whether it is still there, we can tell if a new page has been loaded.
63 const PAGE_LOAD_DETECTION_STRING = 'new_page_not_loaded_since_behat_started_watching';
65 /**
66 * @var $pageloaddetectionrunning boolean Used to ensure that page load detection was started before a page reload
67 * was checked for.
69 private $pageloaddetectionrunning = false;
71 /**
72 * Opens Moodle homepage.
74 * @Given /^I am on homepage$/
76 public function i_am_on_homepage() {
77 $this->execute('behat_general::i_visit', ['/']);
80 /**
81 * Opens Moodle site homepage.
83 * @Given /^I am on site homepage$/
85 public function i_am_on_site_homepage() {
86 $this->execute('behat_general::i_visit', ['/?redirect=0']);
89 /**
90 * Opens course index page.
92 * @Given /^I am on course index$/
94 public function i_am_on_course_index() {
95 $this->execute('behat_general::i_visit', ['/course/index.php']);
98 /**
99 * Reloads the current page.
101 * @Given /^I reload the page$/
103 public function reload() {
104 $this->getSession()->reload();
108 * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection
110 * @Given /^I wait to be redirected$/
112 public function i_wait_to_be_redirected() {
114 // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and
115 // moodle_page::$periodicrefreshdelay possible values.
116 if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) {
117 // We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
118 return true;
121 // Wrapped in try & catch in case the redirection has already been executed.
122 try {
123 $content = $metarefresh->getAttribute('content');
124 } catch (NoSuchElementException $e) {
125 return true;
126 } catch (StaleElementReferenceException $e) {
127 return true;
130 // Getting the refresh time and the url if present.
131 if (strstr($content, 'url') != false) {
133 list($waittime, $url) = explode(';', $content);
135 // Cleaning the URL value.
136 $url = trim(substr($url, strpos($url, 'http')));
138 } else {
139 // Just wait then.
140 $waittime = $content;
144 // Wait until the URL change is executed.
145 if ($this->running_javascript()) {
146 $this->getSession()->wait($waittime * 1000);
148 } else if (!empty($url)) {
149 // We redirect directly as we can not wait for an automatic redirection.
150 $this->getSession()->getDriver()->getClient()->request('get', $url);
152 } else {
153 // Reload the page if no URL was provided.
154 $this->getSession()->getDriver()->reload();
159 * Switches to the specified iframe.
161 * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" iframe$/
162 * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" class iframe$/
163 * @param string $name The name of the iframe
165 public function switch_to_iframe($name) {
166 // We spin to give time to the iframe to be loaded.
167 // Using extended timeout as we don't know about which
168 // kind of iframe will be loaded.
169 $this->spin(
170 function($context) use ($name){
171 $iframe = $context->find('iframe', $name);
172 if ($iframe->hasAttribute('name')) {
173 $iframename = $iframe->getAttribute('name');
174 } else {
175 if (!$this->running_javascript()) {
176 throw new \coding_exception('iframe must have a name attribute to use the switchTo command.');
178 $iframename = uniqid();
179 $this->execute_js_on_node($iframe, "{{ELEMENT}}.name = '{$iframename}';");
181 $context->getSession()->switchToIFrame($iframename);
183 // If no exception we are done.
184 return true;
186 behat_base::get_extended_timeout()
191 * Switches to a second window.
193 * @Given /^I switch to a second window$/
194 * @throws DriverException If there aren't exactly 2 windows open.
196 public function switch_to_second_window() {
197 $names = $this->getSession()->getWindowNames();
199 if (count($names) !== 2) {
200 throw new DriverException('Expected to see 2 windows open, found ' . count($names));
203 $this->getSession()->switchToWindow($names[1]);
207 * Switches to the main Moodle frame.
209 * @Given /^I switch to the main frame$/
211 public function switch_to_the_main_frame() {
212 $this->getSession()->switchToIFrame();
216 * Switches to the specified window. Useful when interacting with popup windows.
218 * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
219 * @param string $windowname
221 public function switch_to_window($windowname) {
222 if ($windowname === self::MAIN_WINDOW_NAME) {
223 // When switching to the main window normalise the window name to null.
224 // This is normalised further in the Mink driver to the root window ID.
225 $windowname = null;
228 $this->getSession()->switchToWindow($windowname);
232 * Switches to the main Moodle window. Useful when you finish interacting with popup windows.
234 * @Given /^I switch to the main window$/
236 public function switch_to_the_main_window() {
237 $this->switch_to_window(self::MAIN_WINDOW_NAME);
241 * Closes all extra windows opened during the navigation.
243 * This assumes all popups are opened by the main tab and you will now get back.
245 * @Given /^I close all opened windows$/
246 * @throws DriverException If there aren't exactly 1 tabs open when finish or no javascript running
248 public function i_close_all_opened_windows() {
249 if (!$this->running_javascript()) {
250 throw new DriverException('Closing windows steps require javascript');
252 $names = $this->getSession()->getWindowNames();
253 for ($index = 1; $index < count($names); $index ++) {
254 $this->getSession()->switchToWindow($names[$index]);
255 $this->execute_script("window.open('', '_self').close();");
257 $names = $this->getSession()->getWindowNames();
258 if (count($names) !== 1) {
259 throw new DriverException('Expected to see 1 tabs open, not ' . count($names));
261 $this->getSession()->switchToWindow($names[0]);
265 * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
266 * @Given /^I accept the currently displayed dialog$/
268 public function accept_currently_displayed_alert_dialog() {
269 $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->accept();
273 * Dismisses the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
274 * @Given /^I dismiss the currently displayed dialog$/
276 public function dismiss_currently_displayed_alert_dialog() {
277 $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->dismiss();
281 * Clicks link with specified id|title|alt|text.
283 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
284 * @throws ElementNotFoundException Thrown by behat_base::find
285 * @param string $link
287 public function click_link($link) {
289 $linknode = $this->find_link($link);
290 $this->ensure_node_is_visible($linknode);
291 $linknode->click();
295 * Waits X seconds. Required after an action that requires data from an AJAX request.
297 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
298 * @param int $seconds
300 public function i_wait_seconds($seconds) {
301 if ($this->running_javascript()) {
302 $this->getSession()->wait($seconds * 1000);
303 } else {
304 sleep($seconds);
309 * Waits until the page is completely loaded. This step is auto-executed after every step.
311 * @Given /^I wait until the page is ready$/
313 public function wait_until_the_page_is_ready() {
315 // No need to wait if not running JS.
316 if (!$this->running_javascript()) {
317 return;
320 $this->getSession()->wait(self::get_timeout() * 1000, self::PAGE_READY_JS);
324 * Waits until the provided element selector exists in the DOM
326 * Using the protected method as this method will be usually
327 * called by other methods which are not returning a set of
328 * steps and performs the actions directly, so it would not
329 * be executed if it returns another step.
331 * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" exists$/
332 * @param string $element
333 * @param string $selector
334 * @return void
336 public function wait_until_exists($element, $selectortype) {
337 $this->ensure_element_exists($element, $selectortype);
341 * Waits until the provided element does not exist in the DOM
343 * Using the protected method as this method will be usually
344 * called by other methods which are not returning a set of
345 * steps and performs the actions directly, so it would not
346 * be executed if it returns another step.
348 * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" does not exist$/
349 * @param string $element
350 * @param string $selector
351 * @return void
353 public function wait_until_does_not_exists($element, $selectortype) {
354 $this->ensure_element_does_not_exist($element, $selectortype);
358 * Generic mouse over action. Mouse over a element of the specified type.
360 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
361 * @param string $element Element we look for
362 * @param string $selectortype The type of what we look for
364 public function i_hover($element, $selectortype) {
365 // Gets the node based on the requested selector type and locator.
366 $node = $this->get_selected_node($selectortype, $element);
367 $this->execute_js_on_node($node, '{{ELEMENT}}.scrollIntoView();');
368 $node->mouseOver();
372 * Generic click action. Click on the element of the specified type.
374 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
375 * @param string $element Element we look for
376 * @param string $selectortype The type of what we look for
378 public function i_click_on($element, $selectortype) {
380 // Gets the node based on the requested selector type and locator.
381 $node = $this->get_selected_node($selectortype, $element);
382 $this->ensure_node_is_visible($node);
383 $node->click();
387 * Sets the focus and takes away the focus from an element, generating blur JS event.
389 * @When /^I take focus off "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
390 * @param string $element Element we look for
391 * @param string $selectortype The type of what we look for
393 public function i_take_focus_off_field($element, $selectortype) {
394 if (!$this->running_javascript()) {
395 throw new ExpectationException('Can\'t take focus off from "' . $element . '" in non-js mode', $this->getSession());
397 // Gets the node based on the requested selector type and locator.
398 $node = $this->get_selected_node($selectortype, $element);
399 $this->ensure_node_is_visible($node);
401 // Ensure element is focused before taking it off.
402 $node->focus();
403 $node->blur();
407 * Clicks the specified element and confirms the expected dialogue.
409 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" confirming the dialogue$/
410 * @throws ElementNotFoundException Thrown by behat_base::find
411 * @param string $element Element we look for
412 * @param string $selectortype The type of what we look for
414 public function i_click_on_confirming_the_dialogue($element, $selectortype) {
415 $this->i_click_on($element, $selectortype);
416 $this->execute('behat_general::accept_currently_displayed_alert_dialog', []);
417 $this->wait_until_the_page_is_ready();
421 * Clicks the specified element and dismissing the expected dialogue.
423 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" dismissing the dialogue$/
424 * @throws ElementNotFoundException Thrown by behat_base::find
425 * @param string $element Element we look for
426 * @param string $selectortype The type of what we look for
428 public function i_click_on_dismissing_the_dialogue($element, $selectortype) {
429 $this->i_click_on($element, $selectortype);
430 $this->execute('behat_general::dismiss_currently_displayed_alert_dialog', []);
431 $this->wait_until_the_page_is_ready();
435 * Click on the element of the specified type which is located inside the second element.
437 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
438 * @param string $element Element we look for
439 * @param string $selectortype The type of what we look for
440 * @param string $nodeelement Element we look in
441 * @param string $nodeselectortype The type of selector where we look in
443 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
445 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
446 $this->ensure_node_is_visible($node);
447 $node->click();
451 * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
453 * The steps definitions calling this step as part of them should
454 * manage the wait times by themselves as the times and when the
455 * waits should be done depends on what is being dragged & dropper.
457 * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
458 * @param string $element
459 * @param string $selectortype
460 * @param string $containerelement
461 * @param string $containerselectortype
463 public function i_drag_and_i_drop_it_in($source, $sourcetype, $target, $targettype) {
464 if (!$this->running_javascript()) {
465 throw new DriverException('Drag and drop steps require javascript');
468 $source = $this->find($sourcetype, $source);
469 $target = $this->find($targettype, $target);
471 if (!$source->isVisible()) {
472 throw new ExpectationException("'{$source}' '{$sourcetype}' is not visible", $this->getSession());
474 if (!$target->isVisible()) {
475 throw new ExpectationException("'{$target}' '{$targettype}' is not visible", $this->getSession());
478 $this->getSession()->getDriver()->dragTo($source->getXpath(), $target->getXpath());
482 * Checks, that the specified element is visible. Only available in tests using Javascript.
484 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
485 * @throws ElementNotFoundException
486 * @throws ExpectationException
487 * @throws DriverException
488 * @param string $element
489 * @param string $selectortype
490 * @return void
492 public function should_be_visible($element, $selectortype) {
494 if (!$this->running_javascript()) {
495 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
498 $node = $this->get_selected_node($selectortype, $element);
499 if (!$node->isVisible()) {
500 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
505 * Checks, that the existing element is not visible. Only available in tests using Javascript.
507 * As a "not" method, it's performance could not be good, but in this
508 * case the performance is good because the element must exist,
509 * otherwise there would be a ElementNotFoundException, also here we are
510 * not spinning until the element is visible.
512 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
513 * @throws ElementNotFoundException
514 * @throws ExpectationException
515 * @param string $element
516 * @param string $selectortype
517 * @return void
519 public function should_not_be_visible($element, $selectortype) {
521 try {
522 $this->should_be_visible($element, $selectortype);
523 } catch (ExpectationException $e) {
524 // All as expected.
525 return;
527 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
531 * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
533 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
534 * @throws ElementNotFoundException
535 * @throws DriverException
536 * @throws ExpectationException
537 * @param string $element Element we look for
538 * @param string $selectortype The type of what we look for
539 * @param string $nodeelement Element we look in
540 * @param string $nodeselectortype The type of selector where we look in
542 public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
544 if (!$this->running_javascript()) {
545 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
548 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
549 if (!$node->isVisible()) {
550 throw new ExpectationException(
551 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
552 $this->getSession()
558 * Checks, that the existing element is not visible inside the existing container. Only available in tests using Javascript.
560 * As a "not" method, it's performance could not be good, but in this
561 * case the performance is good because the element must exist,
562 * otherwise there would be a ElementNotFoundException, also here we are
563 * not spinning until the element is visible.
565 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
566 * @throws ElementNotFoundException
567 * @throws ExpectationException
568 * @param string $element Element we look for
569 * @param string $selectortype The type of what we look for
570 * @param string $nodeelement Element we look in
571 * @param string $nodeselectortype The type of selector where we look in
573 public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
575 try {
576 $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
577 } catch (ExpectationException $e) {
578 // All as expected.
579 return;
581 throw new ExpectationException(
582 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
583 $this->getSession()
588 * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
590 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
591 * @throws ExpectationException
592 * @param string $text
594 public function assert_page_contains_text($text) {
596 // Looking for all the matching nodes without any other descendant matching the
597 // same xpath (we are using contains(., ....).
598 $xpathliteral = behat_context_helper::escape($text);
599 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
600 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
602 try {
603 $nodes = $this->find_all('xpath', $xpath);
604 } catch (ElementNotFoundException $e) {
605 throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
608 // If we are not running javascript we have enough with the
609 // element existing as we can't check if it is visible.
610 if (!$this->running_javascript()) {
611 return;
614 // We spin as we don't have enough checking that the element is there, we
615 // should also ensure that the element is visible. Using microsleep as this
616 // is a repeated step and global performance is important.
617 $this->spin(
618 function($context, $args) {
620 foreach ($args['nodes'] as $node) {
621 if ($node->isVisible()) {
622 return true;
626 // If non of the nodes is visible we loop again.
627 throw new ExpectationException('"' . $args['text'] . '" text was found but was not visible', $context->getSession());
629 array('nodes' => $nodes, 'text' => $text),
630 false,
631 false,
632 true
638 * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
640 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
641 * @throws ExpectationException
642 * @param string $text
644 public function assert_page_not_contains_text($text) {
646 // Looking for all the matching nodes without any other descendant matching the
647 // same xpath (we are using contains(., ....).
648 $xpathliteral = behat_context_helper::escape($text);
649 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
650 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
652 // We should wait a while to ensure that the page is not still loading elements.
653 // Waiting less than self::get_timeout() as we already waited for the DOM to be ready and
654 // all JS to be executed.
655 try {
656 $nodes = $this->find_all('xpath', $xpath, false, false, self::get_reduced_timeout());
657 } catch (ElementNotFoundException $e) {
658 // All ok.
659 return;
662 // If we are not running javascript we have enough with the
663 // element existing as we can't check if it is hidden.
664 if (!$this->running_javascript()) {
665 throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
668 // If the element is there we should be sure that it is not visible.
669 $this->spin(
670 function($context, $args) {
672 foreach ($args['nodes'] as $node) {
673 // If element is removed from dom, then just exit.
674 try {
675 // If element is visible then throw exception, so we keep spinning.
676 if ($node->isVisible()) {
677 throw new ExpectationException('"' . $args['text'] . '" text was found in the page',
678 $context->getSession());
680 } catch (NoSuchElementException $e) {
681 // Do nothing just return, as element is no more on page.
682 return true;
683 } catch (ElementNotFoundException $e) {
684 // Do nothing just return, as element is no more on page.
685 return true;
689 // If non of the found nodes is visible we consider that the text is not visible.
690 return true;
692 array('nodes' => $nodes, 'text' => $text),
693 behat_base::get_reduced_timeout(),
694 false,
695 true
700 * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
702 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
703 * @throws ElementNotFoundException
704 * @throws ExpectationException
705 * @param string $text
706 * @param string $element Element we look in.
707 * @param string $selectortype The type of element where we are looking in.
709 public function assert_element_contains_text($text, $element, $selectortype) {
711 // Getting the container where the text should be found.
712 $container = $this->get_selected_node($selectortype, $element);
714 // Looking for all the matching nodes without any other descendant matching the
715 // same xpath (we are using contains(., ....).
716 $xpathliteral = behat_context_helper::escape($text);
717 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
718 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
720 // Wait until it finds the text inside the container, otherwise custom exception.
721 try {
722 $nodes = $this->find_all('xpath', $xpath, false, $container);
723 } catch (ElementNotFoundException $e) {
724 throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
727 // If we are not running javascript we have enough with the
728 // element existing as we can't check if it is visible.
729 if (!$this->running_javascript()) {
730 return;
733 // We also check the element visibility when running JS tests. Using microsleep as this
734 // is a repeated step and global performance is important.
735 $this->spin(
736 function($context, $args) {
738 foreach ($args['nodes'] as $node) {
739 if ($node->isVisible()) {
740 return true;
744 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element but was not visible', $context->getSession());
746 array('nodes' => $nodes, 'text' => $text, 'element' => $element),
747 false,
748 false,
749 true
754 * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
756 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
757 * @throws ElementNotFoundException
758 * @throws ExpectationException
759 * @param string $text
760 * @param string $element Element we look in.
761 * @param string $selectortype The type of element where we are looking in.
763 public function assert_element_not_contains_text($text, $element, $selectortype) {
765 // Getting the container where the text should be found.
766 $container = $this->get_selected_node($selectortype, $element);
768 // Looking for all the matching nodes without any other descendant matching the
769 // same xpath (we are using contains(., ....).
770 $xpathliteral = behat_context_helper::escape($text);
771 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
772 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
774 // We should wait a while to ensure that the page is not still loading elements.
775 // Giving preference to the reliability of the results rather than to the performance.
776 try {
777 $nodes = $this->find_all('xpath', $xpath, false, $container, self::get_reduced_timeout());
778 } catch (ElementNotFoundException $e) {
779 // All ok.
780 return;
783 // If we are not running javascript we have enough with the
784 // element not being found as we can't check if it is visible.
785 if (!$this->running_javascript()) {
786 throw new ExpectationException('"' . $text . '" text was found in the "' . $element . '" element', $this->getSession());
789 // We need to ensure all the found nodes are hidden.
790 $this->spin(
791 function($context, $args) {
793 foreach ($args['nodes'] as $node) {
794 if ($node->isVisible()) {
795 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element', $context->getSession());
799 // If all the found nodes are hidden we are happy.
800 return true;
802 array('nodes' => $nodes, 'text' => $text, 'element' => $element),
803 behat_base::get_reduced_timeout(),
804 false,
805 true
810 * Checks, that the first specified element appears before the second one.
812 * @Then :preelement :preselectortype should appear before :postelement :postselectortype
813 * @Then :preelement :preselectortype should appear before :postelement :postselectortype in the :containerelement :containerselectortype
814 * @throws ExpectationException
815 * @param string $preelement The locator of the preceding element
816 * @param string $preselectortype The selector type of the preceding element
817 * @param string $postelement The locator of the latest element
818 * @param string $postselectortype The selector type of the latest element
819 * @param string $containerelement
820 * @param string $containerselectortype
822 public function should_appear_before(
823 string $preelement,
824 string $preselectortype,
825 string $postelement,
826 string $postselectortype,
827 ?string $containerelement = null,
828 ?string $containerselectortype = null
830 $msg = "'{$preelement}' '{$preselectortype}' does not appear before '{$postelement}' '{$postselectortype}'";
831 $this->check_element_order(
832 $containerelement,
833 $containerselectortype,
834 $preelement,
835 $preselectortype,
836 $postelement,
837 $postselectortype,
838 $msg
843 * Checks, that the first specified element appears after the second one.
845 * @Then :postelement :postselectortype should appear after :preelement :preselectortype
846 * @Then :postelement :postselectortype should appear after :preelement :preselectortype in the :containerelement :containerselectortype
847 * @throws ExpectationException
848 * @param string $postelement The locator of the latest element
849 * @param string $postselectortype The selector type of the latest element
850 * @param string $preelement The locator of the preceding element
851 * @param string $preselectortype The selector type of the preceding element
852 * @param string $containerelement
853 * @param string $containerselectortype
855 public function should_appear_after(
856 string $postelement,
857 string $postselectortype,
858 string $preelement,
859 string $preselectortype,
860 ?string $containerelement = null,
861 ?string $containerselectortype = null
863 $msg = "'{$postelement}' '{$postselectortype}' does not appear after '{$preelement}' '{$preselectortype}'";
864 $this->check_element_order(
865 $containerelement,
866 $containerselectortype,
867 $preelement,
868 $preselectortype,
869 $postelement,
870 $postselectortype,
871 $msg
876 * Shared code to check whether an element is before or after another one.
878 * @param string $containerelement
879 * @param string $containerselectortype
880 * @param string $preelement The locator of the preceding element
881 * @param string $preselectortype The locator of the preceding element
882 * @param string $postelement The locator of the following element
883 * @param string $postselectortype The selector type of the following element
884 * @param string $msg Message to output if this fails
886 protected function check_element_order(
887 ?string $containerelement,
888 ?string $containerselectortype,
889 string $preelement,
890 string $preselectortype,
891 string $postelement,
892 string $postselectortype,
893 string $msg
895 $containernode = false;
896 if ($containerselectortype && $containerelement) {
897 // Get the container node.
898 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
899 $msg .= " in the '{$containerelement}' '{$containerselectortype}'";
902 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
903 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
905 $newlines = [
906 "\r\n",
907 "\r",
908 "\n",
910 $prexpath = str_replace($newlines, ' ', $this->find($preselector, $prelocator, false, $containernode)->getXpath());
911 $postxpath = str_replace($newlines, ' ', $this->find($postselector, $postlocator, false, $containernode)->getXpath());
913 if ($this->running_javascript()) {
914 // The xpath to do this was running really slowly on certain Chrome versions so we are using
915 // this DOM method instead.
916 $js = <<<EOF
917 (function() {
918 var a = document.evaluate("{$prexpath}", document, null, XPathResult.ANY_TYPE, null).iterateNext();
919 var b = document.evaluate("{$postxpath}", document, null, XPathResult.ANY_TYPE, null).iterateNext();
920 return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING;
921 })()
922 EOF;
923 $ok = $this->evaluate_script($js);
924 } else {
926 // Using following xpath axe to find it.
927 $xpath = "{$prexpath}/following::*[contains(., {$postxpath})]";
928 $ok = $this->getSession()->getDriver()->find($xpath);
931 if (!$ok) {
932 throw new ExpectationException($msg, $this->getSession());
937 * Checks, that element of specified type is disabled.
939 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
940 * @throws ExpectationException Thrown by behat_base::find
941 * @param string $element Element we look in
942 * @param string $selectortype The type of element where we are looking in.
944 public function the_element_should_be_disabled($element, $selectortype) {
945 $this->the_attribute_of_should_be_set("disabled", $element, $selectortype, false);
949 * Checks, that element of specified type is enabled.
951 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
952 * @throws ExpectationException Thrown by behat_base::find
953 * @param string $element Element we look on
954 * @param string $selectortype The type of where we look
956 public function the_element_should_be_enabled($element, $selectortype) {
957 $this->the_attribute_of_should_be_set("disabled", $element, $selectortype, true);
961 * Checks the provided element and selector type are readonly on the current page.
963 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
964 * @throws ExpectationException Thrown by behat_base::find
965 * @param string $element Element we look in
966 * @param string $selectortype The type of element where we are looking in.
968 public function the_element_should_be_readonly($element, $selectortype) {
969 $this->the_attribute_of_should_be_set("readonly", $element, $selectortype, false);
973 * Checks the provided element and selector type are not readonly on the current page.
975 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
976 * @throws ExpectationException Thrown by behat_base::find
977 * @param string $element Element we look in
978 * @param string $selectortype The type of element where we are looking in.
980 public function the_element_should_not_be_readonly($element, $selectortype) {
981 $this->the_attribute_of_should_be_set("readonly", $element, $selectortype, true);
985 * Checks the provided element and selector type exists in the current page.
987 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
989 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist$/
990 * @throws ElementNotFoundException Thrown by behat_base::find
991 * @param string $element The locator of the specified selector
992 * @param string $selectortype The selector type
994 public function should_exist($element, $selectortype) {
995 // Will throw an ElementNotFoundException if it does not exist.
996 $this->find($selectortype, $element);
1000 * Checks that the provided element and selector type not exists in the current page.
1002 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1004 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist$/
1005 * @throws ExpectationException
1006 * @param string $element The locator of the specified selector
1007 * @param string $selectortype The selector type
1009 public function should_not_exist($element, $selectortype) {
1010 // Will throw an ElementNotFoundException if it does not exist, but, actually it should not exist, so we try &
1011 // catch it.
1012 try {
1013 // The exception does not really matter as we will catch it and will never "explode".
1014 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $element);
1016 // Using the spin method as we want a reduced timeout but there is no need for a 0.1 seconds interval
1017 // because in the optimistic case we will timeout.
1018 // If all goes good it will throw an ElementNotFoundExceptionn that we will catch.
1019 $this->find($selectortype, $element, $exception, false, behat_base::get_reduced_timeout());
1020 } catch (ElementNotFoundException $e) {
1021 // We expect the element to not be found.
1022 return;
1025 // The element was found and should not have been. Throw an exception.
1026 throw new ExpectationException("The '{$element}' '{$selectortype}' exists in the current page", $this->getSession());
1030 * This step triggers cron like a user would do going to admin/cron.php.
1032 * @Given /^I trigger cron$/
1034 public function i_trigger_cron() {
1035 $this->execute('behat_general::i_visit', ['/admin/cron.php']);
1039 * Runs a scheduled task immediately, given full class name.
1041 * This is faster and more reliable than running cron (running cron won't
1042 * work more than once in the same test, for instance). However it is
1043 * a little less 'realistic'.
1045 * While the task is running, we suppress mtrace output because it makes
1046 * the Behat result look ugly.
1048 * Note: Most of the code relating to running a task is based on
1049 * admin/tool/task/cli/schedule_task.php.
1051 * @Given /^I run the scheduled task "(?P<task_name>[^"]+)"$/
1052 * @param string $taskname Name of task e.g. 'mod_whatever\task\do_something'
1054 public function i_run_the_scheduled_task($taskname) {
1055 global $CFG;
1056 require_once("{$CFG->libdir}/cronlib.php");
1058 $task = \core\task\manager::get_scheduled_task($taskname);
1059 if (!$task) {
1060 throw new DriverException('The "' . $taskname . '" scheduled task does not exist');
1063 // Do setup for cron task.
1064 raise_memory_limit(MEMORY_EXTRA);
1065 cron_setup_user();
1067 // Get lock.
1068 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
1069 if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
1070 throw new DriverException('Unable to obtain core_cron lock for scheduled task');
1072 if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
1073 $cronlock->release();
1074 throw new DriverException('Unable to obtain task lock for scheduled task');
1076 $task->set_lock($lock);
1077 if (!$task->is_blocking()) {
1078 $cronlock->release();
1079 } else {
1080 $task->set_cron_lock($cronlock);
1083 try {
1084 // Prepare the renderer.
1085 cron_prepare_core_renderer();
1087 // Discard task output as not appropriate for Behat output!
1088 ob_start();
1089 $task->execute();
1090 ob_end_clean();
1092 // Restore the previous renderer.
1093 cron_prepare_core_renderer(true);
1095 // Mark task complete.
1096 \core\task\manager::scheduled_task_complete($task);
1097 } catch (Exception $e) {
1098 // Restore the previous renderer.
1099 cron_prepare_core_renderer(true);
1101 // Mark task failed and throw exception.
1102 \core\task\manager::scheduled_task_failed($task);
1104 throw new DriverException('The "' . $taskname . '" scheduled task failed', 0, $e);
1109 * Runs all ad-hoc tasks in the queue.
1111 * This is faster and more reliable than running cron (running cron won't
1112 * work more than once in the same test, for instance). However it is
1113 * a little less 'realistic'.
1115 * While the task is running, we suppress mtrace output because it makes
1116 * the Behat result look ugly.
1118 * @Given /^I run all adhoc tasks$/
1119 * @throws DriverException
1121 public function i_run_all_adhoc_tasks() {
1122 global $CFG, $DB;
1123 require_once("{$CFG->libdir}/cronlib.php");
1125 // Do setup for cron task.
1126 cron_setup_user();
1128 // Discard task output as not appropriate for Behat output!
1129 ob_start();
1131 // Run all tasks which have a scheduled runtime of before now.
1132 $timenow = time();
1134 while (!\core\task\manager::static_caches_cleared_since($timenow) &&
1135 $task = \core\task\manager::get_next_adhoc_task($timenow)) {
1136 // Clean the output buffer between tasks.
1137 ob_clean();
1139 // Run the task.
1140 cron_run_inner_adhoc_task($task);
1142 // Check whether the task record still exists.
1143 // If a task was successful it will be removed.
1144 // If it failed then it will still exist.
1145 if ($DB->record_exists('task_adhoc', ['id' => $task->get_id()])) {
1146 // End ouptut buffering and flush the current buffer.
1147 // This should be from just the current task.
1148 ob_end_flush();
1150 throw new DriverException('An adhoc task failed', 0);
1153 ob_end_clean();
1157 * Checks that an element and selector type exists in another element and selector type on the current page.
1159 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1161 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
1162 * @throws ElementNotFoundException Thrown by behat_base::find
1163 * @param string $element The locator of the specified selector
1164 * @param string $selectortype The selector type
1165 * @param string $containerelement The container selector type
1166 * @param string $containerselectortype The container locator
1168 public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
1169 // Get the container node.
1170 $containernode = $this->find($containerselectortype, $containerelement);
1172 // Specific exception giving info about where can't we find the element.
1173 $containerdescription = $this->get_selector_description($containerselectortype, $containerelement);
1174 $locatorexceptionmsg = "{$element} not found in the {$containerdescription}}";
1175 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg);
1177 // Looks for the requested node inside the container node.
1178 $this->find($selectortype, $element, $exception, $containernode);
1182 * Checks that an element and selector type does not exist in another element and selector type on the current page.
1184 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1186 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
1187 * @throws ExpectationException
1188 * @param string $element The locator of the specified selector
1189 * @param string $selectortype The selector type
1190 * @param string $containerelement The container selector type
1191 * @param string $containerselectortype The container locator
1193 public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
1194 // Get the container node.
1195 $containernode = $this->find($containerselectortype, $containerelement);
1197 // Will throw an ElementNotFoundException if it does not exist, but, actually it should not exist, so we try &
1198 // catch it.
1199 try {
1200 // Looks for the requested node inside the container node.
1201 $this->find($selectortype, $element, false, $containernode, behat_base::get_reduced_timeout());
1202 } catch (ElementNotFoundException $e) {
1203 // We expect the element to not be found.
1204 return;
1207 // The element was found and should not have been. Throw an exception.
1208 $elementdescription = $this->get_selector_description($selectortype, $element);
1209 $containerdescription = $this->get_selector_description($containerselectortype, $containerelement);
1210 throw new ExpectationException(
1211 "The {$elementdescription} exists in the {$containerdescription}",
1212 $this->getSession()
1217 * Change browser window size small: 640x480, medium: 1024x768, large: 2560x1600, custom: widthxheight
1219 * Example: I change window size to "small" or I change window size to "1024x768"
1220 * or I change viewport size to "800x600". The viewport option is useful to guarantee that the
1221 * browser window has same viewport size even when you run Behat on multiple operating systems.
1223 * @throws ExpectationException
1224 * @Then /^I change (window|viewport) size to "(small|medium|large|\d+x\d+)"$/
1225 * @Then /^I change the (window|viewport) size to "(small|medium|large|\d+x\d+)"$/
1226 * @param string $windowsize size of the window (small|medium|large|wxh).
1228 public function i_change_window_size_to($windowviewport, $windowsize) {
1229 $this->resize_window($windowsize, $windowviewport === 'viewport');
1233 * Checks whether there the specified attribute is set or not.
1235 * @Then the :attribute attribute of :element :selectortype should be set
1236 * @Then the :attribute attribute of :element :selectortype should :not be set
1238 * @throws ExpectationException
1239 * @param string $attribute Name of attribute
1240 * @param string $element The locator of the specified selector
1241 * @param string $selectortype The selector type
1242 * @param string $not
1244 public function the_attribute_of_should_be_set($attribute, $element, $selectortype, $not = null) {
1245 // Get the container node (exception if it doesn't exist).
1246 $containernode = $this->get_selected_node($selectortype, $element);
1247 $hasattribute = $containernode->hasAttribute($attribute);
1249 if ($not && $hasattribute) {
1250 $value = $containernode->getAttribute($attribute);
1251 // Should not be set but is.
1252 throw new ExpectationException(
1253 "The attribute \"{$attribute}\" should not be set but has a value of '{$value}'",
1254 $this->getSession()
1256 } else if (!$not && !$hasattribute) {
1257 // Should be set but is not.
1258 throw new ExpectationException(
1259 "The attribute \"{$attribute}\" should be set but is not",
1260 $this->getSession()
1266 * Checks whether there is an attribute on the given element that contains the specified text.
1268 * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should contain "(?P<text_string>(?:[^"]|\\")*)"$/
1269 * @throws ExpectationException
1270 * @param string $attribute Name of attribute
1271 * @param string $element The locator of the specified selector
1272 * @param string $selectortype The selector type
1273 * @param string $text Expected substring
1275 public function the_attribute_of_should_contain($attribute, $element, $selectortype, $text) {
1276 // Get the container node (exception if it doesn't exist).
1277 $containernode = $this->get_selected_node($selectortype, $element);
1278 $value = $containernode->getAttribute($attribute);
1279 if ($value == null) {
1280 throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
1281 $this->getSession());
1282 } else if (strpos($value, $text) === false) {
1283 throw new ExpectationException('The attribute "' . $attribute .
1284 '" does not contain "' . $text . '" (actual value: "' . $value . '")',
1285 $this->getSession());
1290 * Checks that the attribute on the given element does not contain the specified text.
1292 * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not contain "(?P<text_string>(?:[^"]|\\")*)"$/
1293 * @throws ExpectationException
1294 * @param string $attribute Name of attribute
1295 * @param string $element The locator of the specified selector
1296 * @param string $selectortype The selector type
1297 * @param string $text Expected substring
1299 public function the_attribute_of_should_not_contain($attribute, $element, $selectortype, $text) {
1300 // Get the container node (exception if it doesn't exist).
1301 $containernode = $this->get_selected_node($selectortype, $element);
1302 $value = $containernode->getAttribute($attribute);
1303 if ($value == null) {
1304 throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
1305 $this->getSession());
1306 } else if (strpos($value, $text) !== false) {
1307 throw new ExpectationException('The attribute "' . $attribute .
1308 '" contains "' . $text . '" (value: "' . $value . '")',
1309 $this->getSession());
1314 * Checks the provided value exists in specific row/column of table.
1316 * @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should contain "(?P<value_string>[^"]*)"$/
1317 * @throws ElementNotFoundException
1318 * @param string $row row text which will be looked in.
1319 * @param string $column column text to search (or numeric value for the column position)
1320 * @param string $table table id/class/caption
1321 * @param string $value text to check.
1323 public function row_column_of_table_should_contain($row, $column, $table, $value) {
1324 $tablenode = $this->get_selected_node('table', $table);
1325 $tablexpath = $tablenode->getXpath();
1327 $rowliteral = behat_context_helper::escape($row);
1328 $valueliteral = behat_context_helper::escape($value);
1329 $columnliteral = behat_context_helper::escape($column);
1331 if (preg_match('/^-?(\d+)-?$/', $column, $columnasnumber)) {
1332 // Column indicated as a number, just use it as position of the column.
1333 $columnpositionxpath = "/child::*[position() = {$columnasnumber[1]}]";
1334 } else {
1335 // Header can be in thead or tbody (first row), following xpath should work.
1336 $theadheaderxpath = "thead/tr[1]/th[(normalize-space(.)=" . $columnliteral . " or a[normalize-space(text())=" .
1337 $columnliteral . "] or div[normalize-space(text())=" . $columnliteral . "])]";
1338 $tbodyheaderxpath = "tbody/tr[1]/td[(normalize-space(.)=" . $columnliteral . " or a[normalize-space(text())=" .
1339 $columnliteral . "] or div[normalize-space(text())=" . $columnliteral . "])]";
1341 // Check if column exists.
1342 $columnheaderxpath = $tablexpath . "[" . $theadheaderxpath . " | " . $tbodyheaderxpath . "]";
1343 $columnheader = $this->getSession()->getDriver()->find($columnheaderxpath);
1344 if (empty($columnheader)) {
1345 $columnexceptionmsg = $column . '" in table "' . $table . '"';
1346 throw new ElementNotFoundException($this->getSession(), "\n$columnheaderxpath\n\n".'Column', null, $columnexceptionmsg);
1348 // Following conditions were considered before finding column count.
1349 // 1. Table header can be in thead/tr/th or tbody/tr/td[1].
1350 // 2. First column can have th (Gradebook -> user report), so having lenient sibling check.
1351 $columnpositionxpath = "/child::*[position() = count(" . $tablexpath . "/" . $theadheaderxpath .
1352 "/preceding-sibling::*) + 1]";
1355 // Check if value exists in specific row/column.
1356 // Get row xpath.
1357 // GoutteDriver uses DomCrawler\Crawler and it is making XPath relative to the current context, so use descendant.
1358 $rowxpath = $tablexpath."/tbody/tr[descendant::th[normalize-space(.)=" . $rowliteral .
1359 "] | descendant::td[normalize-space(.)=" . $rowliteral . "]]";
1361 $columnvaluexpath = $rowxpath . $columnpositionxpath . "[contains(normalize-space(.)," . $valueliteral . ")]";
1363 // Looks for the requested node inside the container node.
1364 $coumnnode = $this->getSession()->getDriver()->find($columnvaluexpath);
1365 if (empty($coumnnode)) {
1366 $locatorexceptionmsg = $value . '" in "' . $row . '" row with column "' . $column;
1367 throw new ElementNotFoundException($this->getSession(), "\n$columnvaluexpath\n\n".'Column value', null, $locatorexceptionmsg);
1372 * Checks the provided value should not exist in specific row/column of table.
1374 * @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should not contain "(?P<value_string>[^"]*)"$/
1375 * @throws ElementNotFoundException
1376 * @param string $row row text which will be looked in.
1377 * @param string $column column text to search
1378 * @param string $table table id/class/caption
1379 * @param string $value text to check.
1381 public function row_column_of_table_should_not_contain($row, $column, $table, $value) {
1382 try {
1383 $this->row_column_of_table_should_contain($row, $column, $table, $value);
1384 } catch (ElementNotFoundException $e) {
1385 // Table row/column doesn't contain this value. Nothing to do.
1386 return;
1388 // Throw exception if found.
1389 throw new ExpectationException(
1390 '"' . $column . '" with value "' . $value . '" is present in "' . $row . '" row for table "' . $table . '"',
1391 $this->getSession()
1396 * Checks that the provided value exist in table.
1398 * First row may contain column headers or numeric indexes of the columns
1399 * (syntax -1- is also considered to be column index). Column indexes are
1400 * useful in case of multirow headers and/or presence of cells with colspan.
1402 * @Then /^the following should exist in the "(?P<table_string>[^"]*)" table:$/
1403 * @throws ExpectationException
1404 * @param string $table name of table
1405 * @param TableNode $data table with first row as header and following values
1406 * | Header 1 | Header 2 | Header 3 |
1407 * | Value 1 | Value 2 | Value 3|
1409 public function following_should_exist_in_the_table($table, TableNode $data) {
1410 $datahash = $data->getHash();
1412 foreach ($datahash as $row) {
1413 $firstcell = null;
1414 foreach ($row as $column => $value) {
1415 if ($firstcell === null) {
1416 $firstcell = $value;
1417 } else {
1418 $this->row_column_of_table_should_contain($firstcell, $column, $table, $value);
1425 * Checks that the provided values do not exist in a table.
1427 * @Then /^the following should not exist in the "(?P<table_string>[^"]*)" table:$/
1428 * @throws ExpectationException
1429 * @param string $table name of table
1430 * @param TableNode $data table with first row as header and following values
1431 * | Header 1 | Header 2 | Header 3 |
1432 * | Value 1 | Value 2 | Value 3|
1434 public function following_should_not_exist_in_the_table($table, TableNode $data) {
1435 $datahash = $data->getHash();
1437 foreach ($datahash as $value) {
1438 $row = array_shift($value);
1439 foreach ($value as $column => $value) {
1440 try {
1441 $this->row_column_of_table_should_contain($row, $column, $table, $value);
1442 // Throw exception if found.
1443 } catch (ElementNotFoundException $e) {
1444 // Table row/column doesn't contain this value. Nothing to do.
1445 continue;
1447 throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
1448 $row . '" row for table "' . $table . '"', $this->getSession()
1455 * Given the text of a link, download the linked file and return the contents.
1457 * This is a helper method used by {@link following_should_download_bytes()}
1458 * and {@link following_should_download_between_and_bytes()}
1460 * @param string $link the text of the link.
1461 * @return string the content of the downloaded file.
1463 public function download_file_from_link($link) {
1464 // Find the link.
1465 $linknode = $this->find_link($link);
1466 $this->ensure_node_is_visible($linknode);
1468 // Get the href and check it.
1469 $url = $linknode->getAttribute('href');
1470 if (!$url) {
1471 throw new ExpectationException('Download link does not have href attribute',
1472 $this->getSession());
1474 if (!preg_match('~^https?://~', $url)) {
1475 throw new ExpectationException('Download link not an absolute URL: ' . $url,
1476 $this->getSession());
1479 // Download the URL and check the size.
1480 $session = $this->getSession()->getCookie('MoodleSession');
1481 return download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
1485 * Downloads the file from a link on the page and checks the size.
1487 * Only works if the link has an href attribute. Javascript downloads are
1488 * not supported. Currently, the href must be an absolute URL.
1490 * @Then /^following "(?P<link_string>[^"]*)" should download "(?P<expected_bytes>\d+)" bytes$/
1491 * @throws ExpectationException
1492 * @param string $link the text of the link.
1493 * @param number $expectedsize the expected file size in bytes.
1495 public function following_should_download_bytes($link, $expectedsize) {
1496 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
1498 // It will stop spinning once file is downloaded or time out.
1499 $result = $this->spin(
1500 function($context, $args) {
1501 $link = $args['link'];
1502 return $this->download_file_from_link($link);
1504 array('link' => $link),
1505 behat_base::get_extended_timeout(),
1506 $exception
1509 // Check download size.
1510 $actualsize = (int)strlen($result);
1511 if ($actualsize !== (int)$expectedsize) {
1512 throw new ExpectationException('Downloaded data was ' . $actualsize .
1513 ' bytes, expecting ' . $expectedsize, $this->getSession());
1518 * Downloads the file from a link on the page and checks the size is in a given range.
1520 * Only works if the link has an href attribute. Javascript downloads are
1521 * not supported. Currently, the href must be an absolute URL.
1523 * The range includes the endpoints. That is, a 10 byte file in considered to
1524 * be between "5" and "10" bytes, and between "10" and "20" bytes.
1526 * @Then /^following "(?P<link_string>[^"]*)" should download between "(?P<min_bytes>\d+)" and "(?P<max_bytes>\d+)" bytes$/
1527 * @throws ExpectationException
1528 * @param string $link the text of the link.
1529 * @param number $minexpectedsize the minimum expected file size in bytes.
1530 * @param number $maxexpectedsize the maximum expected file size in bytes.
1532 public function following_should_download_between_and_bytes($link, $minexpectedsize, $maxexpectedsize) {
1533 // If the minimum is greater than the maximum then swap the values.
1534 if ((int)$minexpectedsize > (int)$maxexpectedsize) {
1535 list($minexpectedsize, $maxexpectedsize) = array($maxexpectedsize, $minexpectedsize);
1538 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
1540 // It will stop spinning once file is downloaded or time out.
1541 $result = $this->spin(
1542 function($context, $args) {
1543 $link = $args['link'];
1545 return $this->download_file_from_link($link);
1547 array('link' => $link),
1548 behat_base::get_extended_timeout(),
1549 $exception
1552 // Check download size.
1553 $actualsize = (int)strlen($result);
1554 if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) {
1555 throw new ExpectationException('Downloaded data was ' . $actualsize .
1556 ' bytes, expecting between ' . $minexpectedsize . ' and ' .
1557 $maxexpectedsize, $this->getSession());
1562 * Checks that the image on the page is the same as one of the fixture files
1564 * @Then /^the image at "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be identical to "(?P<filepath_string>(?:[^"]|\\")*)"$/
1565 * @throws ExpectationException
1566 * @param string $element The locator of the image
1567 * @param string $selectortype The selector type
1568 * @param string $filepath path to the fixture file
1570 public function the_image_at_should_be_identical_to($element, $selectortype, $filepath) {
1571 global $CFG;
1573 // Get the container node (exception if it doesn't exist).
1574 $containernode = $this->get_selected_node($selectortype, $element);
1575 $url = $containernode->getAttribute('src');
1576 if ($url == null) {
1577 throw new ExpectationException('Element does not have src attribute',
1578 $this->getSession());
1580 $session = $this->getSession()->getCookie('MoodleSession');
1581 $content = download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
1583 // Get the content of the fixture file.
1584 // Replace 'admin/' if it is in start of path with $CFG->admin .
1585 if (substr($filepath, 0, 6) === 'admin/') {
1586 $filepath = $CFG->admin . DIRECTORY_SEPARATOR . substr($filepath, 6);
1588 $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
1589 $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
1590 if (!is_readable($filepath)) {
1591 throw new ExpectationException('The file to compare to does not exist.', $this->getSession());
1593 $expectedcontent = file_get_contents($filepath);
1595 if ($content !== $expectedcontent) {
1596 throw new ExpectationException('Image is not identical to the fixture. Received ' .
1597 strlen($content) . ' bytes and expected ' . strlen($expectedcontent) . ' bytes', $this->getSession());
1602 * Prepare to detect whether or not a new page has loaded (or the same page reloaded) some time in the future.
1604 * @Given /^I start watching to see if a new page loads$/
1606 public function i_start_watching_to_see_if_a_new_page_loads() {
1607 if (!$this->running_javascript()) {
1608 throw new DriverException('Page load detection requires JavaScript.');
1611 $session = $this->getSession();
1613 if ($this->pageloaddetectionrunning || $session->getPage()->find('xpath', $this->get_page_load_xpath())) {
1614 // If we find this node at this point we are already watching for a reload and the behat steps
1615 // are out of order. We will treat this as an error - really it needs to be fixed as it indicates a problem.
1616 throw new ExpectationException(
1617 'Page load expectation error: page reloads are already been watched for.', $session);
1620 $this->pageloaddetectionrunning = true;
1622 $this->execute_script(
1623 'var span = document.createElement("span");
1624 span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '");
1625 span.setAttribute("style", "display: none;");
1626 document.body.appendChild(span);'
1631 * Verify that a new page has loaded (or the same page has reloaded) since the
1632 * last "I start watching to see if a new page loads" step.
1634 * @Given /^a new page should have loaded since I started watching$/
1636 public function a_new_page_should_have_loaded_since_i_started_watching() {
1637 $session = $this->getSession();
1639 // Make sure page load tracking was started.
1640 if (!$this->pageloaddetectionrunning) {
1641 throw new ExpectationException(
1642 'Page load expectation error: page load tracking was not started.', $session);
1645 // As the node is inserted by code above it is either there or not, and we do not need spin and it is safe
1646 // to use the native API here which is great as exception handling (the alternative is slow).
1647 if ($session->getPage()->find('xpath', $this->get_page_load_xpath())) {
1648 // We don't want to find this node, if we do we have an error.
1649 throw new ExpectationException(
1650 'Page load expectation error: a new page has not been loaded when it should have been.', $session);
1653 // Cancel the tracking of pageloaddetectionrunning.
1654 $this->pageloaddetectionrunning = false;
1658 * Verify that a new page has not loaded (or the same page has reloaded) since the
1659 * last "I start watching to see if a new page loads" step.
1661 * @Given /^a new page should not have loaded since I started watching$/
1663 public function a_new_page_should_not_have_loaded_since_i_started_watching() {
1664 $session = $this->getSession();
1666 // Make sure page load tracking was started.
1667 if (!$this->pageloaddetectionrunning) {
1668 throw new ExpectationException(
1669 'Page load expectation error: page load tracking was not started.', $session);
1672 // We use our API here as we can use the exception handling provided by it.
1673 $this->find(
1674 'xpath',
1675 $this->get_page_load_xpath(),
1676 new ExpectationException(
1677 'Page load expectation error: A new page has been loaded when it should not have been.',
1678 $this->getSession()
1684 * Helper used by {@link a_new_page_should_have_loaded_since_i_started_watching}
1685 * and {@link a_new_page_should_not_have_loaded_since_i_started_watching}
1686 * @return string xpath expression.
1688 protected function get_page_load_xpath() {
1689 return "//span[@data-rel = '" . self::PAGE_LOAD_DETECTION_STRING . "']";
1693 * Wait unit user press Enter/Return key. Useful when debugging a scenario.
1695 * @Then /^(?:|I )pause(?:| scenario execution)$/
1697 public function i_pause_scenario_execution() {
1698 $message = "<colour:lightYellow>Paused. Press <colour:lightRed>Enter/Return<colour:lightYellow> to continue.";
1699 behat_util::pause($this->getSession(), $message);
1703 * Presses a given button in the browser.
1704 * NOTE: Phantomjs and goutte driver reloads page while navigating back and forward.
1706 * @Then /^I press the "(back|forward|reload)" button in the browser$/
1707 * @param string $button the button to press.
1708 * @throws ExpectationException
1710 public function i_press_in_the_browser($button) {
1711 $session = $this->getSession();
1713 if ($button == 'back') {
1714 $session->back();
1715 } else if ($button == 'forward') {
1716 $session->forward();
1717 } else if ($button == 'reload') {
1718 $session->reload();
1719 } else {
1720 throw new ExpectationException('Unknown browser button.', $session);
1725 * Send key presses to the browser without first changing focusing, or applying the key presses to a specific
1726 * element.
1728 * Example usage of this step:
1729 * When I type "Penguin"
1731 * @When I type :keys
1732 * @param string $keys The key, or list of keys, to type
1734 public function i_type(string $keys): void {
1735 // Certain keys, such as the newline character, must be converted to the appropriate character code.
1736 // Without this, keys will behave differently depending on the browser.
1737 $keylist = array_map(function($key): string {
1738 switch ($key) {
1739 case "\n":
1740 return behat_keys::ENTER;
1741 default:
1742 return $key;
1744 }, str_split($keys));
1745 behat_base::type_keys($this->getSession(), $keylist);
1749 * Press a named or character key with an optional set of modifiers.
1751 * Supported named keys are:
1752 * - up
1753 * - down
1754 * - left
1755 * - right
1756 * - pageup|page_up
1757 * - pagedown|page_down
1758 * - home
1759 * - end
1760 * - insert
1761 * - delete
1762 * - backspace
1763 * - escape
1764 * - enter
1765 * - tab
1767 * You can also use a single character for the key name e.g. 'Ctrl C'.
1769 * Supported moderators are:
1770 * - shift
1771 * - ctrl
1772 * - alt
1773 * - meta
1775 * Example usage of this new step:
1776 * When I press the up key
1777 * When I press the space key
1778 * When I press the shift tab key
1780 * Multiple moderator keys can be combined using the '+' operator, for example:
1781 * When I press the ctrl+shift enter key
1782 * When I press the ctrl + shift enter key
1784 * @When /^I press the (?P<modifiers_string>.* )?(?P<key_string>.*) key$/
1785 * @param string $modifiers A list of keyboard modifiers, separated by the `+` character
1786 * @param string $key The name of the key to press
1788 public function i_press_named_key(string $modifiers, string $key): void {
1789 behat_base::require_javascript_in_session($this->getSession());
1791 $keys = [];
1793 foreach (explode('+', $modifiers) as $modifier) {
1794 switch (strtoupper(trim($modifier))) {
1795 case '':
1796 break;
1797 case 'SHIFT':
1798 $keys[] = behat_keys::SHIFT;
1799 break;
1800 case 'CTRL':
1801 $keys[] = behat_keys::CONTROL;
1802 break;
1803 case 'ALT':
1804 $keys[] = behat_keys::ALT;
1805 break;
1806 case 'META':
1807 $keys[] = behat_keys::META;
1808 break;
1809 default:
1810 throw new \coding_exception("Unknown modifier key '$modifier'}");
1814 $modifier = trim($key);
1815 switch (strtoupper($key)) {
1816 case 'UP':
1817 $keys[] = behat_keys::ARROW_UP;
1818 break;
1819 case 'DOWN':
1820 $keys[] = behat_keys::ARROW_DOWN;
1821 break;
1822 case 'LEFT':
1823 $keys[] = behat_keys::ARROW_LEFT;
1824 break;
1825 case 'RIGHT':
1826 $keys[] = behat_keys::ARROW_RIGHT;
1827 break;
1828 case 'HOME':
1829 $keys[] = behat_keys::HOME;
1830 break;
1831 case 'END':
1832 $keys[] = behat_keys::END;
1833 break;
1834 case 'INSERT':
1835 $keys[] = behat_keys::INSERT;
1836 break;
1837 case 'BACKSPACE':
1838 $keys[] = behat_keys::BACKSPACE;
1839 break;
1840 case 'DELETE':
1841 $keys[] = behat_keys::DELETE;
1842 break;
1843 case 'PAGEUP':
1844 case 'PAGE_UP':
1845 $keys[] = behat_keys::PAGE_UP;
1846 break;
1847 case 'PAGEDOWN':
1848 case 'PAGE_DOWN':
1849 $keys[] = behat_keys::PAGE_DOWN;
1850 break;
1851 case 'ESCAPE':
1852 $keys[] = behat_keys::ESCAPE;
1853 break;
1854 case 'ENTER':
1855 $keys[] = behat_keys::ENTER;
1856 break;
1857 case 'TAB':
1858 $keys[] = behat_keys::TAB;
1859 break;
1860 case 'SPACE':
1861 $keys[] = behat_keys::SPACE;
1862 break;
1863 default:
1864 // You can enter a single ASCII character (e.g. a letter) to directly type that key.
1865 if (strlen($key) === 1) {
1866 $keys[] = strtolower($key);
1867 } else {
1868 throw new \coding_exception("Unknown key '$key'}");
1872 behat_base::type_keys($this->getSession(), $keys);
1876 * Trigger a keydown event for a key on a specific element.
1878 * @When /^I press key "(?P<key_string>(?:[^"]|\\")*)" in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
1879 * @param string $key either char-code or character itself,
1880 * may optionally be prefixed with ctrl-, alt-, shift- or meta-
1881 * @param string $element Element we look for
1882 * @param string $selectortype The type of what we look for
1883 * @throws DriverException
1884 * @throws ExpectationException
1886 public function i_press_key_in_element($key, $element, $selectortype) {
1887 if (!$this->running_javascript()) {
1888 throw new DriverException('Key down step is not available with Javascript disabled');
1890 // Gets the node based on the requested selector type and locator.
1891 $node = $this->get_selected_node($selectortype, $element);
1892 $modifier = null;
1893 $validmodifiers = array('ctrl', 'alt', 'shift', 'meta');
1894 $char = $key;
1895 if (strpos($key, '-')) {
1896 list($modifier, $char) = preg_split('/-/', $key, 2);
1897 $modifier = strtolower($modifier);
1898 if (!in_array($modifier, $validmodifiers)) {
1899 throw new ExpectationException(sprintf('Unknown key modifier: %s.', $modifier),
1900 $this->getSession());
1903 if (is_numeric($char)) {
1904 $char = (int)$char;
1907 $node->keyDown($char, $modifier);
1908 $node->keyPress($char, $modifier);
1909 $node->keyUp($char, $modifier);
1913 * Press tab key on a specific element.
1915 * @When /^I press tab key in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
1916 * @param string $element Element we look for
1917 * @param string $selectortype The type of what we look for
1918 * @throws DriverException
1919 * @throws ExpectationException
1921 public function i_post_tab_key_in_element($element, $selectortype) {
1922 if (!$this->running_javascript()) {
1923 throw new DriverException('Tab press step is not available with Javascript disabled');
1925 // Gets the node based on the requested selector type and locator.
1926 $node = $this->get_selected_node($selectortype, $element);
1927 $this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
1928 $this->execute('behat_general::i_press_named_key', ['', 'tab']);
1932 * Checks if database family used is using one of the specified, else skip. (mysql, postgres, mssql, oracle, etc.)
1934 * @Given /^database family used is one of the following:$/
1935 * @param TableNode $databasefamilies list of database.
1936 * @return void.
1937 * @throws \Moodle\BehatExtension\Exception\SkippedException
1939 public function database_family_used_is_one_of_the_following(TableNode $databasefamilies) {
1940 global $DB;
1942 $dbfamily = $DB->get_dbfamily();
1944 // Check if used db family is one of the specified ones. If yes then return.
1945 foreach ($databasefamilies->getRows() as $dbfamilytocheck) {
1946 if ($dbfamilytocheck[0] == $dbfamily) {
1947 return;
1951 throw new \Moodle\BehatExtension\Exception\SkippedException();
1955 * Checks if given plugin is installed, and skips the current scenario if not.
1957 * @Given the :plugin plugin is installed
1958 * @param string $plugin frankenstyle plugin name, e.g. 'filter_embedquestion'.
1959 * @throws \Moodle\BehatExtension\Exception\SkippedException
1961 public function plugin_is_installed(string $plugin): void {
1962 $path = core_component::get_component_directory($plugin);
1963 if (!is_readable($path . '/version.php')) {
1964 throw new \Moodle\BehatExtension\Exception\SkippedException(
1965 'Skipping this scenario because the ' . $plugin . ' is not installed.');
1970 * Checks focus is with the given element.
1972 * @Then /^the focused element is( not)? "(?P<node_string>(?:[^"]|\\")*)" "(?P<node_selector_string>[^"]*)"$/
1973 * @param string $not optional step verifier
1974 * @param string $nodeelement Element identifier
1975 * @param string $nodeselectortype Element type
1976 * @throws DriverException If not using JavaScript
1977 * @throws ExpectationException
1979 public function the_focused_element_is($not, $nodeelement, $nodeselectortype) {
1980 if (!$this->running_javascript()) {
1981 throw new DriverException('Checking focus on an element requires JavaScript');
1984 $element = $this->find($nodeselectortype, $nodeelement);
1985 $xpath = addslashes_js($element->getXpath());
1986 $script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
1987 document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
1988 $targetisfocused = $this->evaluate_script($script);
1989 if ($not == ' not') {
1990 if ($targetisfocused) {
1991 throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
1993 } else {
1994 if (!$targetisfocused) {
1995 throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
2001 * Checks focus is with the given element.
2003 * @Then /^the focused element is( not)? "(?P<n>(?:[^"]|\\")*)" "(?P<ns>[^"]*)" in the "(?P<c>(?:[^"]|\\")*)" "(?P<cs>[^"]*)"$/
2004 * @param string $not string optional step verifier
2005 * @param string $element Element identifier
2006 * @param string $selectortype Element type
2007 * @param string $nodeelement Element we look in
2008 * @param string $nodeselectortype The type of selector where we look in
2009 * @throws DriverException If not using JavaScript
2010 * @throws ExpectationException
2012 public function the_focused_element_is_in_the($not, $element, $selectortype, $nodeelement, $nodeselectortype) {
2013 if (!$this->running_javascript()) {
2014 throw new DriverException('Checking focus on an element requires JavaScript');
2016 $element = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
2017 $xpath = addslashes_js($element->getXpath());
2018 $script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
2019 document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
2020 $targetisfocused = $this->evaluate_script($script);
2021 if ($not == ' not') {
2022 if ($targetisfocused) {
2023 throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
2025 } else {
2026 if (!$targetisfocused) {
2027 throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
2033 * Manually press tab key.
2035 * @When /^I press( shift)? tab$/
2036 * @param string $shift string optional step verifier
2037 * @throws DriverException
2039 public function i_manually_press_tab($shift = '') {
2040 if (empty($shift)) {
2041 $this->execute('behat_general::i_press_named_key', ['', 'tab']);
2042 } else {
2043 $this->execute('behat_general::i_press_named_key', ['shift', 'tab']);
2048 * Trigger click on node via javascript instead of actually clicking on it via pointer.
2049 * This function resolves the issue of nested elements.
2051 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" skipping visibility check$/
2052 * @param string $element
2053 * @param string $selectortype
2055 public function i_click_on_skipping_visibility_check($element, $selectortype) {
2057 // Gets the node based on the requested selector type and locator.
2058 $node = $this->get_selected_node($selectortype, $element);
2059 $this->js_trigger_click($node);
2063 * Checks, that the specified element contains the specified text a certain amount of times.
2064 * When running Javascript tests it also considers that texts may be hidden.
2066 * @Then /^I should see "(?P<elementscount_number>\d+)" occurrences of "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
2067 * @throws ElementNotFoundException
2068 * @throws ExpectationException
2069 * @param int $elementscount How many occurrences of the element we look for.
2070 * @param string $text
2071 * @param string $element Element we look in.
2072 * @param string $selectortype The type of element where we are looking in.
2074 public function i_should_see_occurrences_of_in_element($elementscount, $text, $element, $selectortype) {
2076 // Getting the container where the text should be found.
2077 $container = $this->get_selected_node($selectortype, $element);
2079 // Looking for all the matching nodes without any other descendant matching the
2080 // same xpath (we are using contains(., ....).
2081 $xpathliteral = behat_context_helper::escape($text);
2082 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
2083 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
2085 $nodes = $this->find_all('xpath', $xpath, false, $container);
2087 if ($this->running_javascript()) {
2088 $nodes = array_filter($nodes, function($node) {
2089 return $node->isVisible();
2093 if ($elementscount != count($nodes)) {
2094 throw new ExpectationException('Found '.count($nodes).' elements in column. Expected '.$elementscount,
2095 $this->getSession());
2100 * Manually press enter key.
2102 * @When /^I press enter/
2103 * @throws DriverException
2105 public function i_manually_press_enter() {
2106 $this->execute('behat_general::i_press_named_key', ['', 'enter']);
2110 * Visit a local URL relative to the behat root.
2112 * @When I visit :localurl
2114 * @param string|moodle_url $localurl The URL relative to the behat_wwwroot to visit.
2116 public function i_visit($localurl): void {
2117 $localurl = new moodle_url($localurl);
2118 $this->getSession()->visit($this->locate_path($localurl->out_as_local_url(false)));
2122 * Increase the webdriver timeouts.
2124 * This should be reset between scenarios, or can be called again to decrease the timeouts.
2126 * @Given I mark this test as slow setting a timeout factor of :factor
2128 public function i_mark_this_test_as_long_running(int $factor = 2): void {
2129 $this->set_test_timeout_factor($factor);
2133 * Set the default text editor to the named text editor.
2135 * @Given the default editor is set to :editor
2136 * @param string $editor
2137 * @throws ExpectationException If the specified editor is not available.
2139 public function the_default_editor_is_set_to(string $editor): void {
2140 global $CFG;
2142 // Check if the provided editor is available.
2143 if (!array_key_exists($editor, editors_get_available())) {
2144 throw new ExpectationException(
2145 "Unable to set the editor to {$editor} as it is not installed. The available editors are: " .
2146 implode(', ', array_keys(editors_get_available())),
2147 $this->getSession()
2151 // Make the provided editor the default one in $CFG->texteditors by
2152 // moving it to the first [editor],atto,tiny,tinymce,textarea on the list.
2153 $list = explode(',', $CFG->texteditors);
2154 array_unshift($list, $editor);
2155 $list = array_unique($list);
2157 // Set the list new list of editors.
2158 set_config('texteditors', implode(',', $list));