weekly release 2.7dev
[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.
fb99ef1d 80 return true;
d0a9a29b
DM
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) {
fb99ef1d 87 return true;
39ec8285 88 } catch (StaleElementReference $e) {
fb99ef1d 89 return true;
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 *
c1faf86b
DM
351 * As a "not" method, it's performance is not specially good as we should ensure that the element
352 * have time to appear.
353 *
63950e4d
DM
354 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
355 * @throws ElementNotFoundException
356 * @throws ExpectationException
357 * @param string $element
358 * @param string $selectortype
359 * @return void
360 */
361 public function should_not_be_visible($element, $selectortype) {
362
363 try {
364 $this->should_be_visible($element, $selectortype);
365 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
366 } catch (ExpectationException $e) {
367 // All as expected.
368 }
369 }
370
371 /**
372 * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
373 *
374 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
375 * @throws ElementNotFoundException
376 * @throws DriverException
377 * @throws ExpectationException
378 * @param string $element Element we look for
379 * @param string $selectortype The type of what we look for
380 * @param string $nodeelement Element we look in
381 * @param string $nodeselectortype The type of selector where we look in
382 */
383 public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
384
385 if (!$this->running_javascript()) {
386 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
387 }
388
389 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
390 if (!$node->isVisible()) {
391 throw new ExpectationException(
392 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
393 $this->getSession()
394 );
395 }
396 }
397
398 /**
399 * Checks, that the specified element is not visible inside the specified container. Only available in tests using Javascript.
400 *
401 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
402 * @throws ElementNotFoundException
403 * @throws ExpectationException
404 * @param string $element Element we look for
405 * @param string $selectortype The type of what we look for
406 * @param string $nodeelement Element we look in
407 * @param string $nodeselectortype The type of selector where we look in
408 */
409 public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
410
411 try {
412 $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
413 throw new ExpectationException(
414 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
415 $this->getSession()
416 );
417 } catch (ExpectationException $e) {
418 // All as expected.
419 }
420 }
421
786ea937 422 /**
e9af3ed3 423 * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
786ea937 424 *
786ea937 425 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 426 * @throws ExpectationException
40923977 427 * @param string $text
786ea937
DM
428 */
429 public function assert_page_contains_text($text) {
9a1f4922 430
e9af3ed3
DM
431 // Looking for all the matching nodes without any other descendant matching the
432 // same xpath (we are using contains(., ....).
9a1f4922 433 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
e9af3ed3
DM
434 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
435 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
9a1f4922 436
9a1f4922 437 try {
e9af3ed3 438 $nodes = $this->find_all('xpath', $xpath);
c1faf86b
DM
439 } catch (ElementNotFoundException $e) {
440 throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
441 }
e9af3ed3 442
c1faf86b
DM
443 // If we are not running javascript we have enough with the
444 // element existing as we can't check if it is visible.
445 if (!$this->running_javascript()) {
446 return;
447 }
448
449 // We spin as we don't have enough checking that the element is there, we
450 // should also ensure that the element is visible.
451 $this->spin(
452 function($context, $args) {
453
454 foreach ($args['nodes'] as $node) {
e9af3ed3 455 if ($node->isVisible()) {
c1faf86b 456 return true;
e9af3ed3
DM
457 }
458 }
459
c1faf86b
DM
460 // If non of the nodes is visible we loop again.
461 throw new ExpectationException('"' . $args['text'] . '" text was found but was not visible', $context->getSession());
462 },
463 array('nodes' => $nodes, 'text' => $text)
464 );
e9af3ed3 465
786ea937
DM
466 }
467
468 /**
e9af3ed3 469 * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 470 *
786ea937 471 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 472 * @throws ExpectationException
40923977 473 * @param string $text
786ea937
DM
474 */
475 public function assert_page_not_contains_text($text) {
9a1f4922 476
c1faf86b
DM
477 // Looking for all the matching nodes without any other descendant matching the
478 // same xpath (we are using contains(., ....).
479 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
480 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
481 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
482
483 // We should wait a while to ensure that the page is not still loading elements.
484 // Giving preference to the reliability of the results rather than to the performance.
9a1f4922 485 try {
c1faf86b
DM
486 $nodes = $this->find_all('xpath', $xpath);
487 } catch (ElementNotFoundException $e) {
488 // All ok.
5458ab3e 489 return;
9a1f4922 490 }
5458ab3e 491
c1faf86b
DM
492 // If we are not running javascript we have enough with the
493 // element existing as we can't check if it is hidden.
494 if (!$this->running_javascript()) {
495 throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
496 }
497
498 // If the element is there we should be sure that it is not visible.
499 $this->spin(
500 function($context, $args) {
501
502 foreach ($args['nodes'] as $node) {
503 if ($node->isVisible()) {
504 throw new ExpectationException('"' . $args['text'] . '" text was found in the page', $context->getSession());
505 }
506 }
507
508 // If non of the found nodes is visible we consider that the text is not visible.
509 return true;
510 },
511 array('nodes' => $nodes, 'text' => $text)
512 );
513
786ea937
DM
514 }
515
516 /**
e9af3ed3 517 * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 518 *
40923977 519 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
520 * @throws ElementNotFoundException
521 * @throws ExpectationException
40923977
DM
522 * @param string $text
523 * @param string $element Element we look in.
524 * @param string $selectortype The type of element where we are looking in.
786ea937 525 */
40923977
DM
526 public function assert_element_contains_text($text, $element, $selectortype) {
527
5458ab3e
DM
528 // Getting the container where the text should be found.
529 $container = $this->get_selected_node($selectortype, $element);
530
e9af3ed3
DM
531 // Looking for all the matching nodes without any other descendant matching the
532 // same xpath (we are using contains(., ....).
5458ab3e 533 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
e9af3ed3
DM
534 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
535 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
5458ab3e
DM
536
537 // Wait until it finds the text inside the container, otherwise custom exception.
538 try {
e9af3ed3 539 $nodes = $this->find_all('xpath', $xpath, false, $container);
c1faf86b
DM
540 } catch (ElementNotFoundException $e) {
541 throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
542 }
e9af3ed3 543
c1faf86b
DM
544 // If we are not running javascript we have enough with the
545 // element existing as we can't check if it is visible.
546 if (!$this->running_javascript()) {
547 return;
548 }
549
550 // We also check the element visibility when running JS tests.
551 $this->spin(
552 function($context, $args) {
553
554 foreach ($args['nodes'] as $node) {
e9af3ed3 555 if ($node->isVisible()) {
c1faf86b 556 return true;
e9af3ed3
DM
557 }
558 }
559
c1faf86b
DM
560 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element but was not visible', $context->getSession());
561 },
562 array('nodes' => $nodes, 'text' => $text, 'element' => $element)
563 );
786ea937
DM
564 }
565
566 /**
e9af3ed3 567 * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 568 *
40923977 569 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
570 * @throws ElementNotFoundException
571 * @throws ExpectationException
40923977
DM
572 * @param string $text
573 * @param string $element Element we look in.
574 * @param string $selectortype The type of element where we are looking in.
786ea937 575 */
40923977
DM
576 public function assert_element_not_contains_text($text, $element, $selectortype) {
577
c1faf86b
DM
578 // Getting the container where the text should be found.
579 $container = $this->get_selected_node($selectortype, $element);
580
581 // Looking for all the matching nodes without any other descendant matching the
582 // same xpath (we are using contains(., ....).
583 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
584 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
585 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
586
587 // We should wait a while to ensure that the page is not still loading elements.
588 // Giving preference to the reliability of the results rather than to the performance.
5458ab3e 589 try {
c1faf86b
DM
590 $nodes = $this->find_all('xpath', $xpath, false, $container);
591 } catch (ElementNotFoundException $e) {
592 // All ok.
5458ab3e
DM
593 return;
594 }
595
c1faf86b
DM
596 // If we are not running javascript we have enough with the
597 // element not being found as we can't check if it is visible.
598 if (!$this->running_javascript()) {
599 throw new ExpectationException('"' . $text . '" text was found in the "' . $element . '" element', $this->getSession());
600 }
601
602 // We need to ensure all the found nodes are hidden.
603 $this->spin(
604 function($context, $args) {
605
606 foreach ($args['nodes'] as $node) {
607 if ($node->isVisible()) {
608 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element', $context->getSession());
609 }
610 }
611
612 // If all the found nodes are hidden we are happy.
613 return true;
614 },
615 array('nodes' => $nodes, 'text' => $text, 'element' => $element)
616 );
786ea937
DM
617 }
618
60054942
DM
619 /**
620 * Checks, that the first specified element appears before the second one.
621 *
622 * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
623 * @throws ExpectationException
624 * @param string $preelement The locator of the preceding element
625 * @param string $preselectortype The locator of the preceding element
626 * @param string $postelement The locator of the latest element
627 * @param string $postselectortype The selector type of the latest element
628 */
629 public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
630
631 // We allow postselectortype as a non-text based selector.
632 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
633 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
634
635 $prexpath = $this->find($preselector, $prelocator)->getXpath();
636 $postxpath = $this->find($postselector, $postlocator)->getXpath();
637
638 // Using following xpath axe to find it.
639 $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
640 $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
641 if (!$this->getSession()->getDriver()->find($xpath)) {
642 throw new ExpectationException($msg, $this->getSession());
643 }
644 }
645
646 /**
647 * Checks, that the first specified element appears after the second one.
648 *
649 * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
650 * @throws ExpectationException
651 * @param string $postelement The locator of the latest element
652 * @param string $postselectortype The selector type of the latest element
653 * @param string $preelement The locator of the preceding element
654 * @param string $preselectortype The locator of the preceding element
655 */
656 public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
657
658 // We allow postselectortype as a non-text based selector.
659 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
660 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
661
662 $postxpath = $this->find($postselector, $postlocator)->getXpath();
663 $prexpath = $this->find($preselector, $prelocator)->getXpath();
664
665 // Using preceding xpath axe to find it.
666 $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
667 $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
668 if (!$this->getSession()->getDriver()->find($xpath)) {
669 throw new ExpectationException($msg, $this->getSession());
670 }
671 }
672
786ea937 673 /**
40923977 674 * Checks, that element of specified type is disabled.
786ea937 675 *
40923977 676 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 677 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
678 * @param string $element Element we look in
679 * @param string $selectortype The type of element where we are looking in.
786ea937 680 */
40923977 681 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 682
40923977
DM
683 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
684 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
685
686 if (!$node->hasAttribute('disabled')) {
687 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
688 }
689 }
690
691 /**
40923977 692 * Checks, that element of specified type is enabled.
786ea937 693 *
40923977 694 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 695 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
696 * @param string $element Element we look on
697 * @param string $selectortype The type of where we look
786ea937 698 */
40923977 699 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 700
40923977
DM
701 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
702 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
703
704 if ($node->hasAttribute('disabled')) {
705 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
706 }
707 }
708
a2d3e3b6
MN
709 /**
710 * Checks the provided element and selector type are readonly on the current page.
711 *
712 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
713 * @throws ExpectationException Thrown by behat_base::find
714 * @param string $element Element we look in
715 * @param string $selectortype The type of element where we are looking in.
716 */
717 public function the_element_should_be_readonly($element, $selectortype) {
718 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
719 $node = $this->get_selected_node($selectortype, $element);
720
721 if (!$node->hasAttribute('readonly')) {
722 throw new ExpectationException('The element "' . $element . '" is not readonly', $this->getSession());
723 }
724 }
725
726 /**
727 * Checks the provided element and selector type are not readonly on the current page.
728 *
729 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
730 * @throws ExpectationException Thrown by behat_base::find
731 * @param string $element Element we look in
732 * @param string $selectortype The type of element where we are looking in.
733 */
734 public function the_element_should_not_be_readonly($element, $selectortype) {
735 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
736 $node = $this->get_selected_node($selectortype, $element);
737
738 if ($node->hasAttribute('readonly')) {
739 throw new ExpectationException('The element "' . $element . '" is readonly', $this->getSession());
740 }
741 }
742
ca4f33a7 743 /**
62eb5c46
EL
744 * Checks the provided element and selector type exists in the current page.
745 *
746 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
747 *
748 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
749 * @throws ElementNotFoundException Thrown by behat_base::find
750 * @param string $element The locator of the specified selector
751 * @param string $selectortype The selector type
752 */
753 public function should_exists($element, $selectortype) {
754
755 // Getting Mink selector and locator.
756 list($selector, $locator) = $this->transform_selector($selectortype, $element);
757
758 // Will throw an ElementNotFoundException if it does not exist.
759 $this->find($selector, $locator);
760 }
761
762 /**
62eb5c46
EL
763 * Checks that the provided element and selector type not exists in the current page.
764 *
765 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
766 *
767 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
768 * @throws ExpectationException
769 * @param string $element The locator of the specified selector
770 * @param string $selectortype The selector type
771 */
772 public function should_not_exists($element, $selectortype) {
773
774 try {
775 $this->should_exists($element, $selectortype);
776 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
62eb5c46 777 } catch (ElementNotFoundException $e) {
ca4f33a7
DM
778 // It passes.
779 return;
780 }
781 }
782
066ef320
JM
783 /**
784 * This step triggers cron like a user would do going to admin/cron.php.
785 *
786 * @Given /^I trigger cron$/
787 */
788 public function i_trigger_cron() {
789 $this->getSession()->visit($this->locate_path('/admin/cron.php'));
790 }
791
a2d3e3b6
MN
792 /**
793 * Checks that an element and selector type exists in another element and selector type on the current page.
794 *
795 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
796 *
797 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
798 * @throws ElementNotFoundException Thrown by behat_base::find
799 * @param string $element The locator of the specified selector
800 * @param string $selectortype The selector type
801 * @param string $containerelement The container selector type
802 * @param string $containerselectortype The container locator
803 */
804 public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
805 // Get the container node.
806 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
807
808 list($selector, $locator) = $this->transform_selector($selectortype, $element);
809
810 // Specific exception giving info about where can't we find the element.
811 $locatorexceptionmsg = $element . '" in the "' . $containerelement. '" "' . $containerselectortype. '"';
812 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg);
813
814 // Looks for the requested node inside the container node.
815 $this->find($selector, $locator, $exception, $containernode);
816 }
817
818 /**
819 * Checks that an element and selector type does not exist in another element and selector type on the current page.
820 *
821 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
822 *
823 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
824 * @throws ExpectationException
825 * @param string $element The locator of the specified selector
826 * @param string $selectortype The selector type
827 * @param string $containerelement The container selector type
828 * @param string $containerselectortype The container locator
829 */
830 public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
831 try {
832 $this->should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype);
833 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the "' .
834 $containerelement . '" "' . $containerselectortype . '"', $this->getSession());
835 } catch (ElementNotFoundException $e) {
836 // It passes.
837 return;
838 }
839 }
786ea937 840}