Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * General use steps definitions.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2012 David MonllaĆ³
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  require_once (__DIR__ . '/../../behat/behat_base.php');
  29  
  30  use Behat\Gherkin\Node\TableNode;
  31  use Behat\Mink\Element\NodeElement;
  32  use Behat\Mink\Exception\DriverException;
  33  use Behat\Mink\Exception\ElementNotFoundException;
  34  use Behat\Mink\Exception\ExpectationException;
  35  use Facebook\WebDriver\Exception\NoSuchElementException;
  36  use Facebook\WebDriver\Exception\StaleElementReferenceException;
  37  
  38  /**
  39   * Cross component steps definitions.
  40   *
  41   * Basic web application definitions from MinkExtension and
  42   * BehatchExtension. Definitions modified according to our needs
  43   * when necessary and including only the ones we need to avoid
  44   * overlapping and confusion.
  45   *
  46   * @package   core
  47   * @category  test
  48   * @copyright 2012 David MonllaĆ³
  49   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  50   */
  51  class behat_general extends behat_base {
  52  
  53      /**
  54       * @var string used by {@link switch_to_window()} and
  55       * {@link switch_to_the_main_window()} to work-around a Chrome browser issue.
  56       */
  57      const MAIN_WINDOW_NAME = '__moodle_behat_main_window_name';
  58  
  59      /**
  60       * @var string when we want to check whether or not a new page has loaded,
  61       * we first write this unique string into the page. Then later, by checking
  62       * whether it is still there, we can tell if a new page has been loaded.
  63       */
  64      const PAGE_LOAD_DETECTION_STRING = 'new_page_not_loaded_since_behat_started_watching';
  65  
  66      /**
  67       * @var $pageloaddetectionrunning boolean Used to ensure that page load detection was started before a page reload
  68       * was checked for.
  69       */
  70      private $pageloaddetectionrunning = false;
  71  
  72      /**
  73       * Opens Moodle homepage.
  74       *
  75       * @Given /^I am on homepage$/
  76       */
  77      public function i_am_on_homepage() {
  78          $this->execute('behat_general::i_visit', ['/']);
  79      }
  80  
  81      /**
  82       * Opens Moodle site homepage.
  83       *
  84       * @Given /^I am on site homepage$/
  85       */
  86      public function i_am_on_site_homepage() {
  87          $this->execute('behat_general::i_visit', ['/?redirect=0']);
  88      }
  89  
  90      /**
  91       * Opens course index page.
  92       *
  93       * @Given /^I am on course index$/
  94       */
  95      public function i_am_on_course_index() {
  96          $this->execute('behat_general::i_visit', ['/course/index.php']);
  97      }
  98  
  99      /**
 100       * Reloads the current page.
 101       *
 102       * @Given /^I reload the page$/
 103       */
 104      public function reload() {
 105          $this->getSession()->reload();
 106      }
 107  
 108      /**
 109       * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection
 110       *
 111       * @Given /^I wait to be redirected$/
 112       */
 113      public function i_wait_to_be_redirected() {
 114  
 115          // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and
 116          // moodle_page::$periodicrefreshdelay possible values.
 117          if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) {
 118              // We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
 119              return true;
 120          }
 121  
 122          // Wrapped in try & catch in case the redirection has already been executed.
 123          try {
 124              $content = $metarefresh->getAttribute('content');
 125          } catch (NoSuchElementException $e) {
 126              return true;
 127          } catch (StaleElementReferenceException $e) {
 128              return true;
 129          }
 130  
 131          // Getting the refresh time and the url if present.
 132          if (strstr($content, 'url') != false) {
 133  
 134              list($waittime, $url) = explode(';', $content);
 135  
 136              // Cleaning the URL value.
 137              $url = trim(substr($url, strpos($url, 'http')));
 138  
 139          } else {
 140              // Just wait then.
 141              $waittime = $content;
 142          }
 143  
 144  
 145          // Wait until the URL change is executed.
 146          if ($this->running_javascript()) {
 147              $this->getSession()->wait($waittime * 1000);
 148  
 149          } else if (!empty($url)) {
 150              // We redirect directly as we can not wait for an automatic redirection.
 151              $this->getSession()->getDriver()->getClient()->request('GET', $url);
 152  
 153          } else {
 154              // Reload the page if no URL was provided.
 155              $this->getSession()->getDriver()->reload();
 156          }
 157      }
 158  
 159      /**
 160       * Switches to the specified iframe.
 161       *
 162       * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" iframe$/
 163       * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" class iframe$/
 164       * @param string $name The name of the iframe
 165       */
 166      public function switch_to_iframe($name) {
 167          // We spin to give time to the iframe to be loaded.
 168          // Using extended timeout as we don't know about which
 169          // kind of iframe will be loaded.
 170          $this->spin(
 171              function($context) use ($name){
 172                  $iframe = $context->find('iframe', $name);
 173                  if ($iframe->hasAttribute('name')) {
 174                      $iframename = $iframe->getAttribute('name');
 175                  } else {
 176                      if (!$this->running_javascript()) {
 177                          throw new \coding_exception('iframe must have a name attribute to use the switchTo command.');
 178                      }
 179                      $iframename = uniqid();
 180                      $this->execute_js_on_node($iframe, "{{ELEMENT}}.name = '{$iframename}';");
 181                  }
 182                  $context->getSession()->switchToIFrame($iframename);
 183  
 184                  // If no exception we are done.
 185                  return true;
 186              },
 187              behat_base::get_extended_timeout()
 188          );
 189      }
 190  
 191      /**
 192       * Switches to a second window.
 193       *
 194       * @Given /^I switch to a second window$/
 195       * @throws DriverException If there aren't exactly 2 windows open.
 196       */
 197      public function switch_to_second_window() {
 198          $names = $this->getSession()->getWindowNames();
 199  
 200          if (count($names) !== 2) {
 201              throw new DriverException('Expected to see 2 windows open, found ' . count($names));
 202          }
 203  
 204          $this->getSession()->switchToWindow($names[1]);
 205      }
 206  
 207      /**
 208       * Switches to the main Moodle frame.
 209       *
 210       * @Given /^I switch to the main frame$/
 211       */
 212      public function switch_to_the_main_frame() {
 213          $this->getSession()->switchToIFrame();
 214      }
 215  
 216      /**
 217       * Switches to the specified window. Useful when interacting with popup windows.
 218       *
 219       * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
 220       * @param string $windowname
 221       */
 222      public function switch_to_window($windowname) {
 223          if ($windowname === self::MAIN_WINDOW_NAME) {
 224              // When switching to the main window normalise the window name to null.
 225              // This is normalised further in the Mink driver to the root window ID.
 226              $windowname = null;
 227          }
 228  
 229          $this->getSession()->switchToWindow($windowname);
 230      }
 231  
 232      /**
 233       * Switches to the main Moodle window. Useful when you finish interacting with popup windows.
 234       *
 235       * @Given /^I switch to the main window$/
 236       */
 237      public function switch_to_the_main_window() {
 238          $this->switch_to_window(self::MAIN_WINDOW_NAME);
 239      }
 240  
 241      /**
 242       * Closes all extra windows opened during the navigation.
 243       *
 244       * This assumes all popups are opened by the main tab and you will now get back.
 245       *
 246       * @Given /^I close all opened windows$/
 247       * @throws DriverException If there aren't exactly 1 tabs open when finish or no javascript running
 248       */
 249      public function i_close_all_opened_windows() {
 250          if (!$this->running_javascript()) {
 251              throw new DriverException('Closing windows steps require javascript');
 252          }
 253          $names = $this->getSession()->getWindowNames();
 254          for ($index = 1; $index < count($names); $index ++) {
 255              $this->getSession()->switchToWindow($names[$index]);
 256              $this->execute_script("window.open('', '_self').close();");
 257          }
 258          $names = $this->getSession()->getWindowNames();
 259          if (count($names) !== 1) {
 260              throw new DriverException('Expected to see 1 tabs open, not ' . count($names));
 261          }
 262          $this->getSession()->switchToWindow($names[0]);
 263      }
 264  
 265      /**
 266       * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
 267       * @Given /^I accept the currently displayed dialog$/
 268       */
 269      public function accept_currently_displayed_alert_dialog() {
 270          $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->accept();
 271      }
 272  
 273      /**
 274       * Dismisses the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
 275       * @Given /^I dismiss the currently displayed dialog$/
 276       */
 277      public function dismiss_currently_displayed_alert_dialog() {
 278          $this->getSession()->getDriver()->getWebDriver()->switchTo()->alert()->dismiss();
 279      }
 280  
 281      /**
 282       * Clicks link with specified id|title|alt|text.
 283       *
 284       * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
 285       * @throws ElementNotFoundException Thrown by behat_base::find
 286       * @param string $link
 287       */
 288      public function click_link($link) {
 289  
 290          $linknode = $this->find_link($link);
 291          $this->ensure_node_is_visible($linknode);
 292          $linknode->click();
 293      }
 294  
 295      /**
 296       * Waits X seconds. Required after an action that requires data from an AJAX request.
 297       *
 298       * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
 299       * @param int $seconds
 300       */
 301      public function i_wait_seconds($seconds) {
 302          if ($this->running_javascript()) {
 303              $this->getSession()->wait($seconds * 1000);
 304          } else {
 305              sleep($seconds);
 306          }
 307      }
 308  
 309      /**
 310       * Waits until the page is completely loaded. This step is auto-executed after every step.
 311       *
 312       * @Given /^I wait until the page is ready$/
 313       */
 314      public function wait_until_the_page_is_ready() {
 315  
 316          // No need to wait if not running JS.
 317          if (!$this->running_javascript()) {
 318              return;
 319          }
 320  
 321          $this->getSession()->wait(self::get_timeout() * 1000, self::PAGE_READY_JS);
 322      }
 323  
 324      /**
 325       * Waits until the provided element selector exists in the DOM
 326       *
 327       * Using the protected method as this method will be usually
 328       * called by other methods which are not returning a set of
 329       * steps and performs the actions directly, so it would not
 330       * be executed if it returns another step.
 331  
 332       * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" exists$/
 333       * @param string $element
 334       * @param string $selector
 335       * @return void
 336       */
 337      public function wait_until_exists($element, $selectortype) {
 338          $this->ensure_element_exists($element, $selectortype);
 339      }
 340  
 341      /**
 342       * Waits until the provided element does not exist in the DOM
 343       *
 344       * Using the protected method as this method will be usually
 345       * called by other methods which are not returning a set of
 346       * steps and performs the actions directly, so it would not
 347       * be executed if it returns another step.
 348  
 349       * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" does not exist$/
 350       * @param string $element
 351       * @param string $selector
 352       * @return void
 353       */
 354      public function wait_until_does_not_exists($element, $selectortype) {
 355          $this->ensure_element_does_not_exist($element, $selectortype);
 356      }
 357  
 358      /**
 359       * Generic mouse over action. Mouse over a element of the specified type.
 360       *
 361       * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
 362       * @param string $element Element we look for
 363       * @param string $selectortype The type of what we look for
 364       */
 365      public function i_hover($element, $selectortype) {
 366          // Gets the node based on the requested selector type and locator.
 367          $node = $this->get_selected_node($selectortype, $element);
 368          $this->execute_js_on_node($node, '{{ELEMENT}}.scrollIntoView();');
 369          $node->mouseOver();
 370      }
 371  
 372      /**
 373       * Generic click action. Click on the element of the specified type.
 374       *
 375       * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
 376       * @param string $element Element we look for
 377       * @param string $selectortype The type of what we look for
 378       */
 379      public function i_click_on($element, $selectortype) {
 380  
 381          // Gets the node based on the requested selector type and locator.
 382          $node = $this->get_selected_node($selectortype, $element);
 383          $this->ensure_node_is_visible($node);
 384          $node->click();
 385      }
 386  
 387      /**
 388       * Sets the focus and takes away the focus from an element, generating blur JS event.
 389       *
 390       * @When /^I take focus off "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
 391       * @param string $element Element we look for
 392       * @param string $selectortype The type of what we look for
 393       */
 394      public function i_take_focus_off_field($element, $selectortype) {
 395          if (!$this->running_javascript()) {
 396              throw new ExpectationException('Can\'t take focus off from "' . $element . '" in non-js mode', $this->getSession());
 397          }
 398          // Gets the node based on the requested selector type and locator.
 399          $node = $this->get_selected_node($selectortype, $element);
 400          $this->ensure_node_is_visible($node);
 401  
 402          // Ensure element is focused before taking it off.
 403          $node->focus();
 404          $node->blur();
 405      }
 406  
 407      /**
 408       * Clicks the specified element and confirms the expected dialogue.
 409       *
 410       * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" confirming the dialogue$/
 411       * @throws ElementNotFoundException Thrown by behat_base::find
 412       * @param string $element Element we look for
 413       * @param string $selectortype The type of what we look for
 414       */
 415      public function i_click_on_confirming_the_dialogue($element, $selectortype) {
 416          $this->i_click_on($element, $selectortype);
 417          $this->execute('behat_general::accept_currently_displayed_alert_dialog', []);
 418          $this->wait_until_the_page_is_ready();
 419      }
 420  
 421      /**
 422       * Clicks the specified element and dismissing the expected dialogue.
 423       *
 424       * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" dismissing the dialogue$/
 425       * @throws ElementNotFoundException Thrown by behat_base::find
 426       * @param string $element Element we look for
 427       * @param string $selectortype The type of what we look for
 428       */
 429      public function i_click_on_dismissing_the_dialogue($element, $selectortype) {
 430          $this->i_click_on($element, $selectortype);
 431          $this->execute('behat_general::dismiss_currently_displayed_alert_dialog', []);
 432          $this->wait_until_the_page_is_ready();
 433      }
 434  
 435      /**
 436       * Click on the element of the specified type which is located inside the second element.
 437       *
 438       * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
 439       * @param string $element Element we look for
 440       * @param string $selectortype The type of what we look for
 441       * @param string $nodeelement Element we look in
 442       * @param string $nodeselectortype The type of selector where we look in
 443       */
 444      public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
 445  
 446          $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
 447          $this->ensure_node_is_visible($node);
 448          $node->click();
 449      }
 450  
 451      /**
 452       * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
 453       *
 454       * The steps definitions calling this step as part of them should
 455       * manage the wait times by themselves as the times and when the
 456       * waits should be done depends on what is being dragged & dropper.
 457       *
 458       * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
 459       * @param string $element
 460       * @param string $selectortype
 461       * @param string $containerelement
 462       * @param string $containerselectortype
 463       */
 464      public function i_drag_and_i_drop_it_in($source, $sourcetype, $target, $targettype) {
 465          if (!$this->running_javascript()) {
 466              throw new DriverException('Drag and drop steps require javascript');
 467          }
 468  
 469          $source = $this->find($sourcetype, $source);
 470          $target = $this->find($targettype, $target);
 471  
 472          if (!$source->isVisible()) {
 473              throw new ExpectationException("'{$source}' '{$sourcetype}' is not visible", $this->getSession());
 474          }
 475          if (!$target->isVisible()) {
 476              throw new ExpectationException("'{$target}' '{$targettype}' is not visible", $this->getSession());
 477          }
 478  
 479          $this->getSession()->getDriver()->dragTo($source->getXpath(), $target->getXpath());
 480      }
 481  
 482      /**
 483       * Checks, that the specified element is visible. Only available in tests using Javascript.
 484       *
 485       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
 486       * @throws ElementNotFoundException
 487       * @throws ExpectationException
 488       * @throws DriverException
 489       * @param string $element
 490       * @param string $selectortype
 491       * @return void
 492       */
 493      public function should_be_visible($element, $selectortype) {
 494  
 495          if (!$this->running_javascript()) {
 496              throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
 497          }
 498  
 499          $node = $this->get_selected_node($selectortype, $element);
 500          if (!$node->isVisible()) {
 501              throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
 502          }
 503      }
 504  
 505      /**
 506       * Checks, that the existing element is not visible. Only available in tests using Javascript.
 507       *
 508       * As a "not" method, it's performance could not be good, but in this
 509       * case the performance is good because the element must exist,
 510       * otherwise there would be a ElementNotFoundException, also here we are
 511       * not spinning until the element is visible.
 512       *
 513       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
 514       * @throws ElementNotFoundException
 515       * @throws ExpectationException
 516       * @param string $element
 517       * @param string $selectortype
 518       * @return void
 519       */
 520      public function should_not_be_visible($element, $selectortype) {
 521  
 522          try {
 523              $this->should_be_visible($element, $selectortype);
 524          } catch (ExpectationException $e) {
 525              // All as expected.
 526              return;
 527          }
 528          throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
 529      }
 530  
 531      /**
 532       * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
 533       *
 534       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
 535       * @throws ElementNotFoundException
 536       * @throws DriverException
 537       * @throws ExpectationException
 538       * @param string $element Element we look for
 539       * @param string $selectortype The type of what we look for
 540       * @param string $nodeelement Element we look in
 541       * @param string $nodeselectortype The type of selector where we look in
 542       */
 543      public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
 544  
 545          if (!$this->running_javascript()) {
 546              throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
 547          }
 548  
 549          $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
 550          if (!$node->isVisible()) {
 551              throw new ExpectationException(
 552                  '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
 553                  $this->getSession()
 554              );
 555          }
 556      }
 557  
 558      /**
 559       * Checks, that the existing element is not visible inside the existing container. Only available in tests using Javascript.
 560       *
 561       * As a "not" method, it's performance could not be good, but in this
 562       * case the performance is good because the element must exist,
 563       * otherwise there would be a ElementNotFoundException, also here we are
 564       * not spinning until the element is visible.
 565       *
 566       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
 567       * @throws ElementNotFoundException
 568       * @throws ExpectationException
 569       * @param string $element Element we look for
 570       * @param string $selectortype The type of what we look for
 571       * @param string $nodeelement Element we look in
 572       * @param string $nodeselectortype The type of selector where we look in
 573       */
 574      public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
 575  
 576          try {
 577              $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
 578          } catch (ExpectationException $e) {
 579              // All as expected.
 580              return;
 581          }
 582          throw new ExpectationException(
 583              '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
 584              $this->getSession()
 585          );
 586      }
 587  
 588      /**
 589       * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
 590       *
 591       * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
 592       * @throws ExpectationException
 593       * @param string $text
 594       */
 595      public function assert_page_contains_text($text) {
 596  
 597          // Looking for all the matching nodes without any other descendant matching the
 598          // same xpath (we are using contains(., ....).
 599          $xpathliteral = behat_context_helper::escape($text);
 600          $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
 601              "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
 602  
 603          try {
 604              $nodes = $this->find_all('xpath', $xpath);
 605          } catch (ElementNotFoundException $e) {
 606              throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
 607          }
 608  
 609          // If we are not running javascript we have enough with the
 610          // element existing as we can't check if it is visible.
 611          if (!$this->running_javascript()) {
 612              return;
 613          }
 614  
 615          // We spin as we don't have enough checking that the element is there, we
 616          // should also ensure that the element is visible. Using microsleep as this
 617          // is a repeated step and global performance is important.
 618          $this->spin(
 619              function($context, $args) {
 620  
 621                  foreach ($args['nodes'] as $node) {
 622                      if ($node->isVisible()) {
 623                          return true;
 624                      }
 625                  }
 626  
 627                  // If non of the nodes is visible we loop again.
 628                  throw new ExpectationException('"' . $args['text'] . '" text was found but was not visible', $context->getSession());
 629              },
 630              array('nodes' => $nodes, 'text' => $text),
 631              false,
 632              false,
 633              true
 634          );
 635  
 636      }
 637  
 638      /**
 639       * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
 640       *
 641       * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
 642       * @throws ExpectationException
 643       * @param string $text
 644       */
 645      public function assert_page_not_contains_text($text) {
 646  
 647          // Looking for all the matching nodes without any other descendant matching the
 648          // same xpath (we are using contains(., ....).
 649          $xpathliteral = behat_context_helper::escape($text);
 650          $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
 651              "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
 652  
 653          // We should wait a while to ensure that the page is not still loading elements.
 654          // Waiting less than self::get_timeout() as we already waited for the DOM to be ready and
 655          // all JS to be executed.
 656          try {
 657              $nodes = $this->find_all('xpath', $xpath, false, false, self::get_reduced_timeout());
 658          } catch (ElementNotFoundException $e) {
 659              // All ok.
 660              return;
 661          }
 662  
 663          // If we are not running javascript we have enough with the
 664          // element existing as we can't check if it is hidden.
 665          if (!$this->running_javascript()) {
 666              throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
 667          }
 668  
 669          // If the element is there we should be sure that it is not visible.
 670          $this->spin(
 671              function($context, $args) {
 672  
 673                  foreach ($args['nodes'] as $node) {
 674                      // If element is removed from dom, then just exit.
 675                      try {
 676                          // If element is visible then throw exception, so we keep spinning.
 677                          if ($node->isVisible()) {
 678                              throw new ExpectationException('"' . $args['text'] . '" text was found in the page',
 679                                  $context->getSession());
 680                          }
 681                      } catch (NoSuchElementException $e) {
 682                          // Do nothing just return, as element is no more on page.
 683                          return true;
 684                      } catch (ElementNotFoundException $e) {
 685                          // Do nothing just return, as element is no more on page.
 686                          return true;
 687                      }
 688                  }
 689  
 690                  // If non of the found nodes is visible we consider that the text is not visible.
 691                  return true;
 692              },
 693              array('nodes' => $nodes, 'text' => $text),
 694              behat_base::get_reduced_timeout(),
 695              false,
 696              true
 697          );
 698      }
 699  
 700      /**
 701       * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
 702       *
 703       * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
 704       * @throws ElementNotFoundException
 705       * @throws ExpectationException
 706       * @param string $text
 707       * @param string $element Element we look in.
 708       * @param string $selectortype The type of element where we are looking in.
 709       */
 710      public function assert_element_contains_text($text, $element, $selectortype) {
 711  
 712          // Getting the container where the text should be found.
 713          $container = $this->get_selected_node($selectortype, $element);
 714  
 715          // Looking for all the matching nodes without any other descendant matching the
 716          // same xpath (we are using contains(., ....).
 717          $xpathliteral = behat_context_helper::escape($text);
 718          $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
 719              "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
 720  
 721          // Wait until it finds the text inside the container, otherwise custom exception.
 722          try {
 723              $nodes = $this->find_all('xpath', $xpath, false, $container);
 724          } catch (ElementNotFoundException $e) {
 725              throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
 726          }
 727  
 728          // If we are not running javascript we have enough with the
 729          // element existing as we can't check if it is visible.
 730          if (!$this->running_javascript()) {
 731              return;
 732          }
 733  
 734          // We also check the element visibility when running JS tests. Using microsleep as this
 735          // is a repeated step and global performance is important.
 736          $this->spin(
 737              function($context, $args) {
 738  
 739                  foreach ($args['nodes'] as $node) {
 740                      if ($node->isVisible()) {
 741                          return true;
 742                      }
 743                  }
 744  
 745                  throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element but was not visible', $context->getSession());
 746              },
 747              array('nodes' => $nodes, 'text' => $text, 'element' => $element),
 748              false,
 749              false,
 750              true
 751          );
 752      }
 753  
 754      /**
 755       * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
 756       *
 757       * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
 758       * @throws ElementNotFoundException
 759       * @throws ExpectationException
 760       * @param string $text
 761       * @param string $element Element we look in.
 762       * @param string $selectortype The type of element where we are looking in.
 763       */
 764      public function assert_element_not_contains_text($text, $element, $selectortype) {
 765  
 766          // Getting the container where the text should be found.
 767          $container = $this->get_selected_node($selectortype, $element);
 768  
 769          // Looking for all the matching nodes without any other descendant matching the
 770          // same xpath (we are using contains(., ....).
 771          $xpathliteral = behat_context_helper::escape($text);
 772          $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
 773              "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
 774  
 775          // We should wait a while to ensure that the page is not still loading elements.
 776          // Giving preference to the reliability of the results rather than to the performance.
 777          try {
 778              $nodes = $this->find_all('xpath', $xpath, false, $container, self::get_reduced_timeout());
 779          } catch (ElementNotFoundException $e) {
 780              // All ok.
 781              return;
 782          }
 783  
 784          // If we are not running javascript we have enough with the
 785          // element not being found as we can't check if it is visible.
 786          if (!$this->running_javascript()) {
 787              throw new ExpectationException('"' . $text . '" text was found in the "' . $element . '" element', $this->getSession());
 788          }
 789  
 790          // We need to ensure all the found nodes are hidden.
 791          $this->spin(
 792              function($context, $args) {
 793  
 794                  foreach ($args['nodes'] as $node) {
 795                      if ($node->isVisible()) {
 796                          throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element', $context->getSession());
 797                      }
 798                  }
 799  
 800                  // If all the found nodes are hidden we are happy.
 801                  return true;
 802              },
 803              array('nodes' => $nodes, 'text' => $text, 'element' => $element),
 804              behat_base::get_reduced_timeout(),
 805              false,
 806              true
 807          );
 808      }
 809  
 810      /**
 811       * Checks, that the first specified element appears before the second one.
 812       *
 813       * @Then :preelement :preselectortype should appear before :postelement :postselectortype
 814       * @Then :preelement :preselectortype should appear before :postelement :postselectortype in the :containerelement :containerselectortype
 815       * @throws ExpectationException
 816       * @param string $preelement The locator of the preceding element
 817       * @param string $preselectortype The selector type of the preceding element
 818       * @param string $postelement The locator of the latest element
 819       * @param string $postselectortype The selector type of the latest element
 820       * @param string $containerelement
 821       * @param string $containerselectortype
 822       */
 823      public function should_appear_before(
 824          string $preelement,
 825          string $preselectortype,
 826          string $postelement,
 827          string $postselectortype,
 828          ?string $containerelement = null,
 829          ?string $containerselectortype = null
 830      ) {
 831          $msg = "'{$preelement}' '{$preselectortype}' does not appear before '{$postelement}' '{$postselectortype}'";
 832          $this->check_element_order(
 833              $containerelement,
 834              $containerselectortype,
 835              $preelement,
 836              $preselectortype,
 837              $postelement,
 838              $postselectortype,
 839              $msg
 840          );
 841      }
 842  
 843      /**
 844       * Checks, that the first specified element appears after the second one.
 845       *
 846       * @Then :postelement :postselectortype should appear after :preelement :preselectortype
 847       * @Then :postelement :postselectortype should appear after :preelement :preselectortype in the :containerelement :containerselectortype
 848       * @throws ExpectationException
 849       * @param string $postelement The locator of the latest element
 850       * @param string $postselectortype The selector type of the latest element
 851       * @param string $preelement The locator of the preceding element
 852       * @param string $preselectortype The selector type of the preceding element
 853       * @param string $containerelement
 854       * @param string $containerselectortype
 855       */
 856      public function should_appear_after(
 857          string $postelement,
 858          string $postselectortype,
 859          string $preelement,
 860          string $preselectortype,
 861          ?string $containerelement = null,
 862          ?string $containerselectortype = null
 863      ) {
 864          $msg = "'{$postelement}' '{$postselectortype}' does not appear after '{$preelement}' '{$preselectortype}'";
 865          $this->check_element_order(
 866              $containerelement,
 867              $containerselectortype,
 868              $preelement,
 869              $preselectortype,
 870              $postelement,
 871              $postselectortype,
 872              $msg
 873          );
 874      }
 875  
 876      /**
 877       * Shared code to check whether an element is before or after another one.
 878       *
 879       * @param string $containerelement
 880       * @param string $containerselectortype
 881       * @param string $preelement The locator of the preceding element
 882       * @param string $preselectortype The locator of the preceding element
 883       * @param string $postelement The locator of the following element
 884       * @param string $postselectortype The selector type of the following element
 885       * @param string $msg Message to output if this fails
 886       */
 887      protected function check_element_order(
 888          ?string $containerelement,
 889          ?string $containerselectortype,
 890          string $preelement,
 891          string $preselectortype,
 892          string $postelement,
 893          string $postselectortype,
 894          string $msg
 895      ) {
 896          $containernode = false;
 897          if ($containerselectortype && $containerelement) {
 898              // Get the container node.
 899              $containernode = $this->get_selected_node($containerselectortype, $containerelement);
 900              $msg .= " in the '{$containerelement}' '{$containerselectortype}'";
 901          }
 902  
 903          list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
 904          list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
 905  
 906          $newlines = [
 907              "\r\n",
 908              "\r",
 909              "\n",
 910          ];
 911          $prexpath = str_replace($newlines, ' ', $this->find($preselector, $prelocator, false, $containernode)->getXpath());
 912          $postxpath = str_replace($newlines, ' ', $this->find($postselector, $postlocator, false, $containernode)->getXpath());
 913  
 914          if ($this->running_javascript()) {
 915              // The xpath to do this was running really slowly on certain Chrome versions so we are using
 916              // this DOM method instead.
 917              $js = <<<EOF
 918  (function() {
 919      var a = document.evaluate("{$prexpath}", document, null, XPathResult.ANY_TYPE, null).iterateNext();
 920      var b = document.evaluate("{$postxpath}", document, null, XPathResult.ANY_TYPE, null).iterateNext();
 921      return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING;
 922  })()
 923  EOF;
 924              $ok = $this->evaluate_script($js);
 925          } else {
 926  
 927              // Using following xpath axe to find it.
 928              $xpath = "{$prexpath}/following::*[contains(., {$postxpath})]";
 929              $ok = $this->getSession()->getDriver()->find($xpath);
 930          }
 931  
 932          if (!$ok) {
 933              throw new ExpectationException($msg, $this->getSession());
 934          }
 935      }
 936  
 937      /**
 938       * Checks, that element of specified type is disabled.
 939       *
 940       * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
 941       * @throws ExpectationException Thrown by behat_base::find
 942       * @param string $element Element we look in
 943       * @param string $selectortype The type of element where we are looking in.
 944       */
 945      public function the_element_should_be_disabled($element, $selectortype) {
 946          $this->the_attribute_of_should_be_set("disabled", $element, $selectortype, false);
 947      }
 948  
 949      /**
 950       * Checks, that element of specified type is enabled.
 951       *
 952       * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
 953       * @throws ExpectationException Thrown by behat_base::find
 954       * @param string $element Element we look on
 955       * @param string $selectortype The type of where we look
 956       */
 957      public function the_element_should_be_enabled($element, $selectortype) {
 958          $this->the_attribute_of_should_be_set("disabled", $element, $selectortype, true);
 959      }
 960  
 961      /**
 962       * Checks the provided element and selector type are readonly on the current page.
 963       *
 964       * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
 965       * @throws ExpectationException Thrown by behat_base::find
 966       * @param string $element Element we look in
 967       * @param string $selectortype The type of element where we are looking in.
 968       */
 969      public function the_element_should_be_readonly($element, $selectortype) {
 970          $this->the_attribute_of_should_be_set("readonly", $element, $selectortype, false);
 971      }
 972  
 973      /**
 974       * Checks the provided element and selector type are not readonly on the current page.
 975       *
 976       * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
 977       * @throws ExpectationException Thrown by behat_base::find
 978       * @param string $element Element we look in
 979       * @param string $selectortype The type of element where we are looking in.
 980       */
 981      public function the_element_should_not_be_readonly($element, $selectortype) {
 982          $this->the_attribute_of_should_be_set("readonly", $element, $selectortype, true);
 983      }
 984  
 985      /**
 986       * Checks the provided element and selector type exists in the current page.
 987       *
 988       * This step is for advanced users, use it if you don't find anything else suitable for what you need.
 989       *
 990       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist$/
 991       * @throws ElementNotFoundException Thrown by behat_base::find
 992       * @param string $element The locator of the specified selector
 993       * @param string $selectortype The selector type
 994       */
 995      public function should_exist($element, $selectortype) {
 996          // Will throw an ElementNotFoundException if it does not exist.
 997          $this->find($selectortype, $element);
 998      }
 999  
1000      /**
1001       * Checks that the provided element and selector type not exists in the current page.
1002       *
1003       * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1004       *
1005       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist$/
1006       * @throws ExpectationException
1007       * @param string $element The locator of the specified selector
1008       * @param string $selectortype The selector type
1009       */
1010      public function should_not_exist($element, $selectortype) {
1011          // Will throw an ElementNotFoundException if it does not exist, but, actually it should not exist, so we try &
1012          // catch it.
1013          try {
1014              // The exception does not really matter as we will catch it and will never "explode".
1015              $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $element);
1016  
1017              // Using the spin method as we want a reduced timeout but there is no need for a 0.1 seconds interval
1018              // because in the optimistic case we will timeout.
1019              // If all goes good it will throw an ElementNotFoundExceptionn that we will catch.
1020              $this->find($selectortype, $element, $exception, false, behat_base::get_reduced_timeout());
1021          } catch (ElementNotFoundException $e) {
1022              // We expect the element to not be found.
1023              return;
1024          }
1025  
1026          // The element was found and should not have been. Throw an exception.
1027          throw new ExpectationException("The '{$element}' '{$selectortype}' exists in the current page", $this->getSession());
1028      }
1029  
1030      /**
1031       * This step triggers cron like a user would do going to admin/cron.php.
1032       *
1033       * @Given /^I trigger cron$/
1034       */
1035      public function i_trigger_cron() {
1036          $this->execute('behat_general::i_visit', ['/admin/cron.php']);
1037      }
1038  
1039      /**
1040       * Runs a scheduled task immediately, given full class name.
1041       *
1042       * This is faster and more reliable than running cron (running cron won't
1043       * work more than once in the same test, for instance). However it is
1044       * a little less 'realistic'.
1045       *
1046       * While the task is running, we suppress mtrace output because it makes
1047       * the Behat result look ugly.
1048       *
1049       * Note: Most of the code relating to running a task is based on
1050       * admin/tool/task/cli/schedule_task.php.
1051       *
1052       * @Given /^I run the scheduled task "(?P<task_name>[^"]+)"$/
1053       * @param string $taskname Name of task e.g. 'mod_whatever\task\do_something'
1054       */
1055      public function i_run_the_scheduled_task($taskname) {
1056          global $CFG;
1057          require_once("{$CFG->libdir}/cronlib.php");
1058  
1059          $task = \core\task\manager::get_scheduled_task($taskname);
1060          if (!$task) {
1061              throw new DriverException('The "' . $taskname . '" scheduled task does not exist');
1062          }
1063  
1064          // Do setup for cron task.
1065          raise_memory_limit(MEMORY_EXTRA);
1066          cron_setup_user();
1067  
1068          // Get lock.
1069          $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
1070          if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
1071              throw new DriverException('Unable to obtain core_cron lock for scheduled task');
1072          }
1073          if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
1074              $cronlock->release();
1075              throw new DriverException('Unable to obtain task lock for scheduled task');
1076          }
1077          $task->set_lock($lock);
1078          if (!$task->is_blocking()) {
1079              $cronlock->release();
1080          } else {
1081              $task->set_cron_lock($cronlock);
1082          }
1083  
1084          try {
1085              // Prepare the renderer.
1086              cron_prepare_core_renderer();
1087  
1088              // Discard task output as not appropriate for Behat output!
1089              ob_start();
1090              $task->execute();
1091              ob_end_clean();
1092  
1093              // Restore the previous renderer.
1094              cron_prepare_core_renderer(true);
1095  
1096              // Mark task complete.
1097              \core\task\manager::scheduled_task_complete($task);
1098          } catch (Exception $e) {
1099              // Restore the previous renderer.
1100              cron_prepare_core_renderer(true);
1101  
1102              // Mark task failed and throw exception.
1103              \core\task\manager::scheduled_task_failed($task);
1104  
1105              throw new DriverException('The "' . $taskname . '" scheduled task failed', 0, $e);
1106          }
1107      }
1108  
1109      /**
1110       * Runs all ad-hoc tasks in the queue.
1111       *
1112       * This is faster and more reliable than running cron (running cron won't
1113       * work more than once in the same test, for instance). However it is
1114       * a little less 'realistic'.
1115       *
1116       * While the task is running, we suppress mtrace output because it makes
1117       * the Behat result look ugly.
1118       *
1119       * @Given /^I run all adhoc tasks$/
1120       * @throws DriverException
1121       */
1122      public function i_run_all_adhoc_tasks() {
1123          global $CFG, $DB;
1124          require_once("{$CFG->libdir}/cronlib.php");
1125  
1126          // Do setup for cron task.
1127          cron_setup_user();
1128  
1129          // Discard task output as not appropriate for Behat output!
1130          ob_start();
1131  
1132          // Run all tasks which have a scheduled runtime of before now.
1133          $timenow = time();
1134  
1135          while (!\core\task\manager::static_caches_cleared_since($timenow) &&
1136                  $task = \core\task\manager::get_next_adhoc_task($timenow)) {
1137              // Clean the output buffer between tasks.
1138              ob_clean();
1139  
1140              // Run the task.
1141              cron_run_inner_adhoc_task($task);
1142  
1143              // Check whether the task record still exists.
1144              // If a task was successful it will be removed.
1145              // If it failed then it will still exist.
1146              if ($DB->record_exists('task_adhoc', ['id' => $task->get_id()])) {
1147                  // End ouptut buffering and flush the current buffer.
1148                  // This should be from just the current task.
1149                  ob_end_flush();
1150  
1151                  throw new DriverException('An adhoc task failed', 0);
1152              }
1153          }
1154          ob_end_clean();
1155      }
1156  
1157      /**
1158       * Checks that an element and selector type exists in another element and selector type on the current page.
1159       *
1160       * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1161       *
1162       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
1163       * @throws ElementNotFoundException Thrown by behat_base::find
1164       * @param string $element The locator of the specified selector
1165       * @param string $selectortype The selector type
1166       * @param NodeElement|string $containerelement The locator of the container selector
1167       * @param string $containerselectortype The container selector type
1168       */
1169      public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
1170          // Will throw an ElementNotFoundException if it does not exist.
1171          $this->get_node_in_container($selectortype, $element, $containerselectortype, $containerelement);
1172      }
1173  
1174      /**
1175       * Checks that an element and selector type does not exist in another element and selector type on the current page.
1176       *
1177       * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1178       *
1179       * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
1180       * @throws ExpectationException
1181       * @param string $element The locator of the specified selector
1182       * @param string $selectortype The selector type
1183       * @param NodeElement|string $containerelement The locator of the container selector
1184       * @param string $containerselectortype The container selector type
1185       */
1186      public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
1187          // Get the container node.
1188          $containernode = $this->find($containerselectortype, $containerelement);
1189  
1190          // Will throw an ElementNotFoundException if it does not exist, but, actually it should not exist, so we try &
1191          // catch it.
1192          try {
1193              // Looks for the requested node inside the container node.
1194              $this->find($selectortype, $element, false, $containernode, behat_base::get_reduced_timeout());
1195          } catch (ElementNotFoundException $e) {
1196              // We expect the element to not be found.
1197              return;
1198          }
1199  
1200          // The element was found and should not have been. Throw an exception.
1201          $elementdescription = $this->get_selector_description($selectortype, $element);
1202          $containerdescription = $this->get_selector_description($containerselectortype, $containerelement);
1203          throw new ExpectationException(
1204              "The {$elementdescription} exists in the {$containerdescription}",
1205              $this->getSession()
1206          );
1207      }
1208  
1209      /**
1210       * Change browser window size small: 640x480, medium: 1024x768, large: 2560x1600, custom: widthxheight
1211       *
1212       * Example: I change window size to "small" or I change window size to "1024x768"
1213       * or I change viewport size to "800x600". The viewport option is useful to guarantee that the
1214       * browser window has same viewport size even when you run Behat on multiple operating systems.
1215       *
1216       * @throws ExpectationException
1217       * @Then /^I change (window|viewport) size to "(small|medium|large|\d+x\d+)"$/
1218       * @Then /^I change the (window|viewport) size to "(small|medium|large|\d+x\d+)"$/
1219       * @param string $windowsize size of the window (small|medium|large|wxh).
1220       */
1221      public function i_change_window_size_to($windowviewport, $windowsize) {
1222          $this->resize_window($windowsize, $windowviewport === 'viewport');
1223      }
1224  
1225      /**
1226       * Checks whether there the specified attribute is set or not.
1227       *
1228       * @Then the :attribute attribute of :element :selectortype should be set
1229       * @Then the :attribute attribute of :element :selectortype should :not be set
1230       *
1231       * @throws ExpectationException
1232       * @param string $attribute Name of attribute
1233       * @param string $element The locator of the specified selector
1234       * @param string $selectortype The selector type
1235       * @param string $not
1236       */
1237      public function the_attribute_of_should_be_set($attribute, $element, $selectortype, $not = null) {
1238          // Get the container node (exception if it doesn't exist).
1239          $containernode = $this->get_selected_node($selectortype, $element);
1240          $hasattribute = $containernode->hasAttribute($attribute);
1241  
1242          if ($not && $hasattribute) {
1243              $value = $containernode->getAttribute($attribute);
1244              // Should not be set but is.
1245              throw new ExpectationException(
1246                  "The attribute \"{$attribute}\" should not be set but has a value of '{$value}'",
1247                  $this->getSession()
1248              );
1249          } else if (!$not && !$hasattribute) {
1250              // Should be set but is not.
1251              throw new ExpectationException(
1252                  "The attribute \"{$attribute}\" should be set but is not",
1253                  $this->getSession()
1254              );
1255          }
1256      }
1257  
1258      /**
1259       * Checks whether there is an attribute on the given element that contains the specified text.
1260       *
1261       * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should contain "(?P<text_string>(?:[^"]|\\")*)"$/
1262       * @throws ExpectationException
1263       * @param string $attribute Name of attribute
1264       * @param string $element The locator of the specified selector
1265       * @param string $selectortype The selector type
1266       * @param string $text Expected substring
1267       */
1268      public function the_attribute_of_should_contain($attribute, $element, $selectortype, $text) {
1269          // Get the container node (exception if it doesn't exist).
1270          $containernode = $this->get_selected_node($selectortype, $element);
1271          $value = $containernode->getAttribute($attribute);
1272          if ($value == null) {
1273              throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
1274                      $this->getSession());
1275          } else if (strpos($value, $text) === false) {
1276              throw new ExpectationException('The attribute "' . $attribute .
1277                      '" does not contain "' . $text . '" (actual value: "' . $value . '")',
1278                      $this->getSession());
1279          }
1280      }
1281  
1282      /**
1283       * Checks that the attribute on the given element does not contain the specified text.
1284       *
1285       * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not contain "(?P<text_string>(?:[^"]|\\")*)"$/
1286       * @throws ExpectationException
1287       * @param string $attribute Name of attribute
1288       * @param string $element The locator of the specified selector
1289       * @param string $selectortype The selector type
1290       * @param string $text Expected substring
1291       */
1292      public function the_attribute_of_should_not_contain($attribute, $element, $selectortype, $text) {
1293          // Get the container node (exception if it doesn't exist).
1294          $containernode = $this->get_selected_node($selectortype, $element);
1295          $value = $containernode->getAttribute($attribute);
1296          if ($value == null) {
1297              throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
1298                      $this->getSession());
1299          } else if (strpos($value, $text) !== false) {
1300              throw new ExpectationException('The attribute "' . $attribute .
1301                      '" contains "' . $text . '" (value: "' . $value . '")',
1302                      $this->getSession());
1303          }
1304      }
1305  
1306      /**
1307       * Checks the provided value exists in specific row/column of table.
1308       *
1309       * @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should contain "(?P<value_string>[^"]*)"$/
1310       * @throws ElementNotFoundException
1311       * @param string $row row text which will be looked in.
1312       * @param string $column column text to search (or numeric value for the column position)
1313       * @param string $table table id/class/caption
1314       * @param string $value text to check.
1315       */
1316      public function row_column_of_table_should_contain($row, $column, $table, $value) {
1317          $tablenode = $this->get_selected_node('table', $table);
1318          $tablexpath = $tablenode->getXpath();
1319  
1320          $rowliteral = behat_context_helper::escape($row);
1321          $valueliteral = behat_context_helper::escape($value);
1322          $columnliteral = behat_context_helper::escape($column);
1323  
1324          if (preg_match('/^-?(\d+)-?$/', $column, $columnasnumber)) {
1325              // Column indicated as a number, just use it as position of the column.
1326              $columnpositionxpath = "/child::*[position() = {$columnasnumber[1]}]";
1327          } else {
1328              // Header can be in thead or tbody (first row), following xpath should work.
1329              $theadheaderxpath = "thead/tr[1]/th[(normalize-space(.)=" . $columnliteral . " or a[normalize-space(text())=" .
1330                      $columnliteral . "] or div[normalize-space(text())=" . $columnliteral . "])]";
1331              $tbodyheaderxpath = "tbody/tr[1]/td[(normalize-space(.)=" . $columnliteral . " or a[normalize-space(text())=" .
1332                      $columnliteral . "] or div[normalize-space(text())=" . $columnliteral . "])]";
1333  
1334              // Check if column exists.
1335              $columnheaderxpath = $tablexpath . "[" . $theadheaderxpath . " | " . $tbodyheaderxpath . "]";
1336              $columnheader = $this->getSession()->getDriver()->find($columnheaderxpath);
1337              if (empty($columnheader)) {
1338                  $columnexceptionmsg = $column . '" in table "' . $table . '"';
1339                  throw new ElementNotFoundException($this->getSession(), "\n$columnheaderxpath\n\n".'Column', null, $columnexceptionmsg);
1340              }
1341              // Following conditions were considered before finding column count.
1342              // 1. Table header can be in thead/tr/th or tbody/tr/td[1].
1343              // 2. First column can have th (Gradebook -> user report), so having lenient sibling check.
1344              $columnpositionxpath = "/child::*[position() = count(" . $tablexpath . "/" . $theadheaderxpath .
1345                  "/preceding-sibling::*) + 1]";
1346          }
1347  
1348          // Check if value exists in specific row/column.
1349          // Get row xpath.
1350          // GoutteDriver uses DomCrawler\Crawler and it is making XPath relative to the current context, so use descendant.
1351          $rowxpath = $tablexpath."/tbody/tr[descendant::th[normalize-space(.)=" . $rowliteral .
1352                      "] | descendant::td[normalize-space(.)=" . $rowliteral . "]]";
1353  
1354          $columnvaluexpath = $rowxpath . $columnpositionxpath . "[contains(normalize-space(.)," . $valueliteral . ")]";
1355  
1356          // Looks for the requested node inside the container node.
1357          $coumnnode = $this->getSession()->getDriver()->find($columnvaluexpath);
1358          if (empty($coumnnode)) {
1359              $locatorexceptionmsg = $value . '" in "' . $row . '" row with column "' . $column;
1360              throw new ElementNotFoundException($this->getSession(), "\n$columnvaluexpath\n\n".'Column value', null, $locatorexceptionmsg);
1361          }
1362      }
1363  
1364      /**
1365       * Checks the provided value should not exist in specific row/column of table.
1366       *
1367       * @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should not contain "(?P<value_string>[^"]*)"$/
1368       * @throws ElementNotFoundException
1369       * @param string $row row text which will be looked in.
1370       * @param string $column column text to search
1371       * @param string $table table id/class/caption
1372       * @param string $value text to check.
1373       */
1374      public function row_column_of_table_should_not_contain($row, $column, $table, $value) {
1375          try {
1376              $this->row_column_of_table_should_contain($row, $column, $table, $value);
1377          } catch (ElementNotFoundException $e) {
1378              // Table row/column doesn't contain this value. Nothing to do.
1379              return;
1380          }
1381          // Throw exception if found.
1382          throw new ExpectationException(
1383              '"' . $column . '" with value "' . $value . '" is present in "' . $row . '"  row for table "' . $table . '"',
1384              $this->getSession()
1385          );
1386      }
1387  
1388      /**
1389       * Checks that the provided value exist in table.
1390       *
1391       * First row may contain column headers or numeric indexes of the columns
1392       * (syntax -1- is also considered to be column index). Column indexes are
1393       * useful in case of multirow headers and/or presence of cells with colspan.
1394       *
1395       * @Then /^the following should exist in the "(?P<table_string>[^"]*)" table:$/
1396       * @throws ExpectationException
1397       * @param string $table name of table
1398       * @param TableNode $data table with first row as header and following values
1399       *        | Header 1 | Header 2 | Header 3 |
1400       *        | Value 1 | Value 2 | Value 3|
1401       */
1402      public function following_should_exist_in_the_table($table, TableNode $data) {
1403          $datahash = $data->getHash();
1404  
1405          foreach ($datahash as $row) {
1406              $firstcell = null;
1407              foreach ($row as $column => $value) {
1408                  if ($firstcell === null) {
1409                      $firstcell = $value;
1410                  } else {
1411                      $this->row_column_of_table_should_contain($firstcell, $column, $table, $value);
1412                  }
1413              }
1414          }
1415      }
1416  
1417      /**
1418       * Checks that the provided values do not exist in a table.
1419       *
1420       * @Then /^the following should not exist in the "(?P<table_string>[^"]*)" table:$/
1421       * @throws ExpectationException
1422       * @param string $table name of table
1423       * @param TableNode $data table with first row as header and following values
1424       *        | Header 1 | Header 2 | Header 3 |
1425       *        | Value 1 | Value 2 | Value 3|
1426       */
1427      public function following_should_not_exist_in_the_table($table, TableNode $data) {
1428          $datahash = $data->getHash();
1429  
1430          foreach ($datahash as $value) {
1431              $row = array_shift($value);
1432              foreach ($value as $column => $value) {
1433                  try {
1434                      $this->row_column_of_table_should_contain($row, $column, $table, $value);
1435                      // Throw exception if found.
1436                  } catch (ElementNotFoundException $e) {
1437                      // Table row/column doesn't contain this value. Nothing to do.
1438                      continue;
1439                  }
1440                  throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
1441                      $row . '"  row for table "' . $table . '"', $this->getSession()
1442                  );
1443              }
1444          }
1445      }
1446  
1447      /**
1448       * Given the text of a link, download the linked file and return the contents.
1449       *
1450       * This is a helper method used by {@link following_should_download_bytes()}
1451       * and {@link following_should_download_between_and_bytes()}
1452       *
1453       * @param string $link the text of the link.
1454       * @return string the content of the downloaded file.
1455       */
1456      public function download_file_from_link($link) {
1457          // Find the link.
1458          $linknode = $this->find_link($link);
1459          $this->ensure_node_is_visible($linknode);
1460  
1461          // Get the href and check it.
1462          $url = $linknode->getAttribute('href');
1463          if (!$url) {
1464              throw new ExpectationException('Download link does not have href attribute',
1465                      $this->getSession());
1466          }
1467          if (!preg_match('~^https?://~', $url)) {
1468              throw new ExpectationException('Download link not an absolute URL: ' . $url,
1469                      $this->getSession());
1470          }
1471  
1472          // Download the URL and check the size.
1473          $session = $this->getSession()->getCookie('MoodleSession');
1474          return download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
1475      }
1476  
1477      /**
1478       * Downloads the file from a link on the page and checks the size.
1479       *
1480       * Only works if the link has an href attribute. Javascript downloads are
1481       * not supported. Currently, the href must be an absolute URL.
1482       *
1483       * @Then /^following "(?P<link_string>[^"]*)" should download "(?P<expected_bytes>\d+)" bytes$/
1484       * @throws ExpectationException
1485       * @param string $link the text of the link.
1486       * @param number $expectedsize the expected file size in bytes.
1487       */
1488      public function following_should_download_bytes($link, $expectedsize) {
1489          $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
1490  
1491          // It will stop spinning once file is downloaded or time out.
1492          $result = $this->spin(
1493              function($context, $args) {
1494                  $link = $args['link'];
1495                  return $this->download_file_from_link($link);
1496              },
1497              array('link' => $link),
1498              behat_base::get_extended_timeout(),
1499              $exception
1500          );
1501  
1502          // Check download size.
1503          $actualsize = (int)strlen($result);
1504          if ($actualsize !== (int)$expectedsize) {
1505              throw new ExpectationException('Downloaded data was ' . $actualsize .
1506                      ' bytes, expecting ' . $expectedsize, $this->getSession());
1507          }
1508      }
1509  
1510      /**
1511       * Downloads the file from a link on the page and checks the size is in a given range.
1512       *
1513       * Only works if the link has an href attribute. Javascript downloads are
1514       * not supported. Currently, the href must be an absolute URL.
1515       *
1516       * The range includes the endpoints. That is, a 10 byte file in considered to
1517       * be between "5" and "10" bytes, and between "10" and "20" bytes.
1518       *
1519       * @Then /^following "(?P<link_string>[^"]*)" should download between "(?P<min_bytes>\d+)" and "(?P<max_bytes>\d+)" bytes$/
1520       * @throws ExpectationException
1521       * @param string $link the text of the link.
1522       * @param number $minexpectedsize the minimum expected file size in bytes.
1523       * @param number $maxexpectedsize the maximum expected file size in bytes.
1524       */
1525      public function following_should_download_between_and_bytes($link, $minexpectedsize, $maxexpectedsize) {
1526          // If the minimum is greater than the maximum then swap the values.
1527          if ((int)$minexpectedsize > (int)$maxexpectedsize) {
1528              list($minexpectedsize, $maxexpectedsize) = array($maxexpectedsize, $minexpectedsize);
1529          }
1530  
1531          $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
1532  
1533          // It will stop spinning once file is downloaded or time out.
1534          $result = $this->spin(
1535              function($context, $args) {
1536                  $link = $args['link'];
1537  
1538                  return $this->download_file_from_link($link);
1539              },
1540              array('link' => $link),
1541              behat_base::get_extended_timeout(),
1542              $exception
1543          );
1544  
1545          // Check download size.
1546          $actualsize = (int)strlen($result);
1547          if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) {
1548              throw new ExpectationException('Downloaded data was ' . $actualsize .
1549                      ' bytes, expecting between ' . $minexpectedsize . ' and ' .
1550                      $maxexpectedsize, $this->getSession());
1551          }
1552      }
1553  
1554      /**
1555       * Checks that the image on the page is the same as one of the fixture files
1556       *
1557       * @Then /^the image at "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be identical to "(?P<filepath_string>(?:[^"]|\\")*)"$/
1558       * @throws ExpectationException
1559       * @param string $element The locator of the image
1560       * @param string $selectortype The selector type
1561       * @param string $filepath path to the fixture file
1562       */
1563      public function the_image_at_should_be_identical_to($element, $selectortype, $filepath) {
1564          global $CFG;
1565  
1566          // Get the container node (exception if it doesn't exist).
1567          $containernode = $this->get_selected_node($selectortype, $element);
1568          $url = $containernode->getAttribute('src');
1569          if ($url == null) {
1570              throw new ExpectationException('Element does not have src attribute',
1571                  $this->getSession());
1572          }
1573          $session = $this->getSession()->getCookie('MoodleSession');
1574          $content = download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
1575  
1576          // Get the content of the fixture file.
1577          // Replace 'admin/' if it is in start of path with $CFG->admin .
1578          if (substr($filepath, 0, 6) === 'admin/') {
1579              $filepath = $CFG->admin . DIRECTORY_SEPARATOR . substr($filepath, 6);
1580          }
1581          $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
1582          $filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
1583          if (!is_readable($filepath)) {
1584              throw new ExpectationException('The file to compare to does not exist.', $this->getSession());
1585          }
1586          $expectedcontent = file_get_contents($filepath);
1587  
1588          if ($content !== $expectedcontent) {
1589              throw new ExpectationException('Image is not identical to the fixture. Received ' .
1590              strlen($content) . ' bytes and expected ' . strlen($expectedcontent) . ' bytes', $this->getSession());
1591          }
1592      }
1593  
1594      /**
1595       * Prepare to detect whether or not a new page has loaded (or the same page reloaded) some time in the future.
1596       *
1597       * @Given /^I start watching to see if a new page loads$/
1598       */
1599      public function i_start_watching_to_see_if_a_new_page_loads() {
1600          if (!$this->running_javascript()) {
1601              throw new DriverException('Page load detection requires JavaScript.');
1602          }
1603  
1604          $session = $this->getSession();
1605  
1606          if ($this->pageloaddetectionrunning || $session->getPage()->find('xpath', $this->get_page_load_xpath())) {
1607              // If we find this node at this point we are already watching for a reload and the behat steps
1608              // are out of order. We will treat this as an error - really it needs to be fixed as it indicates a problem.
1609              throw new ExpectationException(
1610                  'Page load expectation error: page reloads are already been watched for.', $session);
1611          }
1612  
1613          $this->pageloaddetectionrunning = true;
1614  
1615          $this->execute_script(
1616              'var span = document.createElement("span");
1617              span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '");
1618              span.setAttribute("style", "display: none;");
1619              document.body.appendChild(span);'
1620          );
1621      }
1622  
1623      /**
1624       * Verify that a new page has loaded (or the same page has reloaded) since the
1625       * last "I start watching to see if a new page loads" step.
1626       *
1627       * @Given /^a new page should have loaded since I started watching$/
1628       */
1629      public function a_new_page_should_have_loaded_since_i_started_watching() {
1630          $session = $this->getSession();
1631  
1632          // Make sure page load tracking was started.
1633          if (!$this->pageloaddetectionrunning) {
1634              throw new ExpectationException(
1635                  'Page load expectation error: page load tracking was not started.', $session);
1636          }
1637  
1638          // As the node is inserted by code above it is either there or not, and we do not need spin and it is safe
1639          // to use the native API here which is great as exception handling (the alternative is slow).
1640          if ($session->getPage()->find('xpath', $this->get_page_load_xpath())) {
1641              // We don't want to find this node, if we do we have an error.
1642              throw new ExpectationException(
1643                  'Page load expectation error: a new page has not been loaded when it should have been.', $session);
1644          }
1645  
1646          // Cancel the tracking of pageloaddetectionrunning.
1647          $this->pageloaddetectionrunning = false;
1648      }
1649  
1650      /**
1651       * Verify that a new page has not loaded (or the same page has reloaded) since the
1652       * last "I start watching to see if a new page loads" step.
1653       *
1654       * @Given /^a new page should not have loaded since I started watching$/
1655       */
1656      public function a_new_page_should_not_have_loaded_since_i_started_watching() {
1657          $session = $this->getSession();
1658  
1659          // Make sure page load tracking was started.
1660          if (!$this->pageloaddetectionrunning) {
1661              throw new ExpectationException(
1662                  'Page load expectation error: page load tracking was not started.', $session);
1663          }
1664  
1665          // We use our API here as we can use the exception handling provided by it.
1666          $this->find(
1667              'xpath',
1668              $this->get_page_load_xpath(),
1669              new ExpectationException(
1670                  'Page load expectation error: A new page has been loaded when it should not have been.',
1671                  $this->getSession()
1672              )
1673          );
1674      }
1675  
1676      /**
1677       * Helper used by {@link a_new_page_should_have_loaded_since_i_started_watching}
1678       * and {@link a_new_page_should_not_have_loaded_since_i_started_watching}
1679       * @return string xpath expression.
1680       */
1681      protected function get_page_load_xpath() {
1682          return "//span[@data-rel = '" . self::PAGE_LOAD_DETECTION_STRING . "']";
1683      }
1684  
1685      /**
1686       * Wait unit user press Enter/Return key. Useful when debugging a scenario.
1687       *
1688       * @Then /^(?:|I )pause(?:| scenario execution)$/
1689       */
1690      public function i_pause_scenario_execution() {
1691          $message = "<colour:lightYellow>Paused. Press <colour:lightRed>Enter/Return<colour:lightYellow> to continue.";
1692          behat_util::pause($this->getSession(), $message);
1693      }
1694  
1695      /**
1696       * Presses a given button in the browser.
1697       * NOTE: Phantomjs and goutte driver reloads page while navigating back and forward.
1698       *
1699       * @Then /^I press the "(back|forward|reload)" button in the browser$/
1700       * @param string $button the button to press.
1701       * @throws ExpectationException
1702       */
1703      public function i_press_in_the_browser($button) {
1704          $session = $this->getSession();
1705  
1706          if ($button == 'back') {
1707              $session->back();
1708          } else if ($button == 'forward') {
1709              $session->forward();
1710          } else if ($button == 'reload') {
1711              $session->reload();
1712          } else {
1713              throw new ExpectationException('Unknown browser button.', $session);
1714          }
1715      }
1716  
1717      /**
1718       * Send key presses to the browser without first changing focusing, or applying the key presses to a specific
1719       * element.
1720       *
1721       * Example usage of this step:
1722       *     When I type "Penguin"
1723       *
1724       * @When    I type :keys
1725       * @param   string $keys The key, or list of keys, to type
1726       */
1727      public function i_type(string $keys): void {
1728          // Certain keys, such as the newline character, must be converted to the appropriate character code.
1729          // Without this, keys will behave differently depending on the browser.
1730          $keylist = array_map(function($key): string {
1731              switch ($key) {
1732                  case "\n":
1733                      return behat_keys::ENTER;
1734                  default:
1735                      return $key;
1736              }
1737          }, str_split($keys));
1738          behat_base::type_keys($this->getSession(), $keylist);
1739      }
1740  
1741      /**
1742       * Press a named or character key with an optional set of modifiers.
1743       *
1744       * Supported named keys are:
1745       * - up
1746       * - down
1747       * - left
1748       * - right
1749       * - pageup|page_up
1750       * - pagedown|page_down
1751       * - home
1752       * - end
1753       * - insert
1754       * - delete
1755       * - backspace
1756       * - escape
1757       * - enter
1758       * - tab
1759       *
1760       * You can also use a single character for the key name e.g. 'Ctrl C'.
1761       *
1762       * Supported moderators are:
1763       * - shift
1764       * - ctrl
1765       * - alt
1766       * - meta
1767       *
1768       * Example usage of this new step:
1769       *     When I press the up key
1770       *     When I press the space key
1771       *     When I press the shift tab key
1772       *
1773       * Multiple moderator keys can be combined using the '+' operator, for example:
1774       *     When I press the ctrl+shift enter key
1775       *     When I press the ctrl + shift enter key
1776       *
1777       * @When    /^I press the (?P<modifiers_string>.* )?(?P<key_string>.*) key$/
1778       * @param   string $modifiers A list of keyboard modifiers, separated by the `+` character
1779       * @param   string $key The name of the key to press
1780       */
1781      public function i_press_named_key(string $modifiers, string $key): void {
1782          behat_base::require_javascript_in_session($this->getSession());
1783  
1784          $keys = [];
1785  
1786          foreach (explode('+', $modifiers) as $modifier) {
1787              switch (strtoupper(trim($modifier))) {
1788                  case '':
1789                      break;
1790                  case 'SHIFT':
1791                      $keys[] = behat_keys::SHIFT;
1792                      break;
1793                  case 'CTRL':
1794                      $keys[] = behat_keys::CONTROL;
1795                      break;
1796                  case 'ALT':
1797                      $keys[] = behat_keys::ALT;
1798                      break;
1799                  case 'META':
1800                      $keys[] = behat_keys::META;
1801                      break;
1802                  default:
1803                      throw new \coding_exception("Unknown modifier key '$modifier'}");
1804              }
1805          }
1806  
1807          $modifier = trim($key);
1808          switch (strtoupper($key)) {
1809              case 'UP':
1810                  $keys[] = behat_keys::ARROW_UP;
1811                  break;
1812              case 'DOWN':
1813                  $keys[] = behat_keys::ARROW_DOWN;
1814                  break;
1815              case 'LEFT':
1816                  $keys[] = behat_keys::ARROW_LEFT;
1817                  break;
1818              case 'RIGHT':
1819                  $keys[] = behat_keys::ARROW_RIGHT;
1820                  break;
1821              case 'HOME':
1822                  $keys[] = behat_keys::HOME;
1823                  break;
1824              case 'END':
1825                  $keys[] = behat_keys::END;
1826                  break;
1827              case 'INSERT':
1828                  $keys[] = behat_keys::INSERT;
1829                  break;
1830              case 'BACKSPACE':
1831                  $keys[] = behat_keys::BACKSPACE;
1832                  break;
1833              case 'DELETE':
1834                  $keys[] = behat_keys::DELETE;
1835                  break;
1836              case 'PAGEUP':
1837              case 'PAGE_UP':
1838                  $keys[] = behat_keys::PAGE_UP;
1839                  break;
1840              case 'PAGEDOWN':
1841              case 'PAGE_DOWN':
1842                  $keys[] = behat_keys::PAGE_DOWN;
1843                  break;
1844              case 'ESCAPE':
1845                  $keys[] = behat_keys::ESCAPE;
1846                  break;
1847              case 'ENTER':
1848                  $keys[] = behat_keys::ENTER;
1849                  break;
1850              case 'TAB':
1851                  $keys[] = behat_keys::TAB;
1852                  break;
1853              case 'SPACE':
1854                  $keys[] = behat_keys::SPACE;
1855                  break;
1856              default:
1857                  // You can enter a single ASCII character (e.g. a letter) to directly type that key.
1858                  if (strlen($key) === 1) {
1859                      $keys[] = strtolower($key);
1860                  } else {
1861                      throw new \coding_exception("Unknown key '$key'}");
1862                  }
1863          }
1864  
1865          behat_base::type_keys($this->getSession(), $keys);
1866      }
1867  
1868      /**
1869       * Trigger a keydown event for a key on a specific element.
1870       *
1871       * @When /^I press key "(?P<key_string>(?:[^"]|\\")*)" in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
1872       * @param string $key either char-code or character itself,
1873       *               may optionally be prefixed with ctrl-, alt-, shift- or meta-
1874       * @param string $element Element we look for
1875       * @param string $selectortype The type of what we look for
1876       * @throws DriverException
1877       * @throws ExpectationException
1878       */
1879      public function i_press_key_in_element($key, $element, $selectortype) {
1880          if (!$this->running_javascript()) {
1881              throw new DriverException('Key down step is not available with Javascript disabled');
1882          }
1883          // Gets the node based on the requested selector type and locator.
1884          $node = $this->get_selected_node($selectortype, $element);
1885          $modifier = null;
1886          $validmodifiers = array('ctrl', 'alt', 'shift', 'meta');
1887          $char = $key;
1888          if (strpos($key, '-')) {
1889              list($modifier, $char) = preg_split('/-/', $key, 2);
1890              $modifier = strtolower($modifier);
1891              if (!in_array($modifier, $validmodifiers)) {
1892                  throw new ExpectationException(sprintf('Unknown key modifier: %s.', $modifier),
1893                      $this->getSession());
1894              }
1895          }
1896          if (is_numeric($char)) {
1897              $char = (int)$char;
1898          }
1899  
1900          $node->keyDown($char, $modifier);
1901          $node->keyPress($char, $modifier);
1902          $node->keyUp($char, $modifier);
1903      }
1904  
1905      /**
1906       * Press tab key on a specific element.
1907       *
1908       * @When /^I press tab key in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
1909       * @param string $element Element we look for
1910       * @param string $selectortype The type of what we look for
1911       * @throws DriverException
1912       * @throws ExpectationException
1913       */
1914      public function i_post_tab_key_in_element($element, $selectortype) {
1915          if (!$this->running_javascript()) {
1916              throw new DriverException('Tab press step is not available with Javascript disabled');
1917          }
1918          // Gets the node based on the requested selector type and locator.
1919          $node = $this->get_selected_node($selectortype, $element);
1920          $this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
1921          $this->execute('behat_general::i_press_named_key', ['', 'tab']);
1922      }
1923  
1924      /**
1925       * Checks if database family used is using one of the specified, else skip. (mysql, postgres, mssql, oracle, etc.)
1926       *
1927       * @Given /^database family used is one of the following:$/
1928       * @param TableNode $databasefamilies list of database.
1929       * @return void.
1930       * @throws \Moodle\BehatExtension\Exception\SkippedException
1931       */
1932      public function database_family_used_is_one_of_the_following(TableNode $databasefamilies) {
1933          global $DB;
1934  
1935          $dbfamily = $DB->get_dbfamily();
1936  
1937          // Check if used db family is one of the specified ones. If yes then return.
1938          foreach ($databasefamilies->getRows() as $dbfamilytocheck) {
1939              if ($dbfamilytocheck[0] == $dbfamily) {
1940                  return;
1941              }
1942          }
1943  
1944          throw new \Moodle\BehatExtension\Exception\SkippedException();
1945      }
1946  
1947      /**
1948       * Checks if given plugin is installed, and skips the current scenario if not.
1949       *
1950       * @Given the :plugin plugin is installed
1951       * @param string $plugin frankenstyle plugin name, e.g. 'filter_embedquestion'.
1952       * @throws \Moodle\BehatExtension\Exception\SkippedException
1953       */
1954      public function plugin_is_installed(string $plugin): void {
1955          $path = core_component::get_component_directory($plugin);
1956          if (!is_readable($path . '/version.php')) {
1957              throw new \Moodle\BehatExtension\Exception\SkippedException(
1958                      'Skipping this scenario because the ' . $plugin . ' is not installed.');
1959          }
1960      }
1961  
1962      /**
1963       * Checks focus is with the given element.
1964       *
1965       * @Then /^the focused element is( not)? "(?P<node_string>(?:[^"]|\\")*)" "(?P<node_selector_string>[^"]*)"$/
1966       * @param string $not optional step verifier
1967       * @param string $nodeelement Element identifier
1968       * @param string $nodeselectortype Element type
1969       * @throws DriverException If not using JavaScript
1970       * @throws ExpectationException
1971       */
1972      public function the_focused_element_is($not, $nodeelement, $nodeselectortype) {
1973          if (!$this->running_javascript()) {
1974              throw new DriverException('Checking focus on an element requires JavaScript');
1975          }
1976  
1977          $element = $this->find($nodeselectortype, $nodeelement);
1978          $xpath = addslashes_js($element->getXpath());
1979          $script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
1980                  document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
1981          $targetisfocused = $this->evaluate_script($script);
1982          if ($not == ' not') {
1983              if ($targetisfocused) {
1984                  throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
1985              }
1986          } else {
1987              if (!$targetisfocused) {
1988                  throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
1989              }
1990          }
1991      }
1992  
1993      /**
1994       * Checks focus is with the given element.
1995       *
1996       * @Then /^the focused element is( not)? "(?P<n>(?:[^"]|\\")*)" "(?P<ns>[^"]*)" in the "(?P<c>(?:[^"]|\\")*)" "(?P<cs>[^"]*)"$/
1997       * @param string $not string optional step verifier
1998       * @param string $element Element identifier
1999       * @param string $selectortype Element type
2000       * @param string $nodeelement Element we look in
2001       * @param string $nodeselectortype The type of selector where we look in
2002       * @throws DriverException If not using JavaScript
2003       * @throws ExpectationException
2004       */
2005      public function the_focused_element_is_in_the($not, $element, $selectortype, $nodeelement, $nodeselectortype) {
2006          if (!$this->running_javascript()) {
2007              throw new DriverException('Checking focus on an element requires JavaScript');
2008          }
2009          $element = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
2010          $xpath = addslashes_js($element->getXpath());
2011          $script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
2012                  document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
2013          $targetisfocused = $this->evaluate_script($script);
2014          if ($not == ' not') {
2015              if ($targetisfocused) {
2016                  throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
2017              }
2018          } else {
2019              if (!$targetisfocused) {
2020                  throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
2021              }
2022          }
2023      }
2024  
2025      /**
2026       * Manually press tab key.
2027       *
2028       * @When /^I press( shift)? tab$/
2029       * @param string $shift string optional step verifier
2030       * @throws DriverException
2031       */
2032      public function i_manually_press_tab($shift = '') {
2033          if (empty($shift)) {
2034              $this->execute('behat_general::i_press_named_key', ['', 'tab']);
2035          } else {
2036              $this->execute('behat_general::i_press_named_key', ['shift', 'tab']);
2037          }
2038      }
2039  
2040      /**
2041       * Trigger click on node via javascript instead of actually clicking on it via pointer.
2042       * This function resolves the issue of nested elements.
2043       *
2044       * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" skipping visibility check$/
2045       * @param string $element
2046       * @param string $selectortype
2047       */
2048      public function i_click_on_skipping_visibility_check($element, $selectortype) {
2049  
2050          // Gets the node based on the requested selector type and locator.
2051          $node = $this->get_selected_node($selectortype, $element);
2052          $this->js_trigger_click($node);
2053      }
2054  
2055      /**
2056       * Checks, that the specified element contains the specified text a certain amount of times.
2057       * When running Javascript tests it also considers that texts may be hidden.
2058       *
2059       * @Then /^I should see "(?P<elementscount_number>\d+)" occurrences of "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
2060       * @throws ElementNotFoundException
2061       * @throws ExpectationException
2062       * @param int    $elementscount How many occurrences of the element we look for.
2063       * @param string $text
2064       * @param string $element Element we look in.
2065       * @param string $selectortype The type of element where we are looking in.
2066       */
2067      public function i_should_see_occurrences_of_in_element($elementscount, $text, $element, $selectortype) {
2068  
2069          // Getting the container where the text should be found.
2070          $container = $this->get_selected_node($selectortype, $element);
2071  
2072          // Looking for all the matching nodes without any other descendant matching the
2073          // same xpath (we are using contains(., ....).
2074          $xpathliteral = behat_context_helper::escape($text);
2075          $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
2076                  "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
2077  
2078          $nodes = $this->find_all('xpath', $xpath, false, $container);
2079  
2080          if ($this->running_javascript()) {
2081              $nodes = array_filter($nodes, function($node) {
2082                  return $node->isVisible();
2083              });
2084          }
2085  
2086          if ($elementscount != count($nodes)) {
2087              throw new ExpectationException('Found '.count($nodes).' elements in column. Expected '.$elementscount,
2088                      $this->getSession());
2089          }
2090      }
2091  
2092      /**
2093       * Manually press enter key.
2094       *
2095       * @When /^I press enter/
2096       * @throws DriverException
2097       */
2098      public function i_manually_press_enter() {
2099          $this->execute('behat_general::i_press_named_key', ['', 'enter']);
2100      }
2101  
2102      /**
2103       * Visit a local URL relative to the behat root.
2104       *
2105       * @When I visit :localurl
2106       *
2107       * @param string|moodle_url $localurl The URL relative to the behat_wwwroot to visit.
2108       */
2109      public function i_visit($localurl): void {
2110          $localurl = new moodle_url($localurl);
2111          $this->getSession()->visit($this->locate_path($localurl->out_as_local_url(false)));
2112      }
2113  
2114      /**
2115       * Increase the webdriver timeouts.
2116       *
2117       * This should be reset between scenarios, or can be called again to decrease the timeouts.
2118       *
2119       * @Given I mark this test as slow setting a timeout factor of :factor
2120       */
2121      public function i_mark_this_test_as_long_running(int $factor = 2): void {
2122          $this->set_test_timeout_factor($factor);
2123      }
2124  
2125      /**
2126       * Set the default text editor to the named text editor.
2127       *
2128       * @Given the default editor is set to :editor
2129       * @param string $editor
2130       * @throws ExpectationException If the specified editor is not available.
2131       */
2132      public function the_default_editor_is_set_to(string $editor): void {
2133          global $CFG;
2134  
2135          // Check if the provided editor is available.
2136          if (!array_key_exists($editor, editors_get_available())) {
2137              throw new ExpectationException(
2138                  "Unable to set the editor to {$editor} as it is not installed. The available editors are: " .
2139                      implode(', ', array_keys(editors_get_available())),
2140                  $this->getSession()
2141              );
2142          }
2143  
2144          // Make the provided editor the default one in $CFG->texteditors by
2145          // moving it to the first [editor],atto,tiny,tinymce,textarea on the list.
2146          $list = explode(',', $CFG->texteditors);
2147          array_unshift($list, $editor);
2148          $list = array_unique($list);
2149  
2150          // Set the list new list of editors.
2151          set_config('texteditors', implode(',', $list));
2152      }
2153  
2154      /**
2155       * Allow to check for minimal Moodle version.
2156       *
2157       * @Given the site is running Moodle version :minversion or higher
2158       * @param string $minversion The minimum version of Moodle required (inclusive).
2159       */
2160      public function the_site_is_running_moodle_version_or_higher(string $minversion): void {
2161          global $CFG;
2162          require_once($CFG->libdir . '/environmentlib.php');
2163  
2164          $currentversion = normalize_version(get_config('', 'release'));
2165  
2166          if (version_compare($currentversion, $minversion, '<')) {
2167              throw new Moodle\BehatExtension\Exception\SkippedException(
2168                  'Site must be running Moodle version ' . $minversion . ' or higher'
2169              );
2170          }
2171      }
2172  
2173      /**
2174       * Allow to check for maximum Moodle version.
2175       *
2176       * @Given the site is running Moodle version :maxversion or lower
2177       * @param string $maxversion The maximum version of Moodle required (inclusive).
2178       */
2179      public function the_site_is_running_moodle_version_or_lower(string $maxversion): void {
2180          global $CFG;
2181          require_once($CFG->libdir . '/environmentlib.php');
2182  
2183          $currentversion = normalize_version(get_config('', 'release'));
2184  
2185          if (version_compare($currentversion, $maxversion, '>')) {
2186              throw new Moodle\BehatExtension\Exception\SkippedException(
2187                  'Site must be running Moodle version ' . $maxversion . ' or lower'
2188              );
2189          }
2190      }
2191  
2192      /**
2193       * Check that the page title contains a given string.
2194       *
2195       * @Given the page title should contain ":title"
2196       * @param string $title The string that should be present on the page title.
2197       */
2198      public function the_page_title_should_contain(string $title): void {
2199          $session = $this->getSession();
2200          if ($this->running_javascript()) {
2201              // When running on JS, the page title can be changed via JS, so it's more reliable to get the actual page title via JS.
2202              $actualtitle = $session->evaluateScript("return document.title");
2203          } else {
2204              $titleelement = $session->getPage()->find('css', 'head title');
2205              if ($titleelement === null) {
2206                  // Throw an exception if a page title is not present on the page.
2207                  throw new ElementNotFoundException(
2208                      $this->getSession(),
2209                      '<title> element',
2210                      'css',
2211                      'head title'
2212                  );
2213              }
2214              $actualtitle = $titleelement->getText();
2215          }
2216  
2217          if (strpos($actualtitle, $title) === false) {
2218              throw new ExpectationException(
2219                  "'$title' was not found from the current page title '$actualtitle'",
2220                  $session
2221              );
2222          }
2223      }
2224  }