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