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 DM |
30 | use Behat\Mink\Exception\ExpectationException as ExpectationException, |
31 | Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; | |
786ea937 DM |
32 | |
33 | /** | |
34 | * Cross component steps definitions. | |
35 | * | |
36 | * Basic web application definitions from MinkExtension and | |
37 | * BehatchExtension. Definitions modified according to our needs | |
38 | * when necessary and including only the ones we need to avoid | |
39 | * overlapping and confusion. | |
40 | * | |
41 | * @package core | |
42 | * @category test | |
43 | * @copyright 2012 David Monllaó | |
44 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
45 | */ | |
46 | class behat_general extends behat_base { | |
47 | ||
48 | /** | |
49 | * Opens Moodle homepage. | |
50 | * | |
786ea937 DM |
51 | * @Given /^I am on homepage$/ |
52 | */ | |
53 | public function i_am_on_homepage() { | |
40923977 | 54 | $this->getSession()->visit($this->locate_path('/')); |
786ea937 DM |
55 | } |
56 | ||
18c84063 DM |
57 | /** |
58 | * Reloads the current page. | |
59 | * | |
60 | * @Given /^I reload the page$/ | |
61 | */ | |
62 | public function reload() { | |
63 | $this->getSession()->reload(); | |
64 | } | |
65 | ||
786ea937 DM |
66 | /** |
67 | * Clicks link with specified id|title|alt|text. | |
68 | * | |
786ea937 | 69 | * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/ |
1f9ffbdb | 70 | * @throws ElementNotFoundException Thrown by behat_base::find |
40923977 | 71 | * @param string $link |
786ea937 DM |
72 | */ |
73 | public function click_link($link) { | |
1f9ffbdb DM |
74 | |
75 | $linknode = $this->find_link($link); | |
76 | $linknode->click(); | |
786ea937 DM |
77 | } |
78 | ||
79 | /** | |
80 | * Waits X seconds. Required after an action that requires data from an AJAX request. | |
81 | * | |
82 | * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/ | |
83 | * @param int $seconds | |
84 | */ | |
85 | public function i_wait_seconds($seconds) { | |
86 | $this->getSession()->wait($seconds * 1000, false); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Waits until the page is completely loaded. This step is auto-executed after every step. | |
91 | * | |
92 | * @Given /^I wait until the page is ready$/ | |
93 | */ | |
94 | public function wait_until_the_page_is_ready() { | |
95 | $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")'); | |
96 | } | |
97 | ||
98 | /** | |
40923977 | 99 | * Generic mouse over action. Mouse over a element of the specified type. |
786ea937 | 100 | * |
40923977 DM |
101 | * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/ |
102 | * @param string $element Element we look for | |
103 | * @param string $selectortype The type of what we look for | |
786ea937 | 104 | */ |
40923977 | 105 | public function i_hover($element, $selectortype) { |
1f9ffbdb | 106 | |
40923977 DM |
107 | // Gets the node based on the requested selector type and locator. |
108 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
109 | $node->mouseOver(); |
110 | } | |
111 | ||
40923977 DM |
112 | /** |
113 | * Generic click action. Click on the element of the specified type. | |
114 | * | |
115 | * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/ | |
116 | * @param string $element Element we look for | |
117 | * @param string $selectortype The type of what we look for | |
118 | */ | |
119 | public function i_click_on($element, $selectortype) { | |
120 | ||
121 | // Gets the node based on the requested selector type and locator. | |
122 | $node = $this->get_selected_node($selectortype, $element); | |
123 | $node->click(); | |
124 | } | |
125 | ||
072f67fc DM |
126 | /** |
127 | * Click on the element of the specified type which is located inside the second element. | |
128 | * | |
129 | * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ | |
130 | * @param string $element Element we look for | |
131 | * @param string $selectortype The type of what we look for | |
132 | * @param string $nodeelement Element we look in | |
133 | * @param string $nodeselectortype The type of selector where we look in | |
134 | */ | |
135 | public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) { | |
136 | ||
137 | $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement); | |
138 | $node->click(); | |
139 | } | |
140 | ||
786ea937 DM |
141 | /** |
142 | * Checks, that page contains specified text. | |
143 | * | |
144 | * @see Behat\MinkExtension\Context\MinkContext | |
145 | * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/ | |
40923977 | 146 | * @param string $text |
786ea937 DM |
147 | */ |
148 | public function assert_page_contains_text($text) { | |
02ca6219 | 149 | $this->assertSession()->pageTextContains($text); |
786ea937 DM |
150 | } |
151 | ||
152 | /** | |
153 | * Checks, that page doesn't contain specified text. | |
154 | * | |
155 | * @see Behat\MinkExtension\Context\MinkContext | |
156 | * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/ | |
40923977 | 157 | * @param string $text |
786ea937 DM |
158 | */ |
159 | public function assert_page_not_contains_text($text) { | |
02ca6219 | 160 | $this->assertSession()->pageTextNotContains($text); |
786ea937 DM |
161 | } |
162 | ||
163 | /** | |
40923977 | 164 | * Checks, that element with specified CSS selector or XPath contains specified text. |
786ea937 | 165 | * |
40923977 DM |
166 | * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ |
167 | * @param string $text | |
168 | * @param string $element Element we look in. | |
169 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 170 | */ |
40923977 DM |
171 | public function assert_element_contains_text($text, $element, $selectortype) { |
172 | ||
173 | // Transforming from steps definitions selector/locator format to Mink format. | |
072f67fc | 174 | list($selector, $locator) = $this->transform_text_selector($selectortype, $element); |
40923977 | 175 | $this->assertSession()->elementTextContains($selector, $locator, $text); |
786ea937 DM |
176 | } |
177 | ||
178 | /** | |
40923977 | 179 | * Checks, that element with specified CSS selector or XPath doesn't contain specified text. |
786ea937 | 180 | * |
40923977 DM |
181 | * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/ |
182 | * @param string $text | |
183 | * @param string $element Element we look in. | |
184 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 185 | */ |
40923977 DM |
186 | public function assert_element_not_contains_text($text, $element, $selectortype) { |
187 | ||
188 | // Transforming from steps definitions selector/locator format to mink format. | |
072f67fc | 189 | list($selector, $locator) = $this->transform_text_selector($selectortype, $element); |
40923977 | 190 | $this->assertSession()->elementTextNotContains($selector, $locator, $text); |
786ea937 DM |
191 | } |
192 | ||
60054942 DM |
193 | /** |
194 | * Checks, that the first specified element appears before the second one. | |
195 | * | |
196 | * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
197 | * @throws ExpectationException | |
198 | * @param string $preelement The locator of the preceding element | |
199 | * @param string $preselectortype The locator of the preceding element | |
200 | * @param string $postelement The locator of the latest element | |
201 | * @param string $postselectortype The selector type of the latest element | |
202 | */ | |
203 | public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) { | |
204 | ||
205 | // We allow postselectortype as a non-text based selector. | |
206 | list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement); | |
207 | list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement); | |
208 | ||
209 | $prexpath = $this->find($preselector, $prelocator)->getXpath(); | |
210 | $postxpath = $this->find($postselector, $postlocator)->getXpath(); | |
211 | ||
212 | // Using following xpath axe to find it. | |
213 | $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"'; | |
214 | $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]'; | |
215 | if (!$this->getSession()->getDriver()->find($xpath)) { | |
216 | throw new ExpectationException($msg, $this->getSession()); | |
217 | } | |
218 | } | |
219 | ||
220 | /** | |
221 | * Checks, that the first specified element appears after the second one. | |
222 | * | |
223 | * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/ | |
224 | * @throws ExpectationException | |
225 | * @param string $postelement The locator of the latest element | |
226 | * @param string $postselectortype The selector type of the latest element | |
227 | * @param string $preelement The locator of the preceding element | |
228 | * @param string $preselectortype The locator of the preceding element | |
229 | */ | |
230 | public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) { | |
231 | ||
232 | // We allow postselectortype as a non-text based selector. | |
233 | list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement); | |
234 | list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement); | |
235 | ||
236 | $postxpath = $this->find($postselector, $postlocator)->getXpath(); | |
237 | $prexpath = $this->find($preselector, $prelocator)->getXpath(); | |
238 | ||
239 | // Using preceding xpath axe to find it. | |
240 | $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"'; | |
241 | $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]'; | |
242 | if (!$this->getSession()->getDriver()->find($xpath)) { | |
243 | throw new ExpectationException($msg, $this->getSession()); | |
244 | } | |
245 | } | |
246 | ||
786ea937 | 247 | /** |
40923977 | 248 | * Checks, that element of specified type is disabled. |
786ea937 | 249 | * |
40923977 | 250 | * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/ |
1f9ffbdb | 251 | * @throws ExpectationException Thrown by behat_base::find |
40923977 DM |
252 | * @param string $element Element we look in |
253 | * @param string $selectortype The type of element where we are looking in. | |
786ea937 | 254 | */ |
40923977 | 255 | public function the_element_should_be_disabled($element, $selectortype) { |
786ea937 | 256 | |
40923977 DM |
257 | // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement. |
258 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
259 | |
260 | if (!$node->hasAttribute('disabled')) { | |
261 | throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession()); | |
262 | } | |
263 | } | |
264 | ||
265 | /** | |
40923977 | 266 | * Checks, that element of specified type is enabled. |
786ea937 | 267 | * |
40923977 | 268 | * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/ |
1f9ffbdb | 269 | * @throws ExpectationException Thrown by behat_base::find |
40923977 DM |
270 | * @param string $element Element we look on |
271 | * @param string $selectortype The type of where we look | |
786ea937 | 272 | */ |
40923977 | 273 | public function the_element_should_be_enabled($element, $selectortype) { |
1f9ffbdb | 274 | |
40923977 DM |
275 | // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement. |
276 | $node = $this->get_selected_node($selectortype, $element); | |
786ea937 DM |
277 | |
278 | if ($node->hasAttribute('disabled')) { | |
279 | throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession()); | |
280 | } | |
281 | } | |
282 | ||
ca4f33a7 | 283 | /** |
62eb5c46 EL |
284 | * Checks the provided element and selector type exists in the current page. |
285 | * | |
286 | * This step is for advanced users, use it if you don't find anything else suitable for what you need. | |
ca4f33a7 DM |
287 | * |
288 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/ | |
289 | * @throws ElementNotFoundException Thrown by behat_base::find | |
290 | * @param string $element The locator of the specified selector | |
291 | * @param string $selectortype The selector type | |
292 | */ | |
293 | public function should_exists($element, $selectortype) { | |
294 | ||
295 | // Getting Mink selector and locator. | |
296 | list($selector, $locator) = $this->transform_selector($selectortype, $element); | |
297 | ||
298 | // Will throw an ElementNotFoundException if it does not exist. | |
299 | $this->find($selector, $locator); | |
300 | } | |
301 | ||
302 | /** | |
62eb5c46 EL |
303 | * Checks that the provided element and selector type not exists in the current page. |
304 | * | |
305 | * This step is for advanced users, use it if you don't find anything else suitable for what you need. | |
ca4f33a7 DM |
306 | * |
307 | * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/ | |
308 | * @throws ExpectationException | |
309 | * @param string $element The locator of the specified selector | |
310 | * @param string $selectortype The selector type | |
311 | */ | |
312 | public function should_not_exists($element, $selectortype) { | |
313 | ||
314 | try { | |
315 | $this->should_exists($element, $selectortype); | |
316 | throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession()); | |
62eb5c46 | 317 | } catch (ElementNotFoundException $e) { |
ca4f33a7 DM |
318 | // It passes. |
319 | return; | |
320 | } | |
321 | } | |
322 | ||
786ea937 | 323 | } |