Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

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