MDL-62653 behat: Ensure that tasks run properly from behat
[moodle.git] / lib / tests / behat / behat_general.php
CommitLineData
786ea937
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * General use steps definitions.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../behat/behat_base.php');
29
ca4f33a7 30use Behat\Mink\Exception\ExpectationException as ExpectationException,
d0a9a29b 31 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
bda1dea4 32 Behat\Mink\Exception\DriverException as DriverException,
39ec8285 33 WebDriver\Exception\NoSuchElement as NoSuchElement,
641459a8 34 WebDriver\Exception\StaleElementReference as StaleElementReference,
eb9ca848 35 Behat\Gherkin\Node\TableNode as TableNode;
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() {
40923977 77 $this->getSession()->visit($this->locate_path('/'));
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() {
86 $this->getSession()->visit($this->locate_path('/?redirect=0'));
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() {
95 $this->getSession()->visit($this->locate_path('/course/index.php'));
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,
177 self::EXTENDED_TIMEOUT
178 );
e5eff0b6
AA
179 }
180
181 /**
182 * Switches to the main Moodle frame.
183 *
184 * @Given /^I switch to the main frame$/
185 */
186 public function switch_to_the_main_frame() {
187 $this->getSession()->switchToIFrame();
188 }
189
1303eb29
DM
190 /**
191 * Switches to the specified window. Useful when interacting with popup windows.
192 *
193 * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
194 * @param string $windowname
195 */
196 public function switch_to_window($windowname) {
a92105fd
TH
197 // In Behat, some browsers (e.g. Chrome) are unable to switch to a
198 // window without a name, and by default the main browser window does
199 // not have a name. To work-around this, when we switch away from an
200 // unnamed window (presumably the main window) to some other named
201 // window, then we first set the main window name to a conventional
202 // value that we can later use this name to switch back.
42ad096f 203 $this->getSession()->executeScript(
a92105fd
TH
204 'if (window.name == "") window.name = "' . self::MAIN_WINDOW_NAME . '"');
205
1303eb29
DM
206 $this->getSession()->switchToWindow($windowname);
207 }
208
209 /**
210 * Switches to the main Moodle window. Useful when you finish interacting with popup windows.
211 *
212 * @Given /^I switch to the main window$/
213 */
214 public function switch_to_the_main_window() {
a92105fd 215 $this->getSession()->switchToWindow(self::MAIN_WINDOW_NAME);
1303eb29
DM
216 }
217
563514b1 218 /**
7daab401 219 * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
563514b1
DM
220 * @Given /^I accept the currently displayed dialog$/
221 */
222 public function accept_currently_displayed_alert_dialog() {
223 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
224 }
225
20dd5a7a
TH
226 /**
227 * Dismisses the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
228 * @Given /^I dismiss the currently displayed dialog$/
229 */
230 public function dismiss_currently_displayed_alert_dialog() {
231 $this->getSession()->getDriver()->getWebDriverSession()->dismiss_alert();
232 }
233
786ea937
DM
234 /**
235 * Clicks link with specified id|title|alt|text.
236 *
786ea937 237 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
1f9ffbdb 238 * @throws ElementNotFoundException Thrown by behat_base::find
40923977 239 * @param string $link
786ea937
DM
240 */
241 public function click_link($link) {
1f9ffbdb
DM
242
243 $linknode = $this->find_link($link);
d1e55a47 244 $this->ensure_node_is_visible($linknode);
1f9ffbdb 245 $linknode->click();
786ea937
DM
246 }
247
248 /**
249 * Waits X seconds. Required after an action that requires data from an AJAX request.
250 *
251 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
252 * @param int $seconds
253 */
254 public function i_wait_seconds($seconds) {
d245f675 255 if ($this->running_javascript()) {
3cf0d01a 256 $this->getSession()->wait($seconds * 1000);
d245f675
AN
257 } else {
258 sleep($seconds);
d0a9a29b 259 }
786ea937
DM
260 }
261
262 /**
263 * Waits until the page is completely loaded. This step is auto-executed after every step.
264 *
265 * @Given /^I wait until the page is ready$/
266 */
267 public function wait_until_the_page_is_ready() {
d0a9a29b 268
eb9ca848 269 // No need to wait if not running JS.
d0a9a29b 270 if (!$this->running_javascript()) {
eb9ca848 271 return;
d0a9a29b
DM
272 }
273
d1e55a47
DM
274 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
275 }
276
277 /**
278 * Waits until the provided element selector exists in the DOM
279 *
280 * Using the protected method as this method will be usually
281 * called by other methods which are not returning a set of
282 * steps and performs the actions directly, so it would not
283 * be executed if it returns another step.
284
285 * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" exists$/
286 * @param string $element
287 * @param string $selector
288 * @return void
289 */
290 public function wait_until_exists($element, $selectortype) {
291 $this->ensure_element_exists($element, $selectortype);
292 }
293
294 /**
295 * Waits until the provided element does not exist in the DOM
296 *
297 * Using the protected method as this method will be usually
298 * called by other methods which are not returning a set of
299 * steps and performs the actions directly, so it would not
300 * be executed if it returns another step.
301
302 * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" does not exist$/
303 * @param string $element
304 * @param string $selector
305 * @return void
306 */
307 public function wait_until_does_not_exists($element, $selectortype) {
308 $this->ensure_element_does_not_exist($element, $selectortype);
786ea937
DM
309 }
310
311 /**
40923977 312 * Generic mouse over action. Mouse over a element of the specified type.
786ea937 313 *
40923977
DM
314 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
315 * @param string $element Element we look for
316 * @param string $selectortype The type of what we look for
786ea937 317 */
40923977 318 public function i_hover($element, $selectortype) {
1f9ffbdb 319
40923977
DM
320 // Gets the node based on the requested selector type and locator.
321 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
322 $node->mouseOver();
323 }
324
40923977
DM
325 /**
326 * Generic click action. Click on the element of the specified type.
327 *
328 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
329 * @param string $element Element we look for
330 * @param string $selectortype The type of what we look for
331 */
332 public function i_click_on($element, $selectortype) {
333
334 // Gets the node based on the requested selector type and locator.
335 $node = $this->get_selected_node($selectortype, $element);
d1e55a47 336 $this->ensure_node_is_visible($node);
40923977
DM
337 $node->click();
338 }
339
d7a0b721
RT
340 /**
341 * Sets the focus and takes away the focus from an element, generating blur JS event.
342 *
343 * @When /^I take focus off "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
344 * @param string $element Element we look for
345 * @param string $selectortype The type of what we look for
346 */
347 public function i_take_focus_off_field($element, $selectortype) {
348 if (!$this->running_javascript()) {
349 throw new ExpectationException('Can\'t take focus off from "' . $element . '" in non-js mode', $this->getSession());
350 }
351 // Gets the node based on the requested selector type and locator.
352 $node = $this->get_selected_node($selectortype, $element);
353 $this->ensure_node_is_visible($node);
354
355 // Ensure element is focused before taking it off.
356 $node->focus();
357 $node->blur();
358 }
359
b28b374f
DM
360 /**
361 * Clicks the specified element and confirms the expected dialogue.
362 *
363 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" confirming the dialogue$/
364 * @throws ElementNotFoundException Thrown by behat_base::find
e04cf8d8
DP
365 * @param string $element Element we look for
366 * @param string $selectortype The type of what we look for
b28b374f
DM
367 */
368 public function i_click_on_confirming_the_dialogue($element, $selectortype) {
369 $this->i_click_on($element, $selectortype);
370 $this->accept_currently_displayed_alert_dialog();
371 }
372
20dd5a7a
TH
373 /**
374 * Clicks the specified element and dismissing the expected dialogue.
375 *
376 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" dismissing the dialogue$/
377 * @throws ElementNotFoundException Thrown by behat_base::find
e04cf8d8
DP
378 * @param string $element Element we look for
379 * @param string $selectortype The type of what we look for
20dd5a7a
TH
380 */
381 public function i_click_on_dismissing_the_dialogue($element, $selectortype) {
382 $this->i_click_on($element, $selectortype);
383 $this->dismiss_currently_displayed_alert_dialog();
384 }
385
072f67fc
DM
386 /**
387 * Click on the element of the specified type which is located inside the second element.
388 *
389 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
390 * @param string $element Element we look for
391 * @param string $selectortype The type of what we look for
392 * @param string $nodeelement Element we look in
393 * @param string $nodeselectortype The type of selector where we look in
394 */
395 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
396
397 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
d1e55a47 398 $this->ensure_node_is_visible($node);
072f67fc
DM
399 $node->click();
400 }
401
563514b1 402 /**
7daab401 403 * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
563514b1
DM
404 *
405 * The steps definitions calling this step as part of them should
406 * manage the wait times by themselves as the times and when the
407 * waits should be done depends on what is being dragged & dropper.
408 *
409 * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
410 * @param string $element
411 * @param string $selectortype
412 * @param string $containerelement
413 * @param string $containerselectortype
414 */
415 public function i_drag_and_i_drop_it_in($element, $selectortype, $containerelement, $containerselectortype) {
416
417 list($sourceselector, $sourcelocator) = $this->transform_selector($selectortype, $element);
418 $sourcexpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($sourceselector, $sourcelocator);
419
420 list($containerselector, $containerlocator) = $this->transform_selector($containerselectortype, $containerelement);
421 $destinationxpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($containerselector, $containerlocator);
422
031152cc
DW
423 $node = $this->get_selected_node("xpath_element", $sourcexpath);
424 if (!$node->isVisible()) {
425 throw new ExpectationException('"' . $sourcexpath . '" "xpath_element" is not visible', $this->getSession());
426 }
427 $node = $this->get_selected_node("xpath_element", $destinationxpath);
428 if (!$node->isVisible()) {
429 throw new ExpectationException('"' . $destinationxpath . '" "xpath_element" is not visible', $this->getSession());
430 }
431
563514b1
DM
432 $this->getSession()->getDriver()->dragTo($sourcexpath, $destinationxpath);
433 }
434
63950e4d
DM
435 /**
436 * Checks, that the specified element is visible. Only available in tests using Javascript.
437 *
438 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
439 * @throws ElementNotFoundException
440 * @throws ExpectationException
441 * @throws DriverException
442 * @param string $element
443 * @param string $selectortype
444 * @return void
445 */
446 public function should_be_visible($element, $selectortype) {
447
448 if (!$this->running_javascript()) {
449 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
450 }
451
452 $node = $this->get_selected_node($selectortype, $element);
453 if (!$node->isVisible()) {
454 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
455 }
456 }
457
458 /**
74c78e74 459 * Checks, that the existing element is not visible. Only available in tests using Javascript.
63950e4d 460 *
74c78e74
DM
461 * As a "not" method, it's performance could not be good, but in this
462 * case the performance is good because the element must exist,
463 * otherwise there would be a ElementNotFoundException, also here we are
464 * not spinning until the element is visible.
c1faf86b 465 *
63950e4d
DM
466 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
467 * @throws ElementNotFoundException
468 * @throws ExpectationException
469 * @param string $element
470 * @param string $selectortype
471 * @return void
472 */
473 public function should_not_be_visible($element, $selectortype) {
474
475 try {
476 $this->should_be_visible($element, $selectortype);
63950e4d
DM
477 } catch (ExpectationException $e) {
478 // All as expected.
ca0ceacd 479 return;
63950e4d 480 }
ca0ceacd 481 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
63950e4d
DM
482 }
483
484 /**
485 * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
486 *
487 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
488 * @throws ElementNotFoundException
489 * @throws DriverException
490 * @throws ExpectationException
491 * @param string $element Element we look for
492 * @param string $selectortype The type of what we look for
493 * @param string $nodeelement Element we look in
494 * @param string $nodeselectortype The type of selector where we look in
495 */
496 public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
497
498 if (!$this->running_javascript()) {
499 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
500 }
501
502 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
503 if (!$node->isVisible()) {
504 throw new ExpectationException(
505 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
506 $this->getSession()
507 );
508 }
509 }
510
511 /**
74c78e74
DM
512 * Checks, that the existing element is not visible inside the existing container. Only available in tests using Javascript.
513 *
514 * As a "not" method, it's performance could not be good, but in this
515 * case the performance is good because the element must exist,
516 * otherwise there would be a ElementNotFoundException, also here we are
517 * not spinning until the element is visible.
63950e4d
DM
518 *
519 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
520 * @throws ElementNotFoundException
521 * @throws ExpectationException
522 * @param string $element Element we look for
523 * @param string $selectortype The type of what we look for
524 * @param string $nodeelement Element we look in
525 * @param string $nodeselectortype The type of selector where we look in
526 */
527 public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
528
529 try {
530 $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
63950e4d
DM
531 } catch (ExpectationException $e) {
532 // All as expected.
ca0ceacd 533 return;
63950e4d 534 }
ca0ceacd
TH
535 throw new ExpectationException(
536 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
537 $this->getSession()
538 );
63950e4d
DM
539 }
540
786ea937 541 /**
e9af3ed3 542 * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
786ea937 543 *
786ea937 544 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 545 * @throws ExpectationException
40923977 546 * @param string $text
786ea937
DM
547 */
548 public function assert_page_contains_text($text) {
9a1f4922 549
e9af3ed3
DM
550 // Looking for all the matching nodes without any other descendant matching the
551 // same xpath (we are using contains(., ....).
921faad9 552 $xpathliteral = behat_context_helper::escape($text);
e9af3ed3
DM
553 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
554 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
9a1f4922 555
9a1f4922 556 try {
e9af3ed3 557 $nodes = $this->find_all('xpath', $xpath);
c1faf86b
DM
558 } catch (ElementNotFoundException $e) {
559 throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
560 }
e9af3ed3 561
c1faf86b
DM
562 // If we are not running javascript we have enough with the
563 // element existing as we can't check if it is visible.
564 if (!$this->running_javascript()) {
565 return;
566 }
567
568 // We spin as we don't have enough checking that the element is there, we
74c78e74
DM
569 // should also ensure that the element is visible. Using microsleep as this
570 // is a repeated step and global performance is important.
c1faf86b
DM
571 $this->spin(
572 function($context, $args) {
573
574 foreach ($args['nodes'] as $node) {
e9af3ed3 575 if ($node->isVisible()) {
c1faf86b 576 return true;
e9af3ed3
DM
577 }
578 }
579
c1faf86b
DM
580 // If non of the nodes is visible we loop again.
581 throw new ExpectationException('"' . $args['text'] . '" text was found but was not visible', $context->getSession());
582 },
74c78e74
DM
583 array('nodes' => $nodes, 'text' => $text),
584 false,
585 false,
586 true
c1faf86b 587 );
e9af3ed3 588
786ea937
DM
589 }
590
591 /**
e9af3ed3 592 * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 593 *
786ea937 594 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 595 * @throws ExpectationException
40923977 596 * @param string $text
786ea937
DM
597 */
598 public function assert_page_not_contains_text($text) {
9a1f4922 599
c1faf86b
DM
600 // Looking for all the matching nodes without any other descendant matching the
601 // same xpath (we are using contains(., ....).
921faad9 602 $xpathliteral = behat_context_helper::escape($text);
c1faf86b
DM
603 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
604 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
605
606 // We should wait a while to ensure that the page is not still loading elements.
74c78e74
DM
607 // Waiting less than self::TIMEOUT as we already waited for the DOM to be ready and
608 // all JS to be executed.
9a1f4922 609 try {
74c78e74 610 $nodes = $this->find_all('xpath', $xpath, false, false, self::REDUCED_TIMEOUT);
c1faf86b
DM
611 } catch (ElementNotFoundException $e) {
612 // All ok.
5458ab3e 613 return;
9a1f4922 614 }
5458ab3e 615
c1faf86b
DM
616 // If we are not running javascript we have enough with the
617 // element existing as we can't check if it is hidden.
618 if (!$this->running_javascript()) {
619 throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
620 }
621
622 // If the element is there we should be sure that it is not visible.
623 $this->spin(
624 function($context, $args) {
625
626 foreach ($args['nodes'] as $node) {
ade50a11
RT
627 // If element is removed from dom, then just exit.
628 try {
629 // If element is visible then throw exception, so we keep spinning.
630 if ($node->isVisible()) {
631 throw new ExpectationException('"' . $args['text'] . '" text was found in the page',
632 $context->getSession());
633 }
634 } catch (WebDriver\Exception\NoSuchElement $e) {
635 // Do nothing just return, as element is no more on page.
636 return true;
f5459099
MN
637 } catch (ElementNotFoundException $e) {
638 // Do nothing just return, as element is no more on page.
639 return true;
c1faf86b
DM
640 }
641 }
642
643 // If non of the found nodes is visible we consider that the text is not visible.
644 return true;
645 },
74c78e74
DM
646 array('nodes' => $nodes, 'text' => $text),
647 self::REDUCED_TIMEOUT,
648 false,
649 true
c1faf86b 650 );
786ea937
DM
651 }
652
653 /**
e9af3ed3 654 * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 655 *
40923977 656 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
657 * @throws ElementNotFoundException
658 * @throws ExpectationException
40923977
DM
659 * @param string $text
660 * @param string $element Element we look in.
661 * @param string $selectortype The type of element where we are looking in.
786ea937 662 */
40923977
DM
663 public function assert_element_contains_text($text, $element, $selectortype) {
664
5458ab3e
DM
665 // Getting the container where the text should be found.
666 $container = $this->get_selected_node($selectortype, $element);
667
e9af3ed3
DM
668 // Looking for all the matching nodes without any other descendant matching the
669 // same xpath (we are using contains(., ....).
921faad9 670 $xpathliteral = behat_context_helper::escape($text);
e9af3ed3
DM
671 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
672 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
5458ab3e
DM
673
674 // Wait until it finds the text inside the container, otherwise custom exception.
675 try {
e9af3ed3 676 $nodes = $this->find_all('xpath', $xpath, false, $container);
c1faf86b
DM
677 } catch (ElementNotFoundException $e) {
678 throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
679 }
e9af3ed3 680
c1faf86b
DM
681 // If we are not running javascript we have enough with the
682 // element existing as we can't check if it is visible.
683 if (!$this->running_javascript()) {
684 return;
685 }
686
74c78e74
DM
687 // We also check the element visibility when running JS tests. Using microsleep as this
688 // is a repeated step and global performance is important.
c1faf86b
DM
689 $this->spin(
690 function($context, $args) {
691
692 foreach ($args['nodes'] as $node) {
e9af3ed3 693 if ($node->isVisible()) {
c1faf86b 694 return true;
e9af3ed3
DM
695 }
696 }
697
c1faf86b
DM
698 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element but was not visible', $context->getSession());
699 },
74c78e74
DM
700 array('nodes' => $nodes, 'text' => $text, 'element' => $element),
701 false,
702 false,
703 true
c1faf86b 704 );
786ea937
DM
705 }
706
707 /**
e9af3ed3 708 * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 709 *
40923977 710 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
711 * @throws ElementNotFoundException
712 * @throws ExpectationException
40923977
DM
713 * @param string $text
714 * @param string $element Element we look in.
715 * @param string $selectortype The type of element where we are looking in.
786ea937 716 */
40923977
DM
717 public function assert_element_not_contains_text($text, $element, $selectortype) {
718
c1faf86b
DM
719 // Getting the container where the text should be found.
720 $container = $this->get_selected_node($selectortype, $element);
721
722 // Looking for all the matching nodes without any other descendant matching the
723 // same xpath (we are using contains(., ....).
921faad9 724 $xpathliteral = behat_context_helper::escape($text);
c1faf86b
DM
725 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
726 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
727
728 // We should wait a while to ensure that the page is not still loading elements.
729 // Giving preference to the reliability of the results rather than to the performance.
5458ab3e 730 try {
74c78e74 731 $nodes = $this->find_all('xpath', $xpath, false, $container, self::REDUCED_TIMEOUT);
c1faf86b
DM
732 } catch (ElementNotFoundException $e) {
733 // All ok.
5458ab3e
DM
734 return;
735 }
736
c1faf86b
DM
737 // If we are not running javascript we have enough with the
738 // element not being found as we can't check if it is visible.
739 if (!$this->running_javascript()) {
740 throw new ExpectationException('"' . $text . '" text was found in the "' . $element . '" element', $this->getSession());
741 }
742
743 // We need to ensure all the found nodes are hidden.
744 $this->spin(
745 function($context, $args) {
746
747 foreach ($args['nodes'] as $node) {
748 if ($node->isVisible()) {
749 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element', $context->getSession());
750 }
751 }
752
753 // If all the found nodes are hidden we are happy.
754 return true;
755 },
74c78e74
DM
756 array('nodes' => $nodes, 'text' => $text, 'element' => $element),
757 self::REDUCED_TIMEOUT,
758 false,
759 true
c1faf86b 760 );
786ea937
DM
761 }
762
60054942
DM
763 /**
764 * Checks, that the first specified element appears before the second one.
765 *
766 * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
767 * @throws ExpectationException
768 * @param string $preelement The locator of the preceding element
769 * @param string $preselectortype The locator of the preceding element
770 * @param string $postelement The locator of the latest element
771 * @param string $postselectortype The selector type of the latest element
772 */
773 public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
774
775 // We allow postselectortype as a non-text based selector.
776 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
777 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
778
779 $prexpath = $this->find($preselector, $prelocator)->getXpath();
780 $postxpath = $this->find($postselector, $postlocator)->getXpath();
781
782 // Using following xpath axe to find it.
783 $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
784 $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
785 if (!$this->getSession()->getDriver()->find($xpath)) {
786 throw new ExpectationException($msg, $this->getSession());
787 }
788 }
789
790 /**
791 * Checks, that the first specified element appears after the second one.
792 *
793 * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
794 * @throws ExpectationException
795 * @param string $postelement The locator of the latest element
796 * @param string $postselectortype The selector type of the latest element
797 * @param string $preelement The locator of the preceding element
798 * @param string $preselectortype The locator of the preceding element
799 */
800 public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
801
802 // We allow postselectortype as a non-text based selector.
803 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
804 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
805
806 $postxpath = $this->find($postselector, $postlocator)->getXpath();
807 $prexpath = $this->find($preselector, $prelocator)->getXpath();
808
809 // Using preceding xpath axe to find it.
810 $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
811 $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
812 if (!$this->getSession()->getDriver()->find($xpath)) {
813 throw new ExpectationException($msg, $this->getSession());
814 }
815 }
816
786ea937 817 /**
40923977 818 * Checks, that element of specified type is disabled.
786ea937 819 *
40923977 820 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 821 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
822 * @param string $element Element we look in
823 * @param string $selectortype The type of element where we are looking in.
786ea937 824 */
40923977 825 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 826
40923977
DM
827 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
828 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
829
830 if (!$node->hasAttribute('disabled')) {
831 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
832 }
833 }
834
835 /**
40923977 836 * Checks, that element of specified type is enabled.
786ea937 837 *
40923977 838 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 839 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
840 * @param string $element Element we look on
841 * @param string $selectortype The type of where we look
786ea937 842 */
40923977 843 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 844
40923977
DM
845 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
846 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
847
848 if ($node->hasAttribute('disabled')) {
849 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
850 }
851 }
852
a2d3e3b6
MN
853 /**
854 * Checks the provided element and selector type are readonly on the current page.
855 *
856 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
857 * @throws ExpectationException Thrown by behat_base::find
858 * @param string $element Element we look in
859 * @param string $selectortype The type of element where we are looking in.
860 */
861 public function the_element_should_be_readonly($element, $selectortype) {
862 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
863 $node = $this->get_selected_node($selectortype, $element);
864
865 if (!$node->hasAttribute('readonly')) {
866 throw new ExpectationException('The element "' . $element . '" is not readonly', $this->getSession());
867 }
868 }
869
870 /**
871 * Checks the provided element and selector type are not readonly on the current page.
872 *
873 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
874 * @throws ExpectationException Thrown by behat_base::find
875 * @param string $element Element we look in
876 * @param string $selectortype The type of element where we are looking in.
877 */
878 public function the_element_should_not_be_readonly($element, $selectortype) {
879 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
880 $node = $this->get_selected_node($selectortype, $element);
881
882 if ($node->hasAttribute('readonly')) {
883 throw new ExpectationException('The element "' . $element . '" is readonly', $this->getSession());
884 }
885 }
886
ca4f33a7 887 /**
62eb5c46
EL
888 * Checks the provided element and selector type exists in the current page.
889 *
890 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7 891 *
c51c3b55 892 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist$/
ca4f33a7
DM
893 * @throws ElementNotFoundException Thrown by behat_base::find
894 * @param string $element The locator of the specified selector
895 * @param string $selectortype The selector type
896 */
c51c3b55 897 public function should_exist($element, $selectortype) {
ca4f33a7
DM
898
899 // Getting Mink selector and locator.
900 list($selector, $locator) = $this->transform_selector($selectortype, $element);
901
902 // Will throw an ElementNotFoundException if it does not exist.
903 $this->find($selector, $locator);
904 }
905
906 /**
62eb5c46
EL
907 * Checks that the provided element and selector type not exists in the current page.
908 *
909 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7 910 *
c51c3b55 911 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist$/
ca4f33a7
DM
912 * @throws ExpectationException
913 * @param string $element The locator of the specified selector
914 * @param string $selectortype The selector type
915 */
c51c3b55 916 public function should_not_exist($element, $selectortype) {
ca4f33a7 917
74c78e74
DM
918 // Getting Mink selector and locator.
919 list($selector, $locator) = $this->transform_selector($selectortype, $element);
920
ca4f33a7 921 try {
74c78e74
DM
922
923 // Using directly the spin method as we want a reduced timeout but there is no
924 // need for a 0.1 seconds interval because in the optimistic case we will timeout.
925 $params = array('selector' => $selector, 'locator' => $locator);
926 // The exception does not really matter as we will catch it and will never "explode".
927 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $element);
928
929 // If all goes good it will throw an ElementNotFoundExceptionn that we will catch.
930 $this->spin(
931 function($context, $args) {
932 return $context->getSession()->getPage()->findAll($args['selector'], $args['locator']);
933 },
934 $params,
20503d15 935 self::REDUCED_TIMEOUT,
74c78e74 936 $exception,
20503d15 937 false
74c78e74 938 );
62eb5c46 939 } catch (ElementNotFoundException $e) {
ca4f33a7
DM
940 // It passes.
941 return;
942 }
ca0ceacd
TH
943
944 throw new ExpectationException('The "' . $element . '" "' . $selectortype .
945 '" exists in the current page', $this->getSession());
ca4f33a7
DM
946 }
947
066ef320
JM
948 /**
949 * This step triggers cron like a user would do going to admin/cron.php.
950 *
951 * @Given /^I trigger cron$/
952 */
953 public function i_trigger_cron() {
954 $this->getSession()->visit($this->locate_path('/admin/cron.php'));
955 }
956
a4ce565f 957 /**
958 * Runs a scheduled task immediately, given full class name.
959 *
960 * This is faster and more reliable than running cron (running cron won't
961 * work more than once in the same test, for instance). However it is
962 * a little less 'realistic'.
963 *
964 * While the task is running, we suppress mtrace output because it makes
965 * the Behat result look ugly.
966 *
967 * Note: Most of the code relating to running a task is based on
968 * admin/tool/task/cli/schedule_task.php.
969 *
970 * @Given /^I run the scheduled task "(?P<task_name>[^"]+)"$/
971 * @param string $taskname Name of task e.g. 'mod_whatever\task\do_something'
972 */
973 public function i_run_the_scheduled_task($taskname) {
df95c479
AN
974 global $CFG;
975 require_once("{$CFG->libdir}/cronlib.php");
976
a4ce565f 977 $task = \core\task\manager::get_scheduled_task($taskname);
978 if (!$task) {
979 throw new DriverException('The "' . $taskname . '" scheduled task does not exist');
980 }
981
982 // Do setup for cron task.
983 raise_memory_limit(MEMORY_EXTRA);
984 cron_setup_user();
985
986 // Get lock.
987 $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
988 if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
989 throw new DriverException('Unable to obtain core_cron lock for scheduled task');
990 }
991 if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
992 $cronlock->release();
993 throw new DriverException('Unable to obtain task lock for scheduled task');
994 }
995 $task->set_lock($lock);
996 if (!$task->is_blocking()) {
997 $cronlock->release();
998 } else {
999 $task->set_cron_lock($cronlock);
1000 }
1001
1002 try {
df95c479
AN
1003 // Prepare the renderer.
1004 cron_prepare_core_renderer();
1005
a4ce565f 1006 // Discard task output as not appropriate for Behat output!
1007 ob_start();
1008 $task->execute();
1009 ob_end_clean();
1010
df95c479
AN
1011 // Restore the previous renderer.
1012 cron_prepare_core_renderer(true);
1013
a4ce565f 1014 // Mark task complete.
1015 \core\task\manager::scheduled_task_complete($task);
1016 } catch (Exception $e) {
df95c479
AN
1017 // Restore the previous renderer.
1018 cron_prepare_core_renderer(true);
1019
a4ce565f 1020 // Mark task failed and throw exception.
1021 \core\task\manager::scheduled_task_failed($task);
df95c479 1022
a4ce565f 1023 throw new DriverException('The "' . $taskname . '" scheduled task failed', 0, $e);
1024 }
1025 }
1026
ff4230d8
JD
1027 /**
1028 * Runs all ad-hoc tasks in the queue.
1029 *
1030 * This is faster and more reliable than running cron (running cron won't
1031 * work more than once in the same test, for instance). However it is
1032 * a little less 'realistic'.
1033 *
1034 * While the task is running, we suppress mtrace output because it makes
1035 * the Behat result look ugly.
1036 *
1037 * @Given /^I run all adhoc tasks$/
1038 * @throws DriverException
1039 */
1040 public function i_run_all_adhoc_tasks() {
df95c479
AN
1041 global $CFG, $DB;
1042 require_once("{$CFG->libdir}/cronlib.php");
1043
ff4230d8
JD
1044 // Do setup for cron task.
1045 cron_setup_user();
1046
df95c479
AN
1047 // Discard task output as not appropriate for Behat output!
1048 ob_start();
1049
1050 // Run all tasks which have a scheduled runtime of before now.
1051 $timenow = time();
1052
1053 while (!\core\task\manager::static_caches_cleared_since($timenow) &&
1054 $task = \core\task\manager::get_next_adhoc_task($timenow)) {
1055 // Clean the output buffer between tasks.
1056 ob_clean();
1057
1058 // Run the task.
1059 cron_run_inner_adhoc_task($task);
1060
1061 // Check whether the task record still exists.
1062 // If a task was successful it will be removed.
1063 // If it failed then it will still exist.
1064 if ($DB->record_exists('task_adhoc', ['id' => $task->get_id()])) {
1065 // End ouptut buffering and flush the current buffer.
1066 // This should be from just the current task.
1067 ob_end_flush();
1068
1069 throw new DriverException('An adhoc task failed', 0);
ff4230d8
JD
1070 }
1071 }
1072 ob_end_clean();
1073 }
1074
a2d3e3b6
MN
1075 /**
1076 * Checks that an element and selector type exists in another element and selector type on the current page.
1077 *
1078 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1079 *
1080 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
1081 * @throws ElementNotFoundException Thrown by behat_base::find
1082 * @param string $element The locator of the specified selector
1083 * @param string $selectortype The selector type
1084 * @param string $containerelement The container selector type
1085 * @param string $containerselectortype The container locator
1086 */
1087 public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
1088 // Get the container node.
1089 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
1090
1091 list($selector, $locator) = $this->transform_selector($selectortype, $element);
1092
1093 // Specific exception giving info about where can't we find the element.
1094 $locatorexceptionmsg = $element . '" in the "' . $containerelement. '" "' . $containerselectortype. '"';
1095 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg);
1096
1097 // Looks for the requested node inside the container node.
1098 $this->find($selector, $locator, $exception, $containernode);
1099 }
1100
1101 /**
1102 * Checks that an element and selector type does not exist in another element and selector type on the current page.
1103 *
1104 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
1105 *
1106 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
1107 * @throws ExpectationException
1108 * @param string $element The locator of the specified selector
1109 * @param string $selectortype The selector type
1110 * @param string $containerelement The container selector type
1111 * @param string $containerselectortype The container locator
1112 */
1113 public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
74c78e74
DM
1114
1115 // Get the container node; here we throw an exception
1116 // if the container node does not exist.
1117 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
1118
1119 list($selector, $locator) = $this->transform_selector($selectortype, $element);
1120
1121 // Will throw an ElementNotFoundException if it does not exist, but, actually
759b323e 1122 // it should not exist, so we try & catch it.
a2d3e3b6 1123 try {
74c78e74
DM
1124 // Would be better to use a 1 second sleep because the element should not be there,
1125 // but we would need to duplicate the whole find_all() logic to do it, the benefit of
1126 // changing to 1 second sleep is not significant.
1127 $this->find($selector, $locator, false, $containernode, self::REDUCED_TIMEOUT);
a2d3e3b6
MN
1128 } catch (ElementNotFoundException $e) {
1129 // It passes.
1130 return;
1131 }
ca0ceacd
TH
1132 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the "' .
1133 $containerelement . '" "' . $containerselectortype . '"', $this->getSession());
a2d3e3b6 1134 }
3b0b5e57
RT
1135
1136 /**
1137 * Change browser window size small: 640x480, medium: 1024x768, large: 2560x1600, custom: widthxheight
1138 *
1139 * Example: I change window size to "small" or I change window size to "1024x768"
5a14c4d9 1140 * or I change viewport size to "800x600". The viewport option is useful to guarantee that the
1141 * browser window has same viewport size even when you run Behat on multiple operating systems.
3b0b5e57
RT
1142 *
1143 * @throws ExpectationException
5a14c4d9 1144 * @Then /^I change (window|viewport) size to "(small|medium|large|\d+x\d+)"$/
3b0b5e57
RT
1145 * @param string $windowsize size of the window (small|medium|large|wxh).
1146 */
5a14c4d9 1147 public function i_change_window_size_to($windowviewport, $windowsize) {
1148 $this->resize_window($windowsize, $windowviewport === 'viewport');
3b0b5e57 1149 }
c0fb7f44 1150
1151 /**
1152 * Checks whether there is an attribute on the given element that contains the specified text.
1153 *
1154 * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should contain "(?P<text_string>(?:[^"]|\\")*)"$/
1155 * @throws ExpectationException
1156 * @param string $attribute Name of attribute
1157 * @param string $element The locator of the specified selector
1158 * @param string $selectortype The selector type
1159 * @param string $text Expected substring
1160 */
1161 public function the_attribute_of_should_contain($attribute, $element, $selectortype, $text) {
1162 // Get the container node (exception if it doesn't exist).
1163 $containernode = $this->get_selected_node($selectortype, $element);
1164 $value = $containernode->getAttribute($attribute);
1165 if ($value == null) {
1166 throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
1167 $this->getSession());
1168 } else if (strpos($value, $text) === false) {
1169 throw new ExpectationException('The attribute "' . $attribute .
1170 '" does not contain "' . $text . '" (actual value: "' . $value . '")',
1171 $this->getSession());
1172 }
1173 }
90c3ffe3 1174
1175 /**
1176 * Checks that the attribute on the given element does not contain the specified text.
1177 *
1178 * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not contain "(?P<text_string>(?:[^"]|\\")*)"$/
1179 * @throws ExpectationException
1180 * @param string $attribute Name of attribute
1181 * @param string $element The locator of the specified selector
1182 * @param string $selectortype The selector type
1183 * @param string $text Expected substring
1184 */
1185 public function the_attribute_of_should_not_contain($attribute, $element, $selectortype, $text) {
1186 // Get the container node (exception if it doesn't exist).
1187 $containernode = $this->get_selected_node($selectortype, $element);
1188 $value = $containernode->getAttribute($attribute);
1189 if ($value == null) {
1190 throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
1191 $this->getSession());
1192 } else if (strpos($value, $text) !== false) {
1193 throw new ExpectationException('The attribute "' . $attribute .
1194 '" contains "' . $text . '" (value: "' . $value . '")',
1195 $this->getSession());
1196 }
1197 }
641459a8
RT
1198
1199 /**
1200 * Checks the provided value exists in specific row/column of table.
1201 *
1202 * @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should contain "(?P<value_string>[^"]*)"$/
1203 * @throws ElementNotFoundException
1204 * @param string $row row text which will be looked in.
97329f1b 1205 * @param string $column column text to search (or numeric value for the column position)
641459a8
RT
1206 * @param string $table table id/class/caption
1207 * @param string $value text to check.
1208 */
1209 public function row_column_of_table_should_contain($row, $column, $table, $value) {
1210 $tablenode = $this->get_selected_node('table', $table);
1211 $tablexpath = $tablenode->getXpath();
1212
921faad9
RT
1213 $rowliteral = behat_context_helper::escape($row);
1214 $valueliteral = behat_context_helper::escape($value);
1215 $columnliteral = behat_context_helper::escape($column);
bd855fd5 1216
97329f1b
MG
1217 if (preg_match('/^-?(\d+)-?$/', $column, $columnasnumber)) {
1218 // Column indicated as a number, just use it as position of the column.
1219 $columnpositionxpath = "/child::*[position() = {$columnasnumber[1]}]";
1220 } else {
1221 // Header can be in thead or tbody (first row), following xpath should work.
1222 $theadheaderxpath = "thead/tr[1]/th[(normalize-space(.)=" . $columnliteral . " or a[normalize-space(text())=" .
dd2e1c22 1223 $columnliteral . "] or div[normalize-space(text())=" . $columnliteral . "])]";
97329f1b 1224 $tbodyheaderxpath = "tbody/tr[1]/td[(normalize-space(.)=" . $columnliteral . " or a[normalize-space(text())=" .
dd2e1c22 1225 $columnliteral . "] or div[normalize-space(text())=" . $columnliteral . "])]";
97329f1b
MG
1226
1227 // Check if column exists.
1228 $columnheaderxpath = $tablexpath . "[" . $theadheaderxpath . " | " . $tbodyheaderxpath . "]";
1229 $columnheader = $this->getSession()->getDriver()->find($columnheaderxpath);
1230 if (empty($columnheader)) {
1231 $columnexceptionmsg = $column . '" in table "' . $table . '"';
1232 throw new ElementNotFoundException($this->getSession(), "\n$columnheaderxpath\n\n".'Column', null, $columnexceptionmsg);
1233 }
1234 // Following conditions were considered before finding column count.
1235 // 1. Table header can be in thead/tr/th or tbody/tr/td[1].
1236 // 2. First column can have th (Gradebook -> user report), so having lenient sibling check.
1237 $columnpositionxpath = "/child::*[position() = count(" . $tablexpath . "/" . $theadheaderxpath .
1238 "/preceding-sibling::*) + 1]";
641459a8
RT
1239 }
1240
1241 // Check if value exists in specific row/column.
1242 // Get row xpath.
e01012e9
RT
1243 // GoutteDriver uses DomCrawler\Crawler and it is making XPath relative to the current context, so use descendant.
1244 $rowxpath = $tablexpath."/tbody/tr[descendant::th[normalize-space(.)=" . $rowliteral .
1245 "] | descendant::td[normalize-space(.)=" . $rowliteral . "]]";
641459a8 1246
bd855fd5 1247 $columnvaluexpath = $rowxpath . $columnpositionxpath . "[contains(normalize-space(.)," . $valueliteral . ")]";
97329f1b 1248
641459a8
RT
1249 // Looks for the requested node inside the container node.
1250 $coumnnode = $this->getSession()->getDriver()->find($columnvaluexpath);
1251 if (empty($coumnnode)) {
97329f1b
MG
1252 $locatorexceptionmsg = $value . '" in "' . $row . '" row with column "' . $column;
1253 throw new ElementNotFoundException($this->getSession(), "\n$columnvaluexpath\n\n".'Column value', null, $locatorexceptionmsg);
641459a8
RT
1254 }
1255 }
1256
1257 /**
1258 * Checks the provided value should not exist in specific row/column of table.
1259 *
1260 * @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should not contain "(?P<value_string>[^"]*)"$/
1261 * @throws ElementNotFoundException
1262 * @param string $row row text which will be looked in.
1263 * @param string $column column text to search
1264 * @param string $table table id/class/caption
1265 * @param string $value text to check.
1266 */
1267 public function row_column_of_table_should_not_contain($row, $column, $table, $value) {
1268 try {
1269 $this->row_column_of_table_should_contain($row, $column, $table, $value);
641459a8
RT
1270 } catch (ElementNotFoundException $e) {
1271 // Table row/column doesn't contain this value. Nothing to do.
1272 return;
1273 }
ca0ceacd
TH
1274 // Throw exception if found.
1275 throw new ExpectationException(
1276 '"' . $column . '" with value "' . $value . '" is present in "' . $row . '" row for table "' . $table . '"',
1277 $this->getSession()
1278 );
641459a8
RT
1279 }
1280
1281 /**
1282 * Checks that the provided value exist in table.
1283 * More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
1284 *
97329f1b
MG
1285 * First row may contain column headers or numeric indexes of the columns
1286 * (syntax -1- is also considered to be column index). Column indexes are
1287 * useful in case of multirow headers and/or presence of cells with colspan.
1288 *
641459a8
RT
1289 * @Then /^the following should exist in the "(?P<table_string>[^"]*)" table:$/
1290 * @throws ExpectationException
1291 * @param string $table name of table
1292 * @param TableNode $data table with first row as header and following values
1293 * | Header 1 | Header 2 | Header 3 |
1294 * | Value 1 | Value 2 | Value 3|
1295 */
08678f6f 1296 public function following_should_exist_in_the_table($table, TableNode $data) {
641459a8
RT
1297 $datahash = $data->getHash();
1298
97329f1b
MG
1299 foreach ($datahash as $row) {
1300 $firstcell = null;
1301 foreach ($row as $column => $value) {
1302 if ($firstcell === null) {
1303 $firstcell = $value;
1304 } else {
1305 $this->row_column_of_table_should_contain($firstcell, $column, $table, $value);
1306 }
641459a8
RT
1307 }
1308 }
1309 }
1310
1311 /**
1312 * Checks that the provided value exist in table.
1313 * More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
1314 *
1315 * @Then /^the following should not exist in the "(?P<table_string>[^"]*)" table:$/
1316 * @throws ExpectationException
1317 * @param string $table name of table
1318 * @param TableNode $data table with first row as header and following values
1319 * | Header 1 | Header 2 | Header 3 |
1320 * | Value 1 | Value 2 | Value 3|
1321 */
08678f6f 1322 public function following_should_not_exist_in_the_table($table, TableNode $data) {
641459a8
RT
1323 $datahash = $data->getHash();
1324
1325 foreach ($datahash as $value) {
1326 $row = array_shift($value);
1327 foreach ($value as $column => $value) {
1328 try {
1329 $this->row_column_of_table_should_contain($row, $column, $table, $value);
1330 // Throw exception if found.
641459a8
RT
1331 } catch (ElementNotFoundException $e) {
1332 // Table row/column doesn't contain this value. Nothing to do.
1333 continue;
1334 }
ca0ceacd
TH
1335 throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
1336 $row . '" row for table "' . $table . '"', $this->getSession()
1337 );
641459a8
RT
1338 }
1339 }
1340 }
cb7db63b
TH
1341
1342 /**
1343 * Given the text of a link, download the linked file and return the contents.
1344 *
1345 * This is a helper method used by {@link following_should_download_bytes()}
1346 * and {@link following_should_download_between_and_bytes()}
1347 *
1348 * @param string $link the text of the link.
1349 * @return string the content of the downloaded file.
1350 */
df003a85 1351 public function download_file_from_link($link) {
cb7db63b
TH
1352 // Find the link.
1353 $linknode = $this->find_link($link);
1354 $this->ensure_node_is_visible($linknode);
1355
1356 // Get the href and check it.
1357 $url = $linknode->getAttribute('href');
1358 if (!$url) {
1359 throw new ExpectationException('Download link does not have href attribute',
1360 $this->getSession());
1361 }
1362 if (!preg_match('~^https?://~', $url)) {
1363 throw new ExpectationException('Download link not an absolute URL: ' . $url,
1364 $this->getSession());
1365 }
1366
1367 // Download the URL and check the size.
1368 $session = $this->getSession()->getCookie('MoodleSession');
1369 return download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
1370 }
1371
1372 /**
1373 * Downloads the file from a link on the page and checks the size.
1374 *
1375 * Only works if the link has an href attribute. Javascript downloads are
1376 * not supported. Currently, the href must be an absolute URL.
1377 *
1378 * @Then /^following "(?P<link_string>[^"]*)" should download "(?P<expected_bytes>\d+)" bytes$/
1379 * @throws ExpectationException
1380 * @param string $link the text of the link.
1381 * @param number $expectedsize the expected file size in bytes.
1382 */
1383 public function following_should_download_bytes($link, $expectedsize) {
c3b72e58
RT
1384 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
1385
1386 // It will stop spinning once file is downloaded or time out.
1387 $result = $this->spin(
1388 function($context, $args) {
1389 $link = $args['link'];
1390 return $this->download_file_from_link($link);
1391 },
1392 array('link' => $link),
1393 self::EXTENDED_TIMEOUT,
1394 $exception
1395 );
1396
1397 // Check download size.
cb7db63b
TH
1398 $actualsize = (int)strlen($result);
1399 if ($actualsize !== (int)$expectedsize) {
1400 throw new ExpectationException('Downloaded data was ' . $actualsize .
1401 ' bytes, expecting ' . $expectedsize, $this->getSession());
1402 }
1403 }
1404
1405 /**
1406 * Downloads the file from a link on the page and checks the size is in a given range.
1407 *
1408 * Only works if the link has an href attribute. Javascript downloads are
1409 * not supported. Currently, the href must be an absolute URL.
1410 *
1411 * The range includes the endpoints. That is, a 10 byte file in considered to
1412 * be between "5" and "10" bytes, and between "10" and "20" bytes.
1413 *
1414 * @Then /^following "(?P<link_string>[^"]*)" should download between "(?P<min_bytes>\d+)" and "(?P<max_bytes>\d+)" bytes$/
1415 * @throws ExpectationException
1416 * @param string $link the text of the link.
1417 * @param number $minexpectedsize the minimum expected file size in bytes.
1418 * @param number $maxexpectedsize the maximum expected file size in bytes.
1419 */
1420 public function following_should_download_between_and_bytes($link, $minexpectedsize, $maxexpectedsize) {
1421 // If the minimum is greater than the maximum then swap the values.
1422 if ((int)$minexpectedsize > (int)$maxexpectedsize) {
1423 list($minexpectedsize, $maxexpectedsize) = array($maxexpectedsize, $minexpectedsize);
1424 }
1425
c3b72e58
RT
1426 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
1427
1428 // It will stop spinning once file is downloaded or time out.
1429 $result = $this->spin(
1430 function($context, $args) {
1431 $link = $args['link'];
1432
1433 return $this->download_file_from_link($link);
1434 },
1435 array('link' => $link),
1436 self::EXTENDED_TIMEOUT,
1437 $exception
1438 );
1439
1440 // Check download size.
cb7db63b
TH
1441 $actualsize = (int)strlen($result);
1442 if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) {
1443 throw new ExpectationException('Downloaded data was ' . $actualsize .
1444 ' bytes, expecting between ' . $minexpectedsize . ' and ' .
1445 $maxexpectedsize, $this->getSession());
1446 }
1447 }
a109a3ca
TH
1448
1449 /**
1450 * Prepare to detect whether or not a new page has loaded (or the same page reloaded) some time in the future.
a33fed21 1451 *
a109a3ca
TH
1452 * @Given /^I start watching to see if a new page loads$/
1453 */
1454 public function i_start_watching_to_see_if_a_new_page_loads() {
1455 if (!$this->running_javascript()) {
1456 throw new DriverException('Page load detection requires JavaScript.');
1457 }
1458
9f3a68fe
RT
1459 $session = $this->getSession();
1460
1461 if ($this->pageloaddetectionrunning || $session->getPage()->find('xpath', $this->get_page_load_xpath())) {
a33fed21
SH
1462 // If we find this node at this point we are already watching for a reload and the behat steps
1463 // are out of order. We will treat this as an error - really it needs to be fixed as it indicates a problem.
9f3a68fe
RT
1464 throw new ExpectationException(
1465 'Page load expectation error: page reloads are already been watched for.', $session);
a33fed21
SH
1466 }
1467
1b2c35af
AN
1468 $this->pageloaddetectionrunning = true;
1469
42ad096f 1470 $session->executeScript(
a109a3ca 1471 'var span = document.createElement("span");
a33fed21 1472 span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '");
a109a3ca
TH
1473 span.setAttribute("style", "display: none;");
1474 document.body.appendChild(span);');
1475 }
1476
1477 /**
a33fed21
SH
1478 * Verify that a new page has loaded (or the same page has reloaded) since the
1479 * last "I start watching to see if a new page loads" step.
1480 *
a109a3ca
TH
1481 * @Given /^a new page should have loaded since I started watching$/
1482 */
1483 public function a_new_page_should_have_loaded_since_i_started_watching() {
9f3a68fe
RT
1484 $session = $this->getSession();
1485
1486 // Make sure page load tracking was started.
1487 if (!$this->pageloaddetectionrunning) {
a33fed21 1488 throw new ExpectationException(
9f3a68fe 1489 'Page load expectation error: page load tracking was not started.', $session);
a33fed21 1490 }
1b2c35af 1491
9f3a68fe
RT
1492 // As the node is inserted by code above it is either there or not, and we do not need spin and it is safe
1493 // to use the native API here which is great as exception handling (the alternative is slow).
1494 if ($session->getPage()->find('xpath', $this->get_page_load_xpath())) {
1495 // We don't want to find this node, if we do we have an error.
1b2c35af 1496 throw new ExpectationException(
9f3a68fe 1497 'Page load expectation error: a new page has not been loaded when it should have been.', $session);
1b2c35af
AN
1498 }
1499
9f3a68fe 1500 // Cancel the tracking of pageloaddetectionrunning.
1b2c35af 1501 $this->pageloaddetectionrunning = false;
a109a3ca
TH
1502 }
1503
1504 /**
a33fed21
SH
1505 * Verify that a new page has not loaded (or the same page has reloaded) since the
1506 * last "I start watching to see if a new page loads" step.
1507 *
a109a3ca
TH
1508 * @Given /^a new page should not have loaded since I started watching$/
1509 */
1510 public function a_new_page_should_not_have_loaded_since_i_started_watching() {
a610c9ab
EL
1511 $session = $this->getSession();
1512
9f3a68fe
RT
1513 // Make sure page load tracking was started.
1514 if (!$this->pageloaddetectionrunning) {
1515 throw new ExpectationException(
1516 'Page load expectation error: page load tracking was not started.', $session);
1517 }
1518
a33fed21
SH
1519 // We use our API here as we can use the exception handling provided by it.
1520 $this->find(
1521 'xpath',
1522 $this->get_page_load_xpath(),
1523 new ExpectationException(
1524 'Page load expectation error: A new page has been loaded when it should not have been.',
1525 $this->getSession()
1526 )
1527 );
a109a3ca
TH
1528 }
1529
1530 /**
1531 * Helper used by {@link a_new_page_should_have_loaded_since_i_started_watching}
1532 * and {@link a_new_page_should_not_have_loaded_since_i_started_watching}
1533 * @return string xpath expression.
1534 */
1535 protected function get_page_load_xpath() {
a33fed21 1536 return "//span[@data-rel = '" . self::PAGE_LOAD_DETECTION_STRING . "']";
a109a3ca 1537 }
d58b0ad6
RT
1538
1539 /**
1540 * Wait unit user press Enter/Return key. Useful when debugging a scenario.
1541 *
1542 * @Then /^(?:|I )pause(?:| scenario execution)$/
1543 */
1544 public function i_pause_scenario_executon() {
1545 global $CFG;
1546
1547 $posixexists = function_exists('posix_isatty');
1548
1549 // Make sure this step is only used with interactive terminal (if detected).
1550 if ($posixexists && !@posix_isatty(STDOUT)) {
1551 $session = $this->getSession();
1552 throw new ExpectationException('Break point should only be used with interative terminal.', $session);
1553 }
1554
1555 // Windows don't support ANSI code by default, but with ANSICON.
1556 $isansicon = getenv('ANSICON');
1557 if (($CFG->ostype === 'WINDOWS') && empty($isansicon)) {
1558 fwrite(STDOUT, "Paused. Press Enter/Return to continue.");
1559 fread(STDIN, 1024);
1560 } else {
1561 fwrite(STDOUT, "\033[s\n\033[0;93mPaused. Press \033[1;31mEnter/Return\033[0;93m to continue.\033[0m");
1562 fread(STDIN, 1024);
1563 fwrite(STDOUT, "\033[2A\033[u\033[2B");
1564 }
1565 }
051b45f8
MN
1566
1567 /**
1568 * Presses a given button in the browser.
cd3a6a78 1569 * NOTE: Phantomjs and goutte driver reloads page while navigating back and forward.
051b45f8
MN
1570 *
1571 * @Then /^I press the "(back|forward|reload)" button in the browser$/
1572 * @param string $button the button to press.
1573 * @throws ExpectationException
1574 */
1575 public function i_press_in_the_browser($button) {
1576 $session = $this->getSession();
1577
1578 if ($button == 'back') {
1579 $session->back();
1580 } else if ($button == 'forward') {
1581 $session->forward();
1582 } else if ($button == 'reload') {
1583 $session->reload();
1584 } else {
1585 throw new ExpectationException('Unknown browser button.', $session);
1586 }
1587 }
77b08110
RW
1588
1589 /**
1590 * Trigger a keydown event for a key on a specific element.
1591 *
1592 * @When /^I press key "(?P<key_string>(?:[^"]|\\")*)" in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
1593 * @param string $key either char-code or character itself,
1594 * may optionally be prefixed with ctrl-, alt-, shift- or meta-
1595 * @param string $element Element we look for
1596 * @param string $selectortype The type of what we look for
1597 * @throws DriverException
1598 * @throws ExpectationException
1599 */
1600 public function i_press_key_in_element($key, $element, $selectortype) {
1601 if (!$this->running_javascript()) {
1602 throw new DriverException('Key down step is not available with Javascript disabled');
1603 }
1604 // Gets the node based on the requested selector type and locator.
1605 $node = $this->get_selected_node($selectortype, $element);
1606 $modifier = null;
1607 $validmodifiers = array('ctrl', 'alt', 'shift', 'meta');
1608 $char = $key;
1609 if (strpos($key, '-')) {
1610 list($modifier, $char) = preg_split('/-/', $key, 2);
1611 $modifier = strtolower($modifier);
1612 if (!in_array($modifier, $validmodifiers)) {
1613 throw new ExpectationException(sprintf('Unknown key modifier: %s.', $modifier));
1614 }
1615 }
1616 if (is_numeric($char)) {
1617 $char = (int)$char;
1618 }
1619
1620 $node->keyDown($char, $modifier);
1621 $node->keyPress($char, $modifier);
1622 $node->keyUp($char, $modifier);
1623 }
fba0ac63
RT
1624
1625 /**
1626 * Press tab key on a specific element.
1627 *
1628 * @When /^I press tab key in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
1629 * @param string $element Element we look for
1630 * @param string $selectortype The type of what we look for
1631 * @throws DriverException
1632 * @throws ExpectationException
1633 */
1634 public function i_post_tab_key_in_element($element, $selectortype) {
1635 if (!$this->running_javascript()) {
1636 throw new DriverException('Tab press step is not available with Javascript disabled');
1637 }
1638 // Gets the node based on the requested selector type and locator.
1639 $node = $this->get_selected_node($selectortype, $element);
b5be3875
MN
1640 $driver = $this->getSession()->getDriver();
1641 if ($driver instanceof \Moodle\BehatExtension\Driver\MoodleSelenium2Driver) {
1642 $driver->post_key("\xEE\x80\x84", $node->getXpath());
1643 } else {
1644 $driver->keyDown($node->getXpath(), "\t");
1645 }
fba0ac63 1646 }
99ad3223
RT
1647
1648 /**
1649 * Checks if database family used is using one of the specified, else skip. (mysql, postgres, mssql, oracle, etc.)
1650 *
1651 * @Given /^database family used is one of the following:$/
1652 * @param TableNode $databasefamilies list of database.
1653 * @return void.
1654 * @throws \Moodle\BehatExtension\Exception\SkippedException
1655 */
1656 public function database_family_used_is_one_of_the_following(TableNode $databasefamilies) {
1657 global $DB;
1658
1659 $dbfamily = $DB->get_dbfamily();
1660
1661 // Check if used db family is one of the specified ones. If yes then return.
1662 foreach ($databasefamilies->getRows() as $dbfamilytocheck) {
1663 if ($dbfamilytocheck[0] == $dbfamily) {
1664 return;
1665 }
1666 }
1667
1668 throw new \Moodle\BehatExtension\Exception\SkippedException();
1669 }
26872a80
K
1670
1671 /**
1672 * Checks focus is with the given element.
1673 *
1674 * @Then /^the focused element is( not)? "(?P<node_string>(?:[^"]|\\")*)" "(?P<node_selector_string>[^"]*)"$/
1675 * @param string $not optional step verifier
1676 * @param string $nodeelement Element identifier
1677 * @param string $nodeselectortype Element type
0cc0f72c 1678 * @throws DriverException If not using JavaScript
26872a80
K
1679 * @throws ExpectationException
1680 */
1681 public function the_focused_element_is($not, $nodeelement, $nodeselectortype) {
1682 if (!$this->running_javascript()) {
0cc0f72c 1683 throw new DriverException('Checking focus on an element requires JavaScript');
26872a80
K
1684 }
1685 list($a, $b) = $this->transform_selector($nodeselectortype, $nodeelement);
1686 $element = $this->find($a, $b);
1687 $xpath = addslashes_js($element->getXpath());
1688 $script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
1689 document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
1690 $targetisfocused = $this->getSession()->evaluateScript($script);
1691 if ($not == ' not') {
1692 if ($targetisfocused) {
1693 throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
1694 }
1695 } else {
1696 if (!$targetisfocused) {
1697 throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
1698 }
1699 }
1700 }
1701
1702 /**
1703 * Checks focus is with the given element.
1704 *
1705 * @Then /^the focused element is( not)? "(?P<n>(?:[^"]|\\")*)" "(?P<ns>[^"]*)" in the "(?P<c>(?:[^"]|\\")*)" "(?P<cs>[^"]*)"$/
1706 * @param string $not string optional step verifier
1707 * @param string $element Element identifier
1708 * @param string $selectortype Element type
1709 * @param string $nodeelement Element we look in
1710 * @param string $nodeselectortype The type of selector where we look in
0cc0f72c 1711 * @throws DriverException If not using JavaScript
26872a80
K
1712 * @throws ExpectationException
1713 */
1714 public function the_focused_element_is_in_the($not, $element, $selectortype, $nodeelement, $nodeselectortype) {
1715 if (!$this->running_javascript()) {
0cc0f72c 1716 throw new DriverException('Checking focus on an element requires JavaScript');
26872a80
K
1717 }
1718 $element = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
1719 $xpath = addslashes_js($element->getXpath());
1720 $script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
1721 document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
1722 $targetisfocused = $this->getSession()->evaluateScript($script);
1723 if ($not == ' not') {
1724 if ($targetisfocused) {
1725 throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
1726 }
1727 } else {
1728 if (!$targetisfocused) {
1729 throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
1730 }
1731 }
1732 }
1733
1734 /**
1735 * Manually press tab key.
1736 *
1737 * @When /^I press( shift)? tab$/
1738 * @param string $shift string optional step verifier
1739 * @throws DriverException
1740 */
1741 public function i_manually_press_tab($shift = '') {
1742 if (!$this->running_javascript()) {
1743 throw new DriverException($shift . ' Tab press step is not available with Javascript disabled');
1744 }
1745
1746 $value = ($shift == ' shift') ? [\WebDriver\Key::SHIFT . \WebDriver\Key::TAB] : [\WebDriver\Key::TAB];
1747 $this->getSession()->getDriver()->getWebDriverSession()->activeElement()->postValue(['value' => $value]);
1748 }
30e1f5a0
TQ
1749
1750 /**
1751 * Trigger click on node via javascript instead of actually clicking on it via pointer.
1752 * This function resolves the issue of nested elements.
1753 *
1754 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" skipping visibility check$/
1755 * @param string $element
1756 * @param string $selectortype
1757 */
1758 public function i_click_on_skipping_visibility_check($element, $selectortype) {
1759
1760 // Gets the node based on the requested selector type and locator.
1761 $node = $this->get_selected_node($selectortype, $element);
1762 $this->js_trigger_click($node);
1763 }
786ea937 1764}