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