MDL-46817 behat: Table selector support css
[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
d7a0b721
RT
285 /**
286 * Sets the focus and takes away the focus from an element, generating blur JS event.
287 *
288 * @When /^I take focus off "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
289 * @param string $element Element we look for
290 * @param string $selectortype The type of what we look for
291 */
292 public function i_take_focus_off_field($element, $selectortype) {
293 if (!$this->running_javascript()) {
294 throw new ExpectationException('Can\'t take focus off from "' . $element . '" in non-js mode', $this->getSession());
295 }
296 // Gets the node based on the requested selector type and locator.
297 $node = $this->get_selected_node($selectortype, $element);
298 $this->ensure_node_is_visible($node);
299
300 // Ensure element is focused before taking it off.
301 $node->focus();
302 $node->blur();
303 }
304
b28b374f
DM
305 /**
306 * Clicks the specified element and confirms the expected dialogue.
307 *
308 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" confirming the dialogue$/
309 * @throws ElementNotFoundException Thrown by behat_base::find
310 * @param string $link
311 */
312 public function i_click_on_confirming_the_dialogue($element, $selectortype) {
313 $this->i_click_on($element, $selectortype);
314 $this->accept_currently_displayed_alert_dialog();
315 }
316
072f67fc
DM
317 /**
318 * Click on the element of the specified type which is located inside the second element.
319 *
320 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
321 * @param string $element Element we look for
322 * @param string $selectortype The type of what we look for
323 * @param string $nodeelement Element we look in
324 * @param string $nodeselectortype The type of selector where we look in
325 */
326 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
327
328 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
d1e55a47 329 $this->ensure_node_is_visible($node);
072f67fc
DM
330 $node->click();
331 }
332
563514b1 333 /**
7daab401 334 * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
563514b1
DM
335 *
336 * The steps definitions calling this step as part of them should
337 * manage the wait times by themselves as the times and when the
338 * waits should be done depends on what is being dragged & dropper.
339 *
340 * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
341 * @param string $element
342 * @param string $selectortype
343 * @param string $containerelement
344 * @param string $containerselectortype
345 */
346 public function i_drag_and_i_drop_it_in($element, $selectortype, $containerelement, $containerselectortype) {
347
348 list($sourceselector, $sourcelocator) = $this->transform_selector($selectortype, $element);
349 $sourcexpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($sourceselector, $sourcelocator);
350
351 list($containerselector, $containerlocator) = $this->transform_selector($containerselectortype, $containerelement);
352 $destinationxpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($containerselector, $containerlocator);
353
354 $this->getSession()->getDriver()->dragTo($sourcexpath, $destinationxpath);
355 }
356
63950e4d
DM
357 /**
358 * Checks, that the specified element is visible. Only available in tests using Javascript.
359 *
360 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
361 * @throws ElementNotFoundException
362 * @throws ExpectationException
363 * @throws DriverException
364 * @param string $element
365 * @param string $selectortype
366 * @return void
367 */
368 public function should_be_visible($element, $selectortype) {
369
370 if (!$this->running_javascript()) {
371 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
372 }
373
374 $node = $this->get_selected_node($selectortype, $element);
375 if (!$node->isVisible()) {
376 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
377 }
378 }
379
380 /**
74c78e74 381 * Checks, that the existing element is not visible. Only available in tests using Javascript.
63950e4d 382 *
74c78e74
DM
383 * As a "not" method, it's performance could not be good, but in this
384 * case the performance is good because the element must exist,
385 * otherwise there would be a ElementNotFoundException, also here we are
386 * not spinning until the element is visible.
c1faf86b 387 *
63950e4d
DM
388 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
389 * @throws ElementNotFoundException
390 * @throws ExpectationException
391 * @param string $element
392 * @param string $selectortype
393 * @return void
394 */
395 public function should_not_be_visible($element, $selectortype) {
396
397 try {
398 $this->should_be_visible($element, $selectortype);
399 throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
400 } catch (ExpectationException $e) {
401 // All as expected.
402 }
403 }
404
405 /**
406 * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
407 *
408 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
409 * @throws ElementNotFoundException
410 * @throws DriverException
411 * @throws ExpectationException
412 * @param string $element Element we look for
413 * @param string $selectortype The type of what we look for
414 * @param string $nodeelement Element we look in
415 * @param string $nodeselectortype The type of selector where we look in
416 */
417 public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
418
419 if (!$this->running_javascript()) {
420 throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
421 }
422
423 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
424 if (!$node->isVisible()) {
425 throw new ExpectationException(
426 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
427 $this->getSession()
428 );
429 }
430 }
431
432 /**
74c78e74
DM
433 * Checks, that the existing element is not visible inside the existing container. Only available in tests using Javascript.
434 *
435 * As a "not" method, it's performance could not be good, but in this
436 * case the performance is good because the element must exist,
437 * otherwise there would be a ElementNotFoundException, also here we are
438 * not spinning until the element is visible.
63950e4d
DM
439 *
440 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
441 * @throws ElementNotFoundException
442 * @throws ExpectationException
443 * @param string $element Element we look for
444 * @param string $selectortype The type of what we look for
445 * @param string $nodeelement Element we look in
446 * @param string $nodeselectortype The type of selector where we look in
447 */
448 public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
449
450 try {
451 $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
452 throw new ExpectationException(
453 '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
454 $this->getSession()
455 );
456 } catch (ExpectationException $e) {
457 // All as expected.
458 }
459 }
460
786ea937 461 /**
e9af3ed3 462 * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
786ea937 463 *
786ea937 464 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 465 * @throws ExpectationException
40923977 466 * @param string $text
786ea937
DM
467 */
468 public function assert_page_contains_text($text) {
9a1f4922 469
e9af3ed3
DM
470 // Looking for all the matching nodes without any other descendant matching the
471 // same xpath (we are using contains(., ....).
9a1f4922 472 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
e9af3ed3
DM
473 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
474 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
9a1f4922 475
9a1f4922 476 try {
e9af3ed3 477 $nodes = $this->find_all('xpath', $xpath);
c1faf86b
DM
478 } catch (ElementNotFoundException $e) {
479 throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
480 }
e9af3ed3 481
c1faf86b
DM
482 // If we are not running javascript we have enough with the
483 // element existing as we can't check if it is visible.
484 if (!$this->running_javascript()) {
485 return;
486 }
487
488 // We spin as we don't have enough checking that the element is there, we
74c78e74
DM
489 // should also ensure that the element is visible. Using microsleep as this
490 // is a repeated step and global performance is important.
c1faf86b
DM
491 $this->spin(
492 function($context, $args) {
493
494 foreach ($args['nodes'] as $node) {
e9af3ed3 495 if ($node->isVisible()) {
c1faf86b 496 return true;
e9af3ed3
DM
497 }
498 }
499
c1faf86b
DM
500 // If non of the nodes is visible we loop again.
501 throw new ExpectationException('"' . $args['text'] . '" text was found but was not visible', $context->getSession());
502 },
74c78e74
DM
503 array('nodes' => $nodes, 'text' => $text),
504 false,
505 false,
506 true
c1faf86b 507 );
e9af3ed3 508
786ea937
DM
509 }
510
511 /**
e9af3ed3 512 * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 513 *
786ea937 514 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
9a1f4922 515 * @throws ExpectationException
40923977 516 * @param string $text
786ea937
DM
517 */
518 public function assert_page_not_contains_text($text) {
9a1f4922 519
c1faf86b
DM
520 // Looking for all the matching nodes without any other descendant matching the
521 // same xpath (we are using contains(., ....).
522 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
523 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
524 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
525
526 // We should wait a while to ensure that the page is not still loading elements.
74c78e74
DM
527 // Waiting less than self::TIMEOUT as we already waited for the DOM to be ready and
528 // all JS to be executed.
9a1f4922 529 try {
74c78e74 530 $nodes = $this->find_all('xpath', $xpath, false, false, self::REDUCED_TIMEOUT);
c1faf86b
DM
531 } catch (ElementNotFoundException $e) {
532 // All ok.
5458ab3e 533 return;
9a1f4922 534 }
5458ab3e 535
c1faf86b
DM
536 // If we are not running javascript we have enough with the
537 // element existing as we can't check if it is hidden.
538 if (!$this->running_javascript()) {
539 throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
540 }
541
542 // If the element is there we should be sure that it is not visible.
543 $this->spin(
544 function($context, $args) {
545
546 foreach ($args['nodes'] as $node) {
547 if ($node->isVisible()) {
548 throw new ExpectationException('"' . $args['text'] . '" text was found in the page', $context->getSession());
549 }
550 }
551
552 // If non of the found nodes is visible we consider that the text is not visible.
553 return true;
554 },
74c78e74
DM
555 array('nodes' => $nodes, 'text' => $text),
556 self::REDUCED_TIMEOUT,
557 false,
558 true
c1faf86b
DM
559 );
560
786ea937
DM
561 }
562
563 /**
e9af3ed3 564 * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 565 *
40923977 566 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
567 * @throws ElementNotFoundException
568 * @throws ExpectationException
40923977
DM
569 * @param string $text
570 * @param string $element Element we look in.
571 * @param string $selectortype The type of element where we are looking in.
786ea937 572 */
40923977
DM
573 public function assert_element_contains_text($text, $element, $selectortype) {
574
5458ab3e
DM
575 // Getting the container where the text should be found.
576 $container = $this->get_selected_node($selectortype, $element);
577
e9af3ed3
DM
578 // Looking for all the matching nodes without any other descendant matching the
579 // same xpath (we are using contains(., ....).
5458ab3e 580 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
e9af3ed3
DM
581 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
582 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
5458ab3e
DM
583
584 // Wait until it finds the text inside the container, otherwise custom exception.
585 try {
e9af3ed3 586 $nodes = $this->find_all('xpath', $xpath, false, $container);
c1faf86b
DM
587 } catch (ElementNotFoundException $e) {
588 throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
589 }
e9af3ed3 590
c1faf86b
DM
591 // If we are not running javascript we have enough with the
592 // element existing as we can't check if it is visible.
593 if (!$this->running_javascript()) {
594 return;
595 }
596
74c78e74
DM
597 // We also check the element visibility when running JS tests. Using microsleep as this
598 // is a repeated step and global performance is important.
c1faf86b
DM
599 $this->spin(
600 function($context, $args) {
601
602 foreach ($args['nodes'] as $node) {
e9af3ed3 603 if ($node->isVisible()) {
c1faf86b 604 return true;
e9af3ed3
DM
605 }
606 }
607
c1faf86b
DM
608 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element but was not visible', $context->getSession());
609 },
74c78e74
DM
610 array('nodes' => $nodes, 'text' => $text, 'element' => $element),
611 false,
612 false,
613 true
c1faf86b 614 );
786ea937
DM
615 }
616
617 /**
e9af3ed3 618 * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
786ea937 619 *
40923977 620 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
5458ab3e
DM
621 * @throws ElementNotFoundException
622 * @throws ExpectationException
40923977
DM
623 * @param string $text
624 * @param string $element Element we look in.
625 * @param string $selectortype The type of element where we are looking in.
786ea937 626 */
40923977
DM
627 public function assert_element_not_contains_text($text, $element, $selectortype) {
628
c1faf86b
DM
629 // Getting the container where the text should be found.
630 $container = $this->get_selected_node($selectortype, $element);
631
632 // Looking for all the matching nodes without any other descendant matching the
633 // same xpath (we are using contains(., ....).
634 $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
635 $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
636 "[count(descendant::*[contains(., $xpathliteral)]) = 0]";
637
638 // We should wait a while to ensure that the page is not still loading elements.
639 // Giving preference to the reliability of the results rather than to the performance.
5458ab3e 640 try {
74c78e74 641 $nodes = $this->find_all('xpath', $xpath, false, $container, self::REDUCED_TIMEOUT);
c1faf86b
DM
642 } catch (ElementNotFoundException $e) {
643 // All ok.
5458ab3e
DM
644 return;
645 }
646
c1faf86b
DM
647 // If we are not running javascript we have enough with the
648 // element not being found as we can't check if it is visible.
649 if (!$this->running_javascript()) {
650 throw new ExpectationException('"' . $text . '" text was found in the "' . $element . '" element', $this->getSession());
651 }
652
653 // We need to ensure all the found nodes are hidden.
654 $this->spin(
655 function($context, $args) {
656
657 foreach ($args['nodes'] as $node) {
658 if ($node->isVisible()) {
659 throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element', $context->getSession());
660 }
661 }
662
663 // If all the found nodes are hidden we are happy.
664 return true;
665 },
74c78e74
DM
666 array('nodes' => $nodes, 'text' => $text, 'element' => $element),
667 self::REDUCED_TIMEOUT,
668 false,
669 true
c1faf86b 670 );
786ea937
DM
671 }
672
60054942
DM
673 /**
674 * Checks, that the first specified element appears before the second one.
675 *
676 * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
677 * @throws ExpectationException
678 * @param string $preelement The locator of the preceding element
679 * @param string $preselectortype The locator of the preceding element
680 * @param string $postelement The locator of the latest element
681 * @param string $postselectortype The selector type of the latest element
682 */
683 public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
684
685 // We allow postselectortype as a non-text based selector.
686 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
687 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
688
689 $prexpath = $this->find($preselector, $prelocator)->getXpath();
690 $postxpath = $this->find($postselector, $postlocator)->getXpath();
691
692 // Using following xpath axe to find it.
693 $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
694 $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
695 if (!$this->getSession()->getDriver()->find($xpath)) {
696 throw new ExpectationException($msg, $this->getSession());
697 }
698 }
699
700 /**
701 * Checks, that the first specified element appears after the second one.
702 *
703 * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
704 * @throws ExpectationException
705 * @param string $postelement The locator of the latest element
706 * @param string $postselectortype The selector type of the latest element
707 * @param string $preelement The locator of the preceding element
708 * @param string $preselectortype The locator of the preceding element
709 */
710 public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
711
712 // We allow postselectortype as a non-text based selector.
713 list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
714 list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
715
716 $postxpath = $this->find($postselector, $postlocator)->getXpath();
717 $prexpath = $this->find($preselector, $prelocator)->getXpath();
718
719 // Using preceding xpath axe to find it.
720 $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
721 $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
722 if (!$this->getSession()->getDriver()->find($xpath)) {
723 throw new ExpectationException($msg, $this->getSession());
724 }
725 }
726
786ea937 727 /**
40923977 728 * Checks, that element of specified type is disabled.
786ea937 729 *
40923977 730 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 731 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
732 * @param string $element Element we look in
733 * @param string $selectortype The type of element where we are looking in.
786ea937 734 */
40923977 735 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 736
40923977
DM
737 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
738 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
739
740 if (!$node->hasAttribute('disabled')) {
741 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
742 }
743 }
744
745 /**
40923977 746 * Checks, that element of specified type is enabled.
786ea937 747 *
40923977 748 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 749 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
750 * @param string $element Element we look on
751 * @param string $selectortype The type of where we look
786ea937 752 */
40923977 753 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 754
40923977
DM
755 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
756 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
757
758 if ($node->hasAttribute('disabled')) {
759 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
760 }
761 }
762
a2d3e3b6
MN
763 /**
764 * Checks the provided element and selector type are readonly on the current page.
765 *
766 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
767 * @throws ExpectationException Thrown by behat_base::find
768 * @param string $element Element we look in
769 * @param string $selectortype The type of element where we are looking in.
770 */
771 public function the_element_should_be_readonly($element, $selectortype) {
772 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
773 $node = $this->get_selected_node($selectortype, $element);
774
775 if (!$node->hasAttribute('readonly')) {
776 throw new ExpectationException('The element "' . $element . '" is not readonly', $this->getSession());
777 }
778 }
779
780 /**
781 * Checks the provided element and selector type are not readonly on the current page.
782 *
783 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
784 * @throws ExpectationException Thrown by behat_base::find
785 * @param string $element Element we look in
786 * @param string $selectortype The type of element where we are looking in.
787 */
788 public function the_element_should_not_be_readonly($element, $selectortype) {
789 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
790 $node = $this->get_selected_node($selectortype, $element);
791
792 if ($node->hasAttribute('readonly')) {
793 throw new ExpectationException('The element "' . $element . '" is readonly', $this->getSession());
794 }
795 }
796
ca4f33a7 797 /**
62eb5c46
EL
798 * Checks the provided element and selector type exists in the current page.
799 *
800 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7 801 *
c51c3b55 802 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist$/
ca4f33a7
DM
803 * @throws ElementNotFoundException Thrown by behat_base::find
804 * @param string $element The locator of the specified selector
805 * @param string $selectortype The selector type
806 */
c51c3b55 807 public function should_exist($element, $selectortype) {
ca4f33a7
DM
808
809 // Getting Mink selector and locator.
810 list($selector, $locator) = $this->transform_selector($selectortype, $element);
811
812 // Will throw an ElementNotFoundException if it does not exist.
813 $this->find($selector, $locator);
814 }
815
816 /**
62eb5c46
EL
817 * Checks that the provided element and selector type not exists in the current page.
818 *
819 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7 820 *
c51c3b55 821 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist$/
ca4f33a7
DM
822 * @throws ExpectationException
823 * @param string $element The locator of the specified selector
824 * @param string $selectortype The selector type
825 */
c51c3b55 826 public function should_not_exist($element, $selectortype) {
ca4f33a7 827
74c78e74
DM
828 // Getting Mink selector and locator.
829 list($selector, $locator) = $this->transform_selector($selectortype, $element);
830
ca4f33a7 831 try {
74c78e74
DM
832
833 // Using directly the spin method as we want a reduced timeout but there is no
834 // need for a 0.1 seconds interval because in the optimistic case we will timeout.
835 $params = array('selector' => $selector, 'locator' => $locator);
836 // The exception does not really matter as we will catch it and will never "explode".
837 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $element);
838
839 // If all goes good it will throw an ElementNotFoundExceptionn that we will catch.
840 $this->spin(
841 function($context, $args) {
842 return $context->getSession()->getPage()->findAll($args['selector'], $args['locator']);
843 },
844 $params,
20503d15 845 self::REDUCED_TIMEOUT,
74c78e74 846 $exception,
20503d15 847 false
74c78e74
DM
848 );
849
ca4f33a7 850 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
62eb5c46 851 } catch (ElementNotFoundException $e) {
ca4f33a7
DM
852 // It passes.
853 return;
854 }
855 }
856
066ef320
JM
857 /**
858 * This step triggers cron like a user would do going to admin/cron.php.
859 *
860 * @Given /^I trigger cron$/
861 */
862 public function i_trigger_cron() {
863 $this->getSession()->visit($this->locate_path('/admin/cron.php'));
864 }
865
a2d3e3b6
MN
866 /**
867 * Checks that an element and selector type exists in another element and selector type on the current page.
868 *
869 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
870 *
871 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
872 * @throws ElementNotFoundException Thrown by behat_base::find
873 * @param string $element The locator of the specified selector
874 * @param string $selectortype The selector type
875 * @param string $containerelement The container selector type
876 * @param string $containerselectortype The container locator
877 */
878 public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
879 // Get the container node.
880 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
881
882 list($selector, $locator) = $this->transform_selector($selectortype, $element);
883
884 // Specific exception giving info about where can't we find the element.
885 $locatorexceptionmsg = $element . '" in the "' . $containerelement. '" "' . $containerselectortype. '"';
886 $exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $locatorexceptionmsg);
887
888 // Looks for the requested node inside the container node.
889 $this->find($selector, $locator, $exception, $containernode);
890 }
891
892 /**
893 * Checks that an element and selector type does not exist in another element and selector type on the current page.
894 *
895 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
896 *
897 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
898 * @throws ExpectationException
899 * @param string $element The locator of the specified selector
900 * @param string $selectortype The selector type
901 * @param string $containerelement The container selector type
902 * @param string $containerselectortype The container locator
903 */
904 public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
74c78e74
DM
905
906 // Get the container node; here we throw an exception
907 // if the container node does not exist.
908 $containernode = $this->get_selected_node($containerselectortype, $containerelement);
909
910 list($selector, $locator) = $this->transform_selector($selectortype, $element);
911
912 // Will throw an ElementNotFoundException if it does not exist, but, actually
759b323e 913 // it should not exist, so we try & catch it.
a2d3e3b6 914 try {
74c78e74
DM
915 // Would be better to use a 1 second sleep because the element should not be there,
916 // but we would need to duplicate the whole find_all() logic to do it, the benefit of
917 // changing to 1 second sleep is not significant.
918 $this->find($selector, $locator, false, $containernode, self::REDUCED_TIMEOUT);
a2d3e3b6
MN
919 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the "' .
920 $containerelement . '" "' . $containerselectortype . '"', $this->getSession());
921 } catch (ElementNotFoundException $e) {
922 // It passes.
923 return;
924 }
925 }
3b0b5e57
RT
926
927 /**
928 * Change browser window size small: 640x480, medium: 1024x768, large: 2560x1600, custom: widthxheight
929 *
930 * Example: I change window size to "small" or I change window size to "1024x768"
931 *
932 * @throws ExpectationException
933 * @Then /^I change window size to "([^"](small|medium|large|\d+x\d+))"$/
934 * @param string $windowsize size of the window (small|medium|large|wxh).
935 */
936 public function i_change_window_size_to($windowsize) {
937 $this->resize_window($windowsize);
938 }
c0fb7f44 939
940 /**
941 * Checks whether there is an attribute on the given element that contains the specified text.
942 *
943 * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should contain "(?P<text_string>(?:[^"]|\\")*)"$/
944 * @throws ExpectationException
945 * @param string $attribute Name of attribute
946 * @param string $element The locator of the specified selector
947 * @param string $selectortype The selector type
948 * @param string $text Expected substring
949 */
950 public function the_attribute_of_should_contain($attribute, $element, $selectortype, $text) {
951 // Get the container node (exception if it doesn't exist).
952 $containernode = $this->get_selected_node($selectortype, $element);
953 $value = $containernode->getAttribute($attribute);
954 if ($value == null) {
955 throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
956 $this->getSession());
957 } else if (strpos($value, $text) === false) {
958 throw new ExpectationException('The attribute "' . $attribute .
959 '" does not contain "' . $text . '" (actual value: "' . $value . '")',
960 $this->getSession());
961 }
962 }
90c3ffe3 963
964 /**
965 * Checks that the attribute on the given element does not contain the specified text.
966 *
967 * @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not contain "(?P<text_string>(?:[^"]|\\")*)"$/
968 * @throws ExpectationException
969 * @param string $attribute Name of attribute
970 * @param string $element The locator of the specified selector
971 * @param string $selectortype The selector type
972 * @param string $text Expected substring
973 */
974 public function the_attribute_of_should_not_contain($attribute, $element, $selectortype, $text) {
975 // Get the container node (exception if it doesn't exist).
976 $containernode = $this->get_selected_node($selectortype, $element);
977 $value = $containernode->getAttribute($attribute);
978 if ($value == null) {
979 throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
980 $this->getSession());
981 } else if (strpos($value, $text) !== false) {
982 throw new ExpectationException('The attribute "' . $attribute .
983 '" contains "' . $text . '" (value: "' . $value . '")',
984 $this->getSession());
985 }
986 }
786ea937 987}