MDL-42625 TinyMCE: Hook into API to track the progress of the init script
[moodle.git] / lib / tests / behat / behat_general.php
CommitLineData
786ea937
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * General use steps definitions.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../behat/behat_base.php');
29
ca4f33a7 30use Behat\Mink\Exception\ExpectationException as ExpectationException,
d0a9a29b 31 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
bda1dea4 32 Behat\Mink\Exception\DriverException as DriverException,
39ec8285
DM
33 WebDriver\Exception\NoSuchElement as NoSuchElement,
34 WebDriver\Exception\StaleElementReference as StaleElementReference;
786ea937
DM
35
36/**
37 * Cross component steps definitions.
38 *
39 * Basic web application definitions from MinkExtension and
40 * BehatchExtension. Definitions modified according to our needs
41 * when necessary and including only the ones we need to avoid
42 * overlapping and confusion.
43 *
44 * @package core
45 * @category test
46 * @copyright 2012 David MonllaĆ³
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
49class behat_general extends behat_base {
50
51 /**
52 * Opens Moodle homepage.
53 *
786ea937
DM
54 * @Given /^I am on homepage$/
55 */
56 public function i_am_on_homepage() {
40923977 57 $this->getSession()->visit($this->locate_path('/'));
786ea937
DM
58 }
59
18c84063
DM
60 /**
61 * Reloads the current page.
62 *
63 * @Given /^I reload the page$/
64 */
65 public function reload() {
66 $this->getSession()->reload();
67 }
68
d0a9a29b
DM
69 /**
70 * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection
71 *
72 * @Given /^I wait to be redirected$/
73 */
74 public function i_wait_to_be_redirected() {
75
76 // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and
77 // moodle_page::$periodicrefreshdelay possible values.
78 if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) {
79 // We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
80 return false;
81 }
82
bda1dea4
DM
83 // Wrapped in try & catch in case the redirection has already been executed.
84 try {
85 $content = $metarefresh->getAttribute('content');
86 } catch (NoSuchElement $e) {
87 return false;
39ec8285
DM
88 } catch (StaleElementReference $e) {
89 return false;
bda1dea4
DM
90 }
91
92 // Getting the refresh time and the url if present.
d0a9a29b
DM
93 if (strstr($content, 'url') != false) {
94
bda1dea4 95 list($waittime, $url) = explode(';', $content);
d0a9a29b
DM
96
97 // Cleaning the URL value.
98 $url = trim(substr($url, strpos($url, 'http')));
99
100 } else {
101 // Just wait then.
102 $waittime = $content;
103 }
104
105
106 // Wait until the URL change is executed.
107 if ($this->running_javascript()) {
108 $this->getSession()->wait($waittime * 1000, false);
109
110 } else if (!empty($url)) {
111 // We redirect directly as we can not wait for an automatic redirection.
112 $this->getSession()->getDriver()->getClient()->request('get', $url);
113
114 } else {
115 // Reload the page if no URL was provided.
116 $this->getSession()->getDriver()->reload();
117 }
118 }
119
e5eff0b6
AA
120 /**
121 * Switches to the specified iframe.
122 *
123 * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" iframe$/
124 * @param string $iframename
125 */
126 public function switch_to_iframe($iframename) {
d1e55a47
DM
127
128 // We spin to give time to the iframe to be loaded.
129 // Using extended timeout as we don't know about which
130 // kind of iframe will be loaded.
131 $this->spin(
132 function($context, $iframename) {
133 $context->getSession()->switchToIFrame($iframename);
134
135 // If no exception we are done.
136 return true;
137 },
138 $iframename,
139 self::EXTENDED_TIMEOUT
140 );
e5eff0b6
AA
141 }
142
143 /**
144 * Switches to the main Moodle frame.
145 *
146 * @Given /^I switch to the main frame$/
147 */
148 public function switch_to_the_main_frame() {
149 $this->getSession()->switchToIFrame();
150 }
151
1303eb29
DM
152 /**
153 * Switches to the specified window. Useful when interacting with popup windows.
154 *
155 * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
156 * @param string $windowname
157 */
158 public function switch_to_window($windowname) {
159 $this->getSession()->switchToWindow($windowname);
160 }
161
162 /**
163 * Switches to the main Moodle window. Useful when you finish interacting with popup windows.
164 *
165 * @Given /^I switch to the main window$/
166 */
167 public function switch_to_the_main_window() {
168 $this->getSession()->switchToWindow();
169 }
170
563514b1 171 /**
7daab401 172 * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
563514b1
DM
173 * @Given /^I accept the currently displayed dialog$/
174 */
175 public function accept_currently_displayed_alert_dialog() {
176 $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
177 }
178
786ea937
DM
179 /**
180 * Clicks link with specified id|title|alt|text.
181 *
786ea937 182 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
1f9ffbdb 183 * @throws ElementNotFoundException Thrown by behat_base::find
40923977 184 * @param string $link
786ea937
DM
185 */
186 public function click_link($link) {
1f9ffbdb
DM
187
188 $linknode = $this->find_link($link);
d1e55a47 189 $this->ensure_node_is_visible($linknode);
1f9ffbdb 190 $linknode->click();
786ea937
DM
191 }
192
193 /**
194 * Waits X seconds. Required after an action that requires data from an AJAX request.
195 *
196 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
197 * @param int $seconds
198 */
199 public function i_wait_seconds($seconds) {
d0a9a29b
DM
200
201 if (!$this->running_javascript()) {
202 throw new DriverException('Waits are disabled in scenarios without Javascript support');
203 }
204
786ea937
DM
205 $this->getSession()->wait($seconds * 1000, false);
206 }
207
208 /**
209 * Waits until the page is completely loaded. This step is auto-executed after every step.
210 *
211 * @Given /^I wait until the page is ready$/
212 */
213 public function wait_until_the_page_is_ready() {
d0a9a29b
DM
214
215 if (!$this->running_javascript()) {
216 throw new DriverException('Waits are disabled in scenarios without Javascript support');
217 }
218
d1e55a47
DM
219 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
220 }
221
222 /**
223 * Waits until the provided element selector exists in the DOM
224 *
225 * Using the protected method as this method will be usually
226 * called by other methods which are not returning a set of
227 * steps and performs the actions directly, so it would not
228 * be executed if it returns another step.
229
230 * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" exists$/
231 * @param string $element
232 * @param string $selector
233 * @return void
234 */
235 public function wait_until_exists($element, $selectortype) {
236 $this->ensure_element_exists($element, $selectortype);
237 }
238
239 /**
240 * Waits until the provided element does not exist in the DOM
241 *
242 * Using the protected method as this method will be usually
243 * called by other methods which are not returning a set of
244 * steps and performs the actions directly, so it would not
245 * be executed if it returns another step.
246
247 * @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" does not exist$/
248 * @param string $element
249 * @param string $selector
250 * @return void
251 */
252 public function wait_until_does_not_exists($element, $selectortype) {
253 $this->ensure_element_does_not_exist($element, $selectortype);
786ea937
DM
254 }
255
256 /**
40923977 257 * Generic mouse over action. Mouse over a element of the specified type.
786ea937 258 *
40923977
DM
259 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
260 * @param string $element Element we look for
261 * @param string $selectortype The type of what we look for
786ea937 262 */
40923977 263 public function i_hover($element, $selectortype) {
1f9ffbdb 264
40923977
DM
265 // Gets the node based on the requested selector type and locator.
266 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
267 $node->mouseOver();
268 }
269
40923977
DM
270 /**
271 * Generic click action. Click on the element of the specified type.
272 *
273 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
274 * @param string $element Element we look for
275 * @param string $selectortype The type of what we look for
276 */
277 public function i_click_on($element, $selectortype) {
278
279 // Gets the node based on the requested selector type and locator.
280 $node = $this->get_selected_node($selectortype, $element);
d1e55a47 281 $this->ensure_node_is_visible($node);
40923977
DM
282 $node->click();
283 }
284
072f67fc
DM
285 /**
286 * Click on the element of the specified type which is located inside the second element.
287 *
288 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
289 * @param string $element Element we look for
290 * @param string $selectortype The type of what we look for
291 * @param string $nodeelement Element we look in
292 * @param string $nodeselectortype The type of selector where we look in
293 */
294 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
295
296 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
d1e55a47 297 $this->ensure_node_is_visible($node);
072f67fc
DM
298 $node->click();
299 }
300
563514b1 301 /**
7daab401 302 * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
563514b1
DM
303 *
304 * The steps definitions calling this step as part of them should
305 * manage the wait times by themselves as the times and when the
306 * waits should be done depends on what is being dragged & dropper.
307 *
308 * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
309 * @param string $element
310 * @param string $selectortype
311 * @param string $containerelement
312 * @param string $containerselectortype
313 */
314 public function i_drag_and_i_drop_it_in($element, $selectortype, $containerelement, $containerselectortype) {
315
316 list($sourceselector, $sourcelocator) = $this->transform_selector($selectortype, $element);
317 $sourcexpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($sourceselector, $sourcelocator);
318
319 list($containerselector, $containerlocator) = $this->transform_selector($containerselectortype, $containerelement);
320 $destinationxpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($containerselector, $containerlocator);
321
322 $this->getSession()->getDriver()->dragTo($sourcexpath, $destinationxpath);
323 }
324
63950e4d
DM
325 /**
326 * Checks, that the specified element is visible. Only available in tests using Javascript.
327 *
328 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
329 * @throws ElementNotFoundException
330 * @throws ExpectationException
331 * @throws DriverException
332 * @param string $element
333 * @param string $selectortype
334 * @return void
335 */
336 public function should_be_visible($element, $selectortype) {
337
338 if (!$this->running_javascript()) {
339 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
340 }
341
342 $node = $this->get_selected_node($selectortype, $element);
343 if (!$node->isVisible()) {
344 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
345 }
346 }
347
348 /**
349 * Checks, that the specified element is not visible. Only available in tests using Javascript.
350 *
351 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
352 * @throws ElementNotFoundException
353 * @throws ExpectationException
354 * @param string $element
355 * @param string $selectortype
356 * @return void
357 */
358 public function should_not_be_visible($element, $selectortype) {
359
360 try {
361 $this->should_be_visible($element, $selectortype);
362 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
363 } catch (ExpectationException $e) {
364 // All as expected.
365 }
366 }
367
368 /**
369 * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
370 *
371 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
372 * @throws ElementNotFoundException
373 * @throws DriverException
374 * @throws ExpectationException
375 * @param string $element Element we look for
376 * @param string $selectortype The type of what we look for
377 * @param string $nodeelement Element we look in
378 * @param string $nodeselectortype The type of selector where we look in
379 */
380 public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
381
382 if (!$this->running_javascript()) {
383 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
384 }
385
386 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
387 if (!$node->isVisible()) {
388 throw new ExpectationException(
389 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
390 $this->getSession()
391 );
392 }
393 }
394
395 /**
396 * Checks, that the specified element is not visible inside the specified container. Only available in tests using Javascript.
397 *
398 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
399 * @throws ElementNotFoundException
400 * @throws ExpectationException
401 * @param string $element Element we look for
402 * @param string $selectortype The type of what we look for
403 * @param string $nodeelement Element we look in
404 * @param string $nodeselectortype The type of selector where we look in
405 */
406 public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
407
408 try {
409 $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
410 throw new ExpectationException(
411 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
412 $this->getSession()
413 );
414 } catch (ExpectationException $e) {
415 // All as expected.
416 }
417 }
418
786ea937 419 /**
e9af3ed3 420 * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
786ea937 421 *
786ea937 422 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 423 * @throws ExpectationException
40923977 424 * @param string $text
786ea937
DM
425 */
426 public function assert_page_contains_text($text) {
9a1f4922 427
e9af3ed3
DM
428 // Looking for all the matching nodes without any other descendant matching the
429 // same xpath (we are using contains(., ....).
9a1f4922 430 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
e9af3ed3
DM
431 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
432 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
9a1f4922
DM
433
434 // Wait until it finds the text, otherwise custom exception.
435 try {
e9af3ed3
DM
436 $nodes = $this->find_all('xpath', $xpath);
437
438 // We also check for the element visibility when running JS tests.
439 if ($this->running_javascript()) {
440 foreach ($nodes as $node) {
441 if ($node->isVisible()) {
442 return;
443 }
444 }
445
446 throw new ExpectationException("'{$text}' text was found but was not visible", $this->getSession());
447 }
448
9a1f4922
DM
449 } catch (ElementNotFoundException $e) {
450 throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
451 }
786ea937
DM
452 }
453
454 /**
e9af3ed3 455 * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 456 *
786ea937 457 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 458 * @throws ExpectationException
40923977 459 * @param string $text
786ea937
DM
460 */
461 public function assert_page_not_contains_text($text) {
9a1f4922 462
5458ab3e 463 // Delegating the process to assert_page_contains_text.
9a1f4922 464 try {
5458ab3e
DM
465 $this->assert_page_contains_text($text);
466 } catch (ExpectationException $e) {
467 // It should not appear, so this is good.
468 return;
9a1f4922 469 }
5458ab3e
DM
470
471 // If the page contains the text this is failing.
472 throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
786ea937
DM
473 }
474
475 /**
e9af3ed3 476 * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 477 *
40923977 478 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
479 * @throws ElementNotFoundException
480 * @throws ExpectationException
40923977
DM
481 * @param string $text
482 * @param string $element Element we look in.
483 * @param string $selectortype The type of element where we are looking in.
786ea937 484 */
40923977
DM
485 public function assert_element_contains_text($text, $element, $selectortype) {
486
5458ab3e
DM
487 // Getting the container where the text should be found.
488 $container = $this->get_selected_node($selectortype, $element);
489
e9af3ed3
DM
490 // Looking for all the matching nodes without any other descendant matching the
491 // same xpath (we are using contains(., ....).
5458ab3e 492 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
e9af3ed3
DM
493 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
494 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
5458ab3e
DM
495
496 // Wait until it finds the text inside the container, otherwise custom exception.
497 try {
e9af3ed3
DM
498 $nodes = $this->find_all('xpath', $xpath, false, $container);
499
500 // We also check for the element visibility when running JS tests.
501 if ($this->running_javascript()) {
502 foreach ($nodes as $node) {
503 if ($node->isVisible()) {
504 return;
505 }
506 }
507
508 throw new ExpectationException("'{$text}' text was found in the {$element} element but was not visible", $this->getSession());
509 }
510
5458ab3e
DM
511 } catch (ElementNotFoundException $e) {
512 throw new ExpectationException('"' . $text . '" text was not found in the ' . $element . ' element', $this->getSession());
513 }
514
786ea937
DM
515 }
516
517 /**
e9af3ed3 518 * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 519 *
40923977 520 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
521 * @throws ElementNotFoundException
522 * @throws ExpectationException
40923977
DM
523 * @param string $text
524 * @param string $element Element we look in.
525 * @param string $selectortype The type of element where we are looking in.
786ea937 526 */
40923977
DM
527 public function assert_element_not_contains_text($text, $element, $selectortype) {
528
5458ab3e
DM
529 // Delegating the process to assert_element_contains_text.
530 try {
531 $this->assert_element_contains_text($text, $element, $selectortype);
532 } catch (ExpectationException $e) {
533 // It should not appear, so this is good.
534 // We only catch ExpectationException as ElementNotFoundException
535 // will be thrown if the container does not exist.
536 return;
537 }
538
539 // If the element contains the text this is failing.
540 throw new ExpectationException('"' . $text . '" text was found in the ' . $element . ' element', $this->getSession());
786ea937
DM
541 }
542
60054942
DM
543 /**
544 * Checks, that the first specified element appears before the second one.
545 *
546 * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
547 * @throws ExpectationException
548 * @param string $preelement The locator of the preceding element
549 * @param string $preselectortype The locator of the preceding element
550 * @param string $postelement The locator of the latest element
551 * @param string $postselectortype The selector type of the latest element
552 */
553 public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
554
555 // We allow postselectortype as a non-text based selector.
556 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
557 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
558
559 $prexpath = $this->find($preselector, $prelocator)->getXpath();
560 $postxpath = $this->find($postselector, $postlocator)->getXpath();
561
562 // Using following xpath axe to find it.
563 $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
564 $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
565 if (!$this->getSession()->getDriver()->find($xpath)) {
566 throw new ExpectationException($msg, $this->getSession());
567 }
568 }
569
570 /**
571 * Checks, that the first specified element appears after the second one.
572 *
573 * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
574 * @throws ExpectationException
575 * @param string $postelement The locator of the latest element
576 * @param string $postselectortype The selector type of the latest element
577 * @param string $preelement The locator of the preceding element
578 * @param string $preselectortype The locator of the preceding element
579 */
580 public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
581
582 // We allow postselectortype as a non-text based selector.
583 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
584 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
585
586 $postxpath = $this->find($postselector, $postlocator)->getXpath();
587 $prexpath = $this->find($preselector, $prelocator)->getXpath();
588
589 // Using preceding xpath axe to find it.
590 $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
591 $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
592 if (!$this->getSession()->getDriver()->find($xpath)) {
593 throw new ExpectationException($msg, $this->getSession());
594 }
595 }
596
786ea937 597 /**
40923977 598 * Checks, that element of specified type is disabled.
786ea937 599 *
40923977 600 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 601 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
602 * @param string $element Element we look in
603 * @param string $selectortype The type of element where we are looking in.
786ea937 604 */
40923977 605 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 606
40923977
DM
607 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
608 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
609
610 if (!$node->hasAttribute('disabled')) {
611 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
612 }
613 }
614
615 /**
40923977 616 * Checks, that element of specified type is enabled.
786ea937 617 *
40923977 618 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 619 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
620 * @param string $element Element we look on
621 * @param string $selectortype The type of where we look
786ea937 622 */
40923977 623 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 624
40923977
DM
625 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
626 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
627
628 if ($node->hasAttribute('disabled')) {
629 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
630 }
631 }
632
a2d3e3b6
MN
633 /**
634 * Checks the provided element and selector type are readonly on the current page.
635 *
636 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
637 * @throws ExpectationException Thrown by behat_base::find
638 * @param string $element Element we look in
639 * @param string $selectortype The type of element where we are looking in.
640 */
641 public function the_element_should_be_readonly($element, $selectortype) {
642 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
643 $node = $this->get_selected_node($selectortype, $element);
644
645 if (!$node->hasAttribute('readonly')) {
646 throw new ExpectationException('The element "' . $element . '" is not readonly', $this->getSession());
647 }
648 }
649
650 /**
651 * Checks the provided element and selector type are not readonly on the current page.
652 *
653 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
654 * @throws ExpectationException Thrown by behat_base::find
655 * @param string $element Element we look in
656 * @param string $selectortype The type of element where we are looking in.
657 */
658 public function the_element_should_not_be_readonly($element, $selectortype) {
659 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
660 $node = $this->get_selected_node($selectortype, $element);
661
662 if ($node->hasAttribute('readonly')) {
663 throw new ExpectationException('The element "' . $element . '" is readonly', $this->getSession());
664 }
665 }
666
ca4f33a7 667 /**
62eb5c46
EL
668 * Checks the provided element and selector type exists in the current page.
669 *
670 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
671 *
672 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
673 * @throws ElementNotFoundException Thrown by behat_base::find
674 * @param string $element The locator of the specified selector
675 * @param string $selectortype The selector type
676 */
677 public function should_exists($element, $selectortype) {
678
679 // Getting Mink selector and locator.
680 list($selector, $locator) = $this->transform_selector($selectortype, $element);
681
682 // Will throw an ElementNotFoundException if it does not exist.
683 $this->find($selector, $locator);
684 }
685
686 /**
62eb5c46
EL
687 * Checks that the provided element and selector type not exists in the current page.
688 *
689 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
690 *
691 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
692 * @throws ExpectationException
693 * @param string $element The locator of the specified selector
694 * @param string $selectortype The selector type
695 */
696 public function should_not_exists($element, $selectortype) {
697
698 try {
699 $this->should_exists($element, $selectortype);
700 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
62eb5c46 701 } catch (ElementNotFoundException $e) {
ca4f33a7
DM
702 // It passes.
703 return;
704 }
705 }
706
066ef320
JM
707 /**
708 * This step triggers cron like a user would do going to admin/cron.php.
709 *
710 * @Given /^I trigger cron$/
711 */
712 public function i_trigger_cron() {
713 $this->getSession()->visit($this->locate_path('/admin/cron.php'));
714 }
715
a2d3e3b6
MN
716 /**
717 * Checks that an element and selector type exists in another element and selector type on the current page.
718 *
719 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
720 *
721 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
722 * @throws ElementNotFoundException Thrown by behat_base::find
723 * @param string $element The locator of the specified selector
724 * @param string $selectortype The selector type
725 * @param string $containerelement The container selector type
726 * @param string $containerselectortype The container locator
727 */
728 public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
729 // Get the container node.
730 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
731
732 list($selector, $locator) = $this->transform_selector($selectortype, $element);
733
734 // Specific exception giving info about where can't we find the element.
735 $locatorexceptionmsg = $element . '" in the "' . $containerelement. '" "' . $containerselectortype. '"';
736 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg);
737
738 // Looks for the requested node inside the container node.
739 $this->find($selector, $locator, $exception, $containernode);
740 }
741
742 /**
743 * Checks that an element and selector type does not exist in another element and selector type on the current page.
744 *
745 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
746 *
747 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
748 * @throws ExpectationException
749 * @param string $element The locator of the specified selector
750 * @param string $selectortype The selector type
751 * @param string $containerelement The container selector type
752 * @param string $containerselectortype The container locator
753 */
754 public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
755 try {
756 $this->should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype);
757 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the "' .
758 $containerelement . '" "' . $containerselectortype . '"', $this->getSession());
759 } catch (ElementNotFoundException $e) {
760 // It passes.
761 return;
762 }
763 }
786ea937 764}