Commit | Line | Data |
---|---|---|
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 | ||
28 | require_once(__DIR__ . '/../../behat/behat_base.php'); | |
29 | ||
ca4f33a7 | 30 | use 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 | */ | |
49 | class behat_general extends behat_base { | |
50 | ||
51 | /** | |
52 | * Opens Moodle homepage. | |
53 | * | |
786ea937 DM |
54 | * @Given /^I am on homepage$/ |
55 | */ | |
56 | public function i_am_on_homepage() { | |
40923977 | 57 | $this->getSession()->visit($this->locate_path('/')); |
786ea937 DM |
58 | } |
59 | ||
18c84063 DM |
60 | /** |
61 | * Reloads the current page. | |
62 | * | |
63 | * @Given /^I reload the page$/ | |
64 | */ | |
65 | public function reload() { | |
66 | $this->getSession()->reload(); | |
67 | } | |
68 | ||
d0a9a29b DM |
69 | /** |
70 | * Follows the page redirection. Use this step after any action that shows a message and waits for a redirection | |
71 | * | |
72 | * @Given /^I wait to be redirected$/ | |
73 | */ | |
74 | public function i_wait_to_be_redirected() { | |
75 | ||
76 | // Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and | |
77 | // moodle_page::$periodicrefreshdelay possible values. | |
78 | if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) { | |
79 | // We don't fail the scenario if no redirection with message is found to avoid race condition false failures. | |
80 | return false; | |
81 | } | |
82 | ||
bda1dea4 DM |
83 | // Wrapped in try & catch in case the redirection has already been executed. |
84 | try { | |
85 | $content = $metarefresh->getAttribute('content'); | |
86 | } catch (NoSuchElement $e) { | |
87 | return false; | |
39ec8285 DM |
88 | } catch (StaleElementReference $e) { |
89 | return false; | |
bda1dea4 DM |
90 | } |
91 | ||
92 | // Getting the refresh time and the url if present. | |
d0a9a29b DM |
93 | if (strstr($content, 'url') != false) { |
94 | ||
bda1dea4 | 95 | list($waittime, $url) = explode(';', $content); |
d0a9a29b DM |
96 | |
97 | // Cleaning the URL value. | |
98 | $url = trim(substr($url, strpos($url, 'http'))); | |
99 | ||
100 | } else { | |
101 | // Just wait then. | |
102 | $waittime = $content; | |
103 | } | |
104 | ||
105 | ||
106 | // Wait until the URL change is executed. | |
107 | if ($this->running_javascript()) { | |
108 | $this->getSession()->wait($waittime * 1000, false); | |
109 | ||
110 | } else if (!empty($url)) { | |
111 | // We redirect directly as we can not wait for an automatic redirection. | |
112 | $this->getSession()->getDriver()->getClient()->request('get', $url); | |
113 | ||
114 | } else { | |
115 | // Reload the page if no URL was provided. | |
116 | $this->getSession()->getDriver()->reload(); | |
117 | } | |
118 | } | |
119 | ||
e5eff0b6 AA |
120 | /** |
121 | * Switches to the specified iframe. | |
122 | * | |
123 | * @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" iframe$/ | |
124 | * @param string $iframename | |
125 | */ | |
126 | public function switch_to_iframe($iframename) { | |
127 | $this->getSession()->switchToIFrame($iframename); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Switches to the main Moodle frame. | |
132 | * | |
133 | * @Given /^I switch to the main frame$/ | |
134 | */ | |
135 | public function switch_to_the_main_frame() { | |
136 | $this->getSession()->switchToIFrame(); | |
137 | } | |
138 | ||
1303eb29 DM |
139 | /** |
140 | * Switches to the specified window. Useful when interacting with popup windows. | |
141 | * | |
142 | * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/ | |
143 | * @param string $windowname | |
144 | */ | |
145 | public function switch_to_window($windowname) { | |
146 | $this->getSession()->switchToWindow($windowname); | |
147 | } | |
148 | ||
149 | /** | |
150 | * Switches to the main Moodle window. Useful when you finish interacting with popup windows. | |
151 | * | |
152 | * @Given /^I switch to the main window$/ | |
153 | */ | |
154 | public function switch_to_the_main_window() { | |
155 | $this->getSession()->switchToWindow(); | |
156 | } | |
157 | ||
563514b1 | 158 | /** |
7daab401 | 159 | * Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental. |
563514b1 DM |
160 | * @Given /^I accept the currently displayed dialog$/ |
161 | */ | |
162 | public function accept_currently_displayed_alert_dialog() { | |
163 | $this->getSession()->getDriver()->getWebDriverSession()->accept_alert(); | |
164 | } | |
165 | ||
786ea937 DM |
166 | /** |
167 | * Clicks link with specified id|title|alt|text. | |
168 | * | |
786ea937 | 169 | * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/ |
1f9ffbdb | 170 | * @throws ElementNotFoundException Thrown by behat_base::find |
40923977 | 171 | * @param string $link |
786ea937 DM |
172 | */ |
173 | public function click_link($link) { | |
1f9ffbdb DM |
174 | |
175 | $linknode = $this->find_link($link); | |
176 | $linknode->click(); | |
786ea937 DM |
177 | } |
178 | ||
179 | /** | |
180 | * Waits X seconds. Required after an action that requires data from an AJAX request. | |
181 | * | |
182 | * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/ | |
183 | * @param int $seconds | |
184 | */ | |
185 | public function i_wait_seconds($seconds) { | |
d0a9a29b DM |
186 | |
187 | if (!$this->running_javascript()) { | |
188 | throw new DriverException('Waits are disabled in scenarios without Javascript support'); | |
189 | } | |
190 | ||
786ea937 DM |
191 | $this->getSession()->wait($seconds * 1000, false); |
192 | } | |
193 | ||
194 | /** | |
195 | * Waits until the page is completely loaded. This step is auto-executed after every step. | |
196 | * | |
197 | * @Given /^I wait until the page is ready$/ | |
198 | */ | |
199 | public function wait_until_the_page_is_ready() { | |
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(self::TIMEOUT, '(document.readyState === "complete")'); |
206 | } | |
207 | ||
208 | /** | |
40923977 | 209 | * Generic mouse over action. Mouse over a element of the specified type. |
786ea937 | 210 | * |
40923977 DM |
211 | * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/ |
212 | * @param string $element Element we look for | |
213 | * @param string $selectortype The type of what we look for | |
786ea937 | 214 | */ |
40923977 | 215 | public function i_hover($element, $selectortype) { |
1f9ffbdb | 216 | |
40923977 DM |
217 | // Gets the node based on the requested selector type and locator. |
218 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
219 | $node->mouseOver(); |
220 | } | |
221 | ||
40923977 DM |
222 | /** |
223 | * Generic click action. Click on the element of the specified type. | |
224 | * | |
225 | * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/ | |
226 | * @param string $element Element we look for | |
227 | * @param string $selectortype The type of what we look for | |
228 | */ | |
229 | public function i_click_on($element, $selectortype) { | |
230 | ||
231 | // Gets the node based on the requested selector type and locator. | |
232 | $node = $this->get_selected_node($selectortype, $element); | |
233 | $node->click(); | |
234 | } | |
235 | ||
072f67fc DM |
236 | /** |
237 | * Click on the element of the specified type which is located inside the second element. | |
238 | * | |
239 | * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ | |
240 | * @param string $element Element we look for | |
241 | * @param string $selectortype The type of what we look for | |
242 | * @param string $nodeelement Element we look in | |
243 | * @param string $nodeselectortype The type of selector where we look in | |
244 | */ | |
245 | public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) { | |
246 | ||
247 | $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement); | |
248 | $node->click(); | |
249 | } | |
250 | ||
f9d3667e DM |
251 | /** |
252 | * Click on the specified element inside a table row containing the specified text. | |
253 | * | |
254 | * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<row_text_string>(?:[^"]|\\")*)" table row$/ | |
255 | * @throws ElementNotFoundException | |
256 | * @param string $element Element we look for | |
257 | * @param string $selectortype The type of what we look for | |
258 | * @param string $tablerowtext The table row text | |
259 | */ | |
260 | public function i_click_on_in_the_table_row($element, $selectortype, $tablerowtext) { | |
261 | ||
262 | // The table row container. | |
263 | $nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text '); | |
38976081 DM |
264 | $tablerowtext = $this->getSession()->getSelectorsHandler()->xpathLiteral($tablerowtext); |
265 | $rownode = $this->find('xpath', "//tr[contains(., $tablerowtext)]", $nocontainerexception); | |
f9d3667e DM |
266 | |
267 | // Looking for the element DOM node inside the specified row. | |
268 | list($selector, $locator) = $this->transform_selector($selectortype, $element); | |
269 | $elementnode = $this->find($selector, $locator, false, $rownode); | |
270 | $elementnode->click(); | |
271 | } | |
272 | ||
563514b1 | 273 | /** |
7daab401 | 274 | * Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental. |
563514b1 DM |
275 | * |
276 | * The steps definitions calling this step as part of them should | |
277 | * manage the wait times by themselves as the times and when the | |
278 | * waits should be done depends on what is being dragged & dropper. | |
279 | * | |
280 | * @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
281 | * @param string $element | |
282 | * @param string $selectortype | |
283 | * @param string $containerelement | |
284 | * @param string $containerselectortype | |
285 | */ | |
286 | public function i_drag_and_i_drop_it_in($element, $selectortype, $containerelement, $containerselectortype) { | |
287 | ||
288 | list($sourceselector, $sourcelocator) = $this->transform_selector($selectortype, $element); | |
289 | $sourcexpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($sourceselector, $sourcelocator); | |
290 | ||
291 | list($containerselector, $containerlocator) = $this->transform_selector($containerselectortype, $containerelement); | |
292 | $destinationxpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($containerselector, $containerlocator); | |
293 | ||
294 | $this->getSession()->getDriver()->dragTo($sourcexpath, $destinationxpath); | |
295 | } | |
296 | ||
63950e4d DM |
297 | /** |
298 | * Checks, that the specified element is visible. Only available in tests using Javascript. | |
299 | * | |
300 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/ | |
301 | * @throws ElementNotFoundException | |
302 | * @throws ExpectationException | |
303 | * @throws DriverException | |
304 | * @param string $element | |
305 | * @param string $selectortype | |
306 | * @return void | |
307 | */ | |
308 | public function should_be_visible($element, $selectortype) { | |
309 | ||
310 | if (!$this->running_javascript()) { | |
311 | throw new DriverException('Visible checks are disabled in scenarios without Javascript support'); | |
312 | } | |
313 | ||
314 | $node = $this->get_selected_node($selectortype, $element); | |
315 | if (!$node->isVisible()) { | |
316 | throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession()); | |
317 | } | |
318 | } | |
319 | ||
320 | /** | |
321 | * Checks, that the specified element is not visible. Only available in tests using Javascript. | |
322 | * | |
323 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/ | |
324 | * @throws ElementNotFoundException | |
325 | * @throws ExpectationException | |
326 | * @param string $element | |
327 | * @param string $selectortype | |
328 | * @return void | |
329 | */ | |
330 | public function should_not_be_visible($element, $selectortype) { | |
331 | ||
332 | try { | |
333 | $this->should_be_visible($element, $selectortype); | |
334 | throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession()); | |
335 | } catch (ExpectationException $e) { | |
336 | // All as expected. | |
337 | } | |
338 | } | |
339 | ||
340 | /** | |
341 | * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript. | |
342 | * | |
343 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/ | |
344 | * @throws ElementNotFoundException | |
345 | * @throws DriverException | |
346 | * @throws ExpectationException | |
347 | * @param string $element Element we look for | |
348 | * @param string $selectortype The type of what we look for | |
349 | * @param string $nodeelement Element we look in | |
350 | * @param string $nodeselectortype The type of selector where we look in | |
351 | */ | |
352 | public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) { | |
353 | ||
354 | if (!$this->running_javascript()) { | |
355 | throw new DriverException('Visible checks are disabled in scenarios without Javascript support'); | |
356 | } | |
357 | ||
358 | $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement); | |
359 | if (!$node->isVisible()) { | |
360 | throw new ExpectationException( | |
361 | '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible', | |
362 | $this->getSession() | |
363 | ); | |
364 | } | |
365 | } | |
366 | ||
367 | /** | |
368 | * Checks, that the specified element is not visible inside the specified container. Only available in tests using Javascript. | |
369 | * | |
370 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/ | |
371 | * @throws ElementNotFoundException | |
372 | * @throws ExpectationException | |
373 | * @param string $element Element we look for | |
374 | * @param string $selectortype The type of what we look for | |
375 | * @param string $nodeelement Element we look in | |
376 | * @param string $nodeselectortype The type of selector where we look in | |
377 | */ | |
378 | public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) { | |
379 | ||
380 | try { | |
381 | $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype); | |
382 | throw new ExpectationException( | |
383 | '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible', | |
384 | $this->getSession() | |
385 | ); | |
386 | } catch (ExpectationException $e) { | |
387 | // All as expected. | |
388 | } | |
389 | } | |
390 | ||
786ea937 | 391 | /** |
e9af3ed3 | 392 | * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests. |
786ea937 | 393 | * |
786ea937 | 394 | * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/ |
9a1f4922 | 395 | * @throws ExpectationException |
40923977 | 396 | * @param string $text |
786ea937 DM |
397 | */ |
398 | public function assert_page_contains_text($text) { | |
9a1f4922 | 399 | |
e9af3ed3 DM |
400 | // Looking for all the matching nodes without any other descendant matching the |
401 | // same xpath (we are using contains(., ....). | |
9a1f4922 | 402 | $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); |
e9af3ed3 DM |
403 | $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" . |
404 | "[count(descendant::*[contains(., $xpathliteral)]) = 0]"; | |
9a1f4922 DM |
405 | |
406 | // Wait until it finds the text, otherwise custom exception. | |
407 | try { | |
e9af3ed3 DM |
408 | $nodes = $this->find_all('xpath', $xpath); |
409 | ||
410 | // We also check for the element visibility when running JS tests. | |
411 | if ($this->running_javascript()) { | |
412 | foreach ($nodes as $node) { | |
413 | if ($node->isVisible()) { | |
414 | return; | |
415 | } | |
416 | } | |
417 | ||
418 | throw new ExpectationException("'{$text}' text was found but was not visible", $this->getSession()); | |
419 | } | |
420 | ||
9a1f4922 DM |
421 | } catch (ElementNotFoundException $e) { |
422 | throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession()); | |
423 | } | |
786ea937 DM |
424 | } |
425 | ||
426 | /** | |
e9af3ed3 | 427 | * Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden. |
786ea937 | 428 | * |
786ea937 | 429 | * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/ |
9a1f4922 | 430 | * @throws ExpectationException |
40923977 | 431 | * @param string $text |
786ea937 DM |
432 | */ |
433 | public function assert_page_not_contains_text($text) { | |
9a1f4922 | 434 | |
5458ab3e | 435 | // Delegating the process to assert_page_contains_text. |
9a1f4922 | 436 | try { |
5458ab3e DM |
437 | $this->assert_page_contains_text($text); |
438 | } catch (ExpectationException $e) { | |
439 | // It should not appear, so this is good. | |
440 | return; | |
9a1f4922 | 441 | } |
5458ab3e DM |
442 | |
443 | // If the page contains the text this is failing. | |
444 | throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession()); | |
786ea937 DM |
445 | } |
446 | ||
447 | /** | |
e9af3ed3 | 448 | * Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden. |
786ea937 | 449 | * |
40923977 | 450 | * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ |
5458ab3e DM |
451 | * @throws ElementNotFoundException |
452 | * @throws ExpectationException | |
40923977 DM |
453 | * @param string $text |
454 | * @param string $element Element we look in. | |
455 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 456 | */ |
40923977 DM |
457 | public function assert_element_contains_text($text, $element, $selectortype) { |
458 | ||
5458ab3e DM |
459 | // Getting the container where the text should be found. |
460 | $container = $this->get_selected_node($selectortype, $element); | |
461 | ||
e9af3ed3 DM |
462 | // Looking for all the matching nodes without any other descendant matching the |
463 | // same xpath (we are using contains(., ....). | |
5458ab3e | 464 | $xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); |
e9af3ed3 DM |
465 | $xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" . |
466 | "[count(descendant::*[contains(., $xpathliteral)]) = 0]"; | |
5458ab3e DM |
467 | |
468 | // Wait until it finds the text inside the container, otherwise custom exception. | |
469 | try { | |
e9af3ed3 DM |
470 | $nodes = $this->find_all('xpath', $xpath, false, $container); |
471 | ||
472 | // We also check for the element visibility when running JS tests. | |
473 | if ($this->running_javascript()) { | |
474 | foreach ($nodes as $node) { | |
475 | if ($node->isVisible()) { | |
476 | return; | |
477 | } | |
478 | } | |
479 | ||
480 | throw new ExpectationException("'{$text}' text was found in the {$element} element but was not visible", $this->getSession()); | |
481 | } | |
482 | ||
5458ab3e DM |
483 | } catch (ElementNotFoundException $e) { |
484 | throw new ExpectationException('"' . $text . '" text was not found in the ' . $element . ' element', $this->getSession()); | |
485 | } | |
486 | ||
786ea937 DM |
487 | } |
488 | ||
489 | /** | |
e9af3ed3 | 490 | * Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden. |
786ea937 | 491 | * |
40923977 | 492 | * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ |
5458ab3e DM |
493 | * @throws ElementNotFoundException |
494 | * @throws ExpectationException | |
40923977 DM |
495 | * @param string $text |
496 | * @param string $element Element we look in. | |
497 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 498 | */ |
40923977 DM |
499 | public function assert_element_not_contains_text($text, $element, $selectortype) { |
500 | ||
5458ab3e DM |
501 | // Delegating the process to assert_element_contains_text. |
502 | try { | |
503 | $this->assert_element_contains_text($text, $element, $selectortype); | |
504 | } catch (ExpectationException $e) { | |
505 | // It should not appear, so this is good. | |
506 | // We only catch ExpectationException as ElementNotFoundException | |
507 | // will be thrown if the container does not exist. | |
508 | return; | |
509 | } | |
510 | ||
511 | // If the element contains the text this is failing. | |
512 | throw new ExpectationException('"' . $text . '" text was found in the ' . $element . ' element', $this->getSession()); | |
786ea937 DM |
513 | } |
514 | ||
60054942 DM |
515 | /** |
516 | * Checks, that the first specified element appears before the second one. | |
517 | * | |
518 | * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
519 | * @throws ExpectationException | |
520 | * @param string $preelement The locator of the preceding element | |
521 | * @param string $preselectortype The locator of the preceding element | |
522 | * @param string $postelement The locator of the latest element | |
523 | * @param string $postselectortype The selector type of the latest element | |
524 | */ | |
525 | public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) { | |
526 | ||
527 | // We allow postselectortype as a non-text based selector. | |
528 | list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement); | |
529 | list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement); | |
530 | ||
531 | $prexpath = $this->find($preselector, $prelocator)->getXpath(); | |
532 | $postxpath = $this->find($postselector, $postlocator)->getXpath(); | |
533 | ||
534 | // Using following xpath axe to find it. | |
535 | $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"'; | |
536 | $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]'; | |
537 | if (!$this->getSession()->getDriver()->find($xpath)) { | |
538 | throw new ExpectationException($msg, $this->getSession()); | |
539 | } | |
540 | } | |
541 | ||
542 | /** | |
543 | * Checks, that the first specified element appears after the second one. | |
544 | * | |
545 | * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
546 | * @throws ExpectationException | |
547 | * @param string $postelement The locator of the latest element | |
548 | * @param string $postselectortype The selector type of the latest element | |
549 | * @param string $preelement The locator of the preceding element | |
550 | * @param string $preselectortype The locator of the preceding element | |
551 | */ | |
552 | public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) { | |
553 | ||
554 | // We allow postselectortype as a non-text based selector. | |
555 | list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement); | |
556 | list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement); | |
557 | ||
558 | $postxpath = $this->find($postselector, $postlocator)->getXpath(); | |
559 | $prexpath = $this->find($preselector, $prelocator)->getXpath(); | |
560 | ||
561 | // Using preceding xpath axe to find it. | |
562 | $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"'; | |
563 | $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]'; | |
564 | if (!$this->getSession()->getDriver()->find($xpath)) { | |
565 | throw new ExpectationException($msg, $this->getSession()); | |
566 | } | |
567 | } | |
568 | ||
786ea937 | 569 | /** |
40923977 | 570 | * Checks, that element of specified type is disabled. |
786ea937 | 571 | * |
40923977 | 572 | * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/ |
1f9ffbdb | 573 | * @throws ExpectationException Thrown by behat_base::find |
40923977 DM |
574 | * @param string $element Element we look in |
575 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 576 | */ |
40923977 | 577 | public function the_element_should_be_disabled($element, $selectortype) { |
786ea937 | 578 | |
40923977 DM |
579 | // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement. |
580 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
581 | |
582 | if (!$node->hasAttribute('disabled')) { | |
583 | throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession()); | |
584 | } | |
585 | } | |
586 | ||
587 | /** | |
40923977 | 588 | * Checks, that element of specified type is enabled. |
786ea937 | 589 | * |
40923977 | 590 | * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/ |
1f9ffbdb | 591 | * @throws ExpectationException Thrown by behat_base::find |
40923977 DM |
592 | * @param string $element Element we look on |
593 | * @param string $selectortype The type of where we look | |
786ea937 | 594 | */ |
40923977 | 595 | public function the_element_should_be_enabled($element, $selectortype) { |
1f9ffbdb | 596 | |
40923977 DM |
597 | // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement. |
598 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
599 | |
600 | if ($node->hasAttribute('disabled')) { | |
601 | throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession()); | |
602 | } | |
603 | } | |
604 | ||
ca4f33a7 | 605 | /** |
62eb5c46 EL |
606 | * Checks the provided element and selector type exists in the current page. |
607 | * | |
608 | * This step is for advanced users, use it if you don't find anything else suitable for what you need. | |
ca4f33a7 DM |
609 | * |
610 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/ | |
611 | * @throws ElementNotFoundException Thrown by behat_base::find | |
612 | * @param string $element The locator of the specified selector | |
613 | * @param string $selectortype The selector type | |
614 | */ | |
615 | public function should_exists($element, $selectortype) { | |
616 | ||
617 | // Getting Mink selector and locator. | |
618 | list($selector, $locator) = $this->transform_selector($selectortype, $element); | |
619 | ||
620 | // Will throw an ElementNotFoundException if it does not exist. | |
621 | $this->find($selector, $locator); | |
622 | } | |
623 | ||
624 | /** | |
62eb5c46 EL |
625 | * Checks that the provided element and selector type not exists in the current page. |
626 | * | |
627 | * This step is for advanced users, use it if you don't find anything else suitable for what you need. | |
ca4f33a7 DM |
628 | * |
629 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/ | |
630 | * @throws ExpectationException | |
631 | * @param string $element The locator of the specified selector | |
632 | * @param string $selectortype The selector type | |
633 | */ | |
634 | public function should_not_exists($element, $selectortype) { | |
635 | ||
636 | try { | |
637 | $this->should_exists($element, $selectortype); | |
638 | throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession()); | |
62eb5c46 | 639 | } catch (ElementNotFoundException $e) { |
ca4f33a7 DM |
640 | // It passes. |
641 | return; | |
642 | } | |
643 | } | |
644 | ||
066ef320 JM |
645 | /** |
646 | * This step triggers cron like a user would do going to admin/cron.php. | |
647 | * | |
648 | * @Given /^I trigger cron$/ | |
649 | */ | |
650 | public function i_trigger_cron() { | |
651 | $this->getSession()->visit($this->locate_path('/admin/cron.php')); | |
652 | } | |
653 | ||
786ea937 | 654 | } |