1 |
efrain |
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 |
|
|
|
30 |
use Behat\Gherkin\Node\TableNode;
|
|
|
31 |
use Behat\Mink\Element\NodeElement;
|
|
|
32 |
use Behat\Mink\Exception\DriverException;
|
|
|
33 |
use Behat\Mink\Exception\ElementNotFoundException;
|
|
|
34 |
use Behat\Mink\Exception\ExpectationException;
|
11 |
efrain |
35 |
use Facebook\WebDriver\Exception\NoSuchAlertException;
|
1 |
efrain |
36 |
use Facebook\WebDriver\Exception\NoSuchElementException;
|
|
|
37 |
use Facebook\WebDriver\Exception\StaleElementReferenceException;
|
11 |
efrain |
38 |
use Facebook\WebDriver\WebDriverAlert;
|
|
|
39 |
use Facebook\WebDriver\WebDriverExpectedCondition;
|
1 |
efrain |
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Cross component steps definitions.
|
|
|
43 |
*
|
|
|
44 |
* Basic web application definitions from MinkExtension and
|
|
|
45 |
* BehatchExtension. Definitions modified according to our needs
|
|
|
46 |
* when necessary and including only the ones we need to avoid
|
|
|
47 |
* overlapping and confusion.
|
|
|
48 |
*
|
|
|
49 |
* @package core
|
|
|
50 |
* @category test
|
|
|
51 |
* @copyright 2012 David Monllaó
|
|
|
52 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
53 |
*/
|
|
|
54 |
class behat_general extends behat_base {
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @var string used by {@link switch_to_window()} and
|
|
|
58 |
* {@link switch_to_the_main_window()} to work-around a Chrome browser issue.
|
|
|
59 |
*/
|
|
|
60 |
const MAIN_WINDOW_NAME = '__moodle_behat_main_window_name';
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* @var string when we want to check whether or not a new page has loaded,
|
|
|
64 |
* we first write this unique string into the page. Then later, by checking
|
|
|
65 |
* whether it is still there, we can tell if a new page has been loaded.
|
|
|
66 |
*/
|
|
|
67 |
const PAGE_LOAD_DETECTION_STRING = 'new_page_not_loaded_since_behat_started_watching';
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* @var $pageloaddetectionrunning boolean Used to ensure that page load detection was started before a page reload
|
|
|
71 |
* was checked for.
|
|
|
72 |
*/
|
|
|
73 |
private $pageloaddetectionrunning = false;
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Opens Moodle homepage.
|
|
|
77 |
*
|
|
|
78 |
* @Given /^I am on homepage$/
|
|
|
79 |
*/
|
|
|
80 |
public function i_am_on_homepage() {
|
|
|
81 |
$this->execute('behat_general::i_visit', ['/']);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Opens Moodle site homepage.
|
|
|
86 |
*
|
|
|
87 |
* @Given /^I am on site homepage$/
|
|
|
88 |
*/
|
|
|
89 |
public function i_am_on_site_homepage() {
|
|
|
90 |
$this->execute('behat_general::i_visit', ['/?redirect=0']);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Opens course index page.
|
|
|
95 |
*
|
|
|
96 |
* @Given /^I am on course index$/
|
|
|
97 |
*/
|
|
|
98 |
public function i_am_on_course_index() {
|
|
|
99 |
$this->execute('behat_general::i_visit', ['/course/index.php']);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Reloads the current page.
|
|
|
104 |
*
|
|
|
105 |
* @Given /^I reload the page$/
|
|
|
106 |
*/
|
|
|
107 |
public function reload() {
|
|
|
108 |
$this->getSession()->reload();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Follows the page redirection. Use this step after any action that shows a message and waits for a redirection
|
|
|
113 |
*
|
|
|
114 |
* @Given /^I wait to be redirected$/
|
|
|
115 |
*/
|
|
|
116 |
public function i_wait_to_be_redirected() {
|
|
|
117 |
|
|
|
118 |
// Xpath and processes based on core_renderer::redirect_message(), core_renderer::$metarefreshtag and
|
|
|
119 |
// moodle_page::$periodicrefreshdelay possible values.
|
|
|
120 |
if (!$metarefresh = $this->getSession()->getPage()->find('xpath', "//head/descendant::meta[@http-equiv='refresh']")) {
|
|
|
121 |
// We don't fail the scenario if no redirection with message is found to avoid race condition false failures.
|
|
|
122 |
return true;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
// Wrapped in try & catch in case the redirection has already been executed.
|
|
|
126 |
try {
|
|
|
127 |
$content = $metarefresh->getAttribute('content');
|
|
|
128 |
} catch (NoSuchElementException $e) {
|
|
|
129 |
return true;
|
|
|
130 |
} catch (StaleElementReferenceException $e) {
|
|
|
131 |
return true;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Getting the refresh time and the url if present.
|
|
|
135 |
if (strstr($content, 'url') != false) {
|
|
|
136 |
|
|
|
137 |
list($waittime, $url) = explode(';', $content);
|
|
|
138 |
|
|
|
139 |
// Cleaning the URL value.
|
|
|
140 |
$url = trim(substr($url, strpos($url, 'http')));
|
|
|
141 |
|
|
|
142 |
} else {
|
|
|
143 |
// Just wait then.
|
|
|
144 |
$waittime = $content;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
// Wait until the URL change is executed.
|
|
|
149 |
if ($this->running_javascript()) {
|
|
|
150 |
$this->getSession()->wait($waittime * 1000);
|
|
|
151 |
|
|
|
152 |
} else if (!empty($url)) {
|
|
|
153 |
// We redirect directly as we can not wait for an automatic redirection.
|
|
|
154 |
$this->getSession()->getDriver()->getClient()->request('GET', $url);
|
|
|
155 |
|
|
|
156 |
} else {
|
|
|
157 |
// Reload the page if no URL was provided.
|
|
|
158 |
$this->getSession()->getDriver()->reload();
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Switches to the specified iframe.
|
|
|
164 |
*
|
|
|
165 |
* @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" iframe$/
|
|
|
166 |
* @Given /^I switch to "(?P<iframe_name_string>(?:[^"]|\\")*)" class iframe$/
|
|
|
167 |
* @param string $name The name of the iframe
|
|
|
168 |
*/
|
|
|
169 |
public function switch_to_iframe($name) {
|
|
|
170 |
// We spin to give time to the iframe to be loaded.
|
|
|
171 |
// Using extended timeout as we don't know about which
|
|
|
172 |
// kind of iframe will be loaded.
|
|
|
173 |
$this->spin(
|
|
|
174 |
function($context) use ($name){
|
|
|
175 |
$iframe = $context->find('iframe', $name);
|
|
|
176 |
if ($iframe->hasAttribute('name')) {
|
|
|
177 |
$iframename = $iframe->getAttribute('name');
|
|
|
178 |
} else {
|
|
|
179 |
if (!$this->running_javascript()) {
|
|
|
180 |
throw new \coding_exception('iframe must have a name attribute to use the switchTo command.');
|
|
|
181 |
}
|
|
|
182 |
$iframename = uniqid();
|
|
|
183 |
$this->execute_js_on_node($iframe, "{{ELEMENT}}.name = '{$iframename}';");
|
|
|
184 |
}
|
|
|
185 |
$context->getSession()->switchToIFrame($iframename);
|
|
|
186 |
|
|
|
187 |
// If no exception we are done.
|
|
|
188 |
return true;
|
|
|
189 |
},
|
|
|
190 |
behat_base::get_extended_timeout()
|
|
|
191 |
);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Switches to the main Moodle frame.
|
|
|
196 |
*
|
|
|
197 |
* @Given /^I switch to the main frame$/
|
|
|
198 |
*/
|
|
|
199 |
public function switch_to_the_main_frame() {
|
|
|
200 |
$this->getSession()->switchToIFrame();
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Switches to the specified window. Useful when interacting with popup windows.
|
|
|
205 |
*
|
|
|
206 |
* @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" (window|tab)$/
|
|
|
207 |
* @param string $windowname
|
|
|
208 |
*/
|
|
|
209 |
public function switch_to_window($windowname) {
|
|
|
210 |
if ($windowname === self::MAIN_WINDOW_NAME) {
|
|
|
211 |
// When switching to the main window normalise the window name to null.
|
|
|
212 |
// This is normalised further in the Mink driver to the root window ID.
|
|
|
213 |
$windowname = null;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
$this->getSession()->switchToWindow($windowname);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Switches to a second window.
|
|
|
221 |
*
|
|
|
222 |
* @Given /^I switch to a second window$/
|
|
|
223 |
* @throws DriverException If there aren't exactly 2 windows open.
|
|
|
224 |
*/
|
|
|
225 |
public function switch_to_second_window() {
|
|
|
226 |
$names = $this->getSession()->getWindowNames();
|
|
|
227 |
|
|
|
228 |
if (count($names) !== 2) {
|
|
|
229 |
throw new DriverException('Expected to see 2 windows open, found ' . count($names));
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
$this->getSession()->switchToWindow($names[1]);
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Switches to the main Moodle window. Useful when you finish interacting with popup windows.
|
|
|
237 |
*
|
|
|
238 |
* @Given /^I switch to the main (window|tab)$/
|
|
|
239 |
*/
|
|
|
240 |
public function switch_to_the_main_window() {
|
|
|
241 |
$this->switch_to_window(self::MAIN_WINDOW_NAME);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Closes all extra windows opened during the navigation.
|
|
|
246 |
*
|
|
|
247 |
* This assumes all popups are opened by the main tab and you will now get back.
|
|
|
248 |
*
|
|
|
249 |
* @Given /^I close all opened windows$/
|
|
|
250 |
* @throws DriverException If there aren't exactly 1 tabs open when finish or no javascript running
|
|
|
251 |
*/
|
|
|
252 |
public function i_close_all_opened_windows() {
|
|
|
253 |
if (!$this->running_javascript()) {
|
|
|
254 |
throw new DriverException('Closing windows steps require javascript');
|
|
|
255 |
}
|
|
|
256 |
$names = $this->getSession()->getWindowNames();
|
|
|
257 |
for ($index = 1; $index < count($names); $index ++) {
|
|
|
258 |
$this->getSession()->switchToWindow($names[$index]);
|
|
|
259 |
$this->execute_script("window.open('', '_self').close();");
|
|
|
260 |
}
|
|
|
261 |
$names = $this->getSession()->getWindowNames();
|
|
|
262 |
if (count($names) !== 1) {
|
|
|
263 |
throw new DriverException('Expected to see 1 tabs open, not ' . count($names));
|
|
|
264 |
}
|
|
|
265 |
$this->getSession()->switchToWindow($names[0]);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
11 |
efrain |
269 |
* Wait for an alert to be displayed.
|
|
|
270 |
*
|
|
|
271 |
* @return WebDriverAlert
|
|
|
272 |
*/
|
|
|
273 |
public function wait_for_alert(): WebDriverAlert {
|
|
|
274 |
$webdriver = $this->getSession()->getDriver()->getWebdriver();
|
|
|
275 |
$webdriver->wait()->until(WebDriverExpectedCondition::alertIsPresent());
|
|
|
276 |
|
|
|
277 |
return $webdriver->switchTo()->alert();
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
1 |
efrain |
281 |
* Accepts the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
|
|
|
282 |
* @Given /^I accept the currently displayed dialog$/
|
|
|
283 |
*/
|
|
|
284 |
public function accept_currently_displayed_alert_dialog() {
|
11 |
efrain |
285 |
$alert = $this->wait_for_alert();
|
|
|
286 |
$alert->accept();
|
1 |
efrain |
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Dismisses the currently displayed alert dialog. This step does not work in all the browsers, consider it experimental.
|
|
|
291 |
* @Given /^I dismiss the currently displayed dialog$/
|
|
|
292 |
*/
|
|
|
293 |
public function dismiss_currently_displayed_alert_dialog() {
|
11 |
efrain |
294 |
$alert = $this->wait_for_alert();
|
|
|
295 |
$alert->dismiss();
|
1 |
efrain |
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Clicks link with specified id|title|alt|text.
|
|
|
300 |
*
|
|
|
301 |
* @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
|
|
|
302 |
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
|
303 |
* @param string $link
|
|
|
304 |
*/
|
|
|
305 |
public function click_link($link) {
|
|
|
306 |
$linknode = $this->find_link($link);
|
|
|
307 |
$linknode->click();
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Waits X seconds. Required after an action that requires data from an AJAX request.
|
|
|
312 |
*
|
|
|
313 |
* @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
|
|
|
314 |
* @param int $seconds
|
|
|
315 |
*/
|
|
|
316 |
public function i_wait_seconds($seconds) {
|
|
|
317 |
if ($this->running_javascript()) {
|
|
|
318 |
$this->getSession()->wait($seconds * 1000);
|
|
|
319 |
} else {
|
|
|
320 |
sleep($seconds);
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* Waits until the page is completely loaded. This step is auto-executed after every step.
|
|
|
326 |
*
|
|
|
327 |
* @Given /^I wait until the page is ready$/
|
|
|
328 |
*/
|
|
|
329 |
public function wait_until_the_page_is_ready() {
|
|
|
330 |
|
|
|
331 |
// No need to wait if not running JS.
|
|
|
332 |
if (!$this->running_javascript()) {
|
|
|
333 |
return;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
$this->getSession()->wait(self::get_timeout() * 1000, self::PAGE_READY_JS);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* Waits until the provided element selector exists in the DOM
|
|
|
341 |
*
|
|
|
342 |
* Using the protected method as this method will be usually
|
|
|
343 |
* called by other methods which are not returning a set of
|
|
|
344 |
* steps and performs the actions directly, so it would not
|
|
|
345 |
* be executed if it returns another step.
|
|
|
346 |
|
|
|
347 |
* @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" exists$/
|
|
|
348 |
* @param string $element
|
|
|
349 |
* @param string $selector
|
|
|
350 |
* @return void
|
|
|
351 |
*/
|
|
|
352 |
public function wait_until_exists($element, $selectortype) {
|
|
|
353 |
$this->ensure_element_exists($element, $selectortype);
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* Waits until the provided element does not exist in the DOM
|
|
|
358 |
*
|
|
|
359 |
* Using the protected method as this method will be usually
|
|
|
360 |
* called by other methods which are not returning a set of
|
|
|
361 |
* steps and performs the actions directly, so it would not
|
|
|
362 |
* be executed if it returns another step.
|
|
|
363 |
|
|
|
364 |
* @Given /^I wait until "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" does not exist$/
|
|
|
365 |
* @param string $element
|
|
|
366 |
* @param string $selector
|
|
|
367 |
* @return void
|
|
|
368 |
*/
|
|
|
369 |
public function wait_until_does_not_exists($element, $selectortype) {
|
|
|
370 |
$this->ensure_element_does_not_exist($element, $selectortype);
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
/**
|
|
|
374 |
* Generic mouse over action. Mouse over a element of the specified type.
|
|
|
375 |
*
|
|
|
376 |
* @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
|
377 |
* @param string $element Element we look for
|
|
|
378 |
* @param string $selectortype The type of what we look for
|
|
|
379 |
*/
|
|
|
380 |
public function i_hover($element, $selectortype) {
|
|
|
381 |
// Gets the node based on the requested selector type and locator.
|
|
|
382 |
$node = $this->get_selected_node($selectortype, $element);
|
|
|
383 |
$this->execute_js_on_node($node, '{{ELEMENT}}.scrollIntoView();');
|
|
|
384 |
$node->mouseOver();
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
/**
|
|
|
388 |
* Generic mouse over action. Mouse over a element of the specified type.
|
|
|
389 |
*
|
|
|
390 |
* @When I hover over the :element :selectortype in the :containerelement :containerselectortype
|
|
|
391 |
* @param string $element Element we look for
|
|
|
392 |
* @param string $selectortype The type of what we look for
|
|
|
393 |
* @param string $containerelement Element we look for
|
|
|
394 |
* @param string $containerselectortype The type of what we look for
|
|
|
395 |
*/
|
|
|
396 |
public function i_hover_in_the(string $element, $selectortype, string $containerelement, $containerselectortype): void {
|
|
|
397 |
// Gets the node based on the requested selector type and locator.
|
|
|
398 |
$node = $this->get_node_in_container($selectortype, $element, $containerselectortype, $containerelement);
|
|
|
399 |
$this->execute_js_on_node($node, '{{ELEMENT}}.scrollIntoView();');
|
|
|
400 |
$node->mouseOver();
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
/**
|
|
|
404 |
* Generic click action. Click on the element of the specified type.
|
|
|
405 |
*
|
|
|
406 |
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
|
407 |
* @param string $element Element we look for
|
|
|
408 |
* @param string $selectortype The type of what we look for
|
|
|
409 |
*/
|
|
|
410 |
public function i_click_on($element, $selectortype) {
|
|
|
411 |
// Gets the node based on the requested selector type and locator.
|
11 |
efrain |
412 |
$this->get_selected_node($selectortype, $element)->click();
|
1 |
efrain |
413 |
}
|
|
|
414 |
|
|
|
415 |
/**
|
|
|
416 |
* Sets the focus and takes away the focus from an element, generating blur JS event.
|
|
|
417 |
*
|
|
|
418 |
* @When /^I take focus off "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
|
419 |
* @param string $element Element we look for
|
|
|
420 |
* @param string $selectortype The type of what we look for
|
|
|
421 |
*/
|
|
|
422 |
public function i_take_focus_off_field($element, $selectortype) {
|
|
|
423 |
if (!$this->running_javascript()) {
|
|
|
424 |
throw new ExpectationException('Can\'t take focus off from "' . $element . '" in non-js mode', $this->getSession());
|
|
|
425 |
}
|
|
|
426 |
// Gets the node based on the requested selector type and locator.
|
|
|
427 |
$node = $this->get_selected_node($selectortype, $element);
|
|
|
428 |
$this->ensure_node_is_visible($node);
|
|
|
429 |
|
|
|
430 |
// Ensure element is focused before taking it off.
|
|
|
431 |
$node->focus();
|
|
|
432 |
$node->blur();
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
/**
|
|
|
436 |
* Clicks the specified element and confirms the expected dialogue.
|
|
|
437 |
*
|
|
|
438 |
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" confirming the dialogue$/
|
|
|
439 |
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
|
440 |
* @param string $element Element we look for
|
|
|
441 |
* @param string $selectortype The type of what we look for
|
|
|
442 |
*/
|
|
|
443 |
public function i_click_on_confirming_the_dialogue($element, $selectortype) {
|
|
|
444 |
$this->i_click_on($element, $selectortype);
|
|
|
445 |
$this->execute('behat_general::accept_currently_displayed_alert_dialog', []);
|
|
|
446 |
$this->wait_until_the_page_is_ready();
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
/**
|
|
|
450 |
* Clicks the specified element and dismissing the expected dialogue.
|
|
|
451 |
*
|
|
|
452 |
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" dismissing the dialogue$/
|
|
|
453 |
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
|
454 |
* @param string $element Element we look for
|
|
|
455 |
* @param string $selectortype The type of what we look for
|
|
|
456 |
*/
|
|
|
457 |
public function i_click_on_dismissing_the_dialogue($element, $selectortype) {
|
|
|
458 |
$this->i_click_on($element, $selectortype);
|
|
|
459 |
$this->execute('behat_general::dismiss_currently_displayed_alert_dialog', []);
|
|
|
460 |
$this->wait_until_the_page_is_ready();
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
/**
|
|
|
464 |
* Click on the element of the specified type which is located inside the second element.
|
|
|
465 |
*
|
|
|
466 |
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
|
467 |
* @param string $element Element we look for
|
|
|
468 |
* @param string $selectortype The type of what we look for
|
|
|
469 |
* @param string $nodeelement Element we look in
|
|
|
470 |
* @param string $nodeselectortype The type of selector where we look in
|
|
|
471 |
*/
|
|
|
472 |
public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
|
|
|
473 |
$node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
|
|
|
474 |
$node->click();
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* Click on the element with some modifier key pressed (alt, shift, meta or control).
|
|
|
479 |
*
|
|
|
480 |
* It is important to note that not all HTML elements are compatible with this step because
|
|
|
481 |
* the webdriver limitations. For example, alt click on checkboxes with a visible label will
|
|
|
482 |
* produce a normal checkbox click without the modifier.
|
|
|
483 |
*
|
|
|
484 |
* @When I :modifier click on :element :selectortype in the :nodeelement :nodeselectortype
|
|
|
485 |
* @param string $modifier the extra modifier to press (for example, alt+shift or shift)
|
|
|
486 |
* @param string $element Element we look for
|
|
|
487 |
* @param string $selectortype The type of what we look for
|
|
|
488 |
* @param string $nodeelement Element we look in
|
|
|
489 |
* @param string $nodeselectortype The type of selector where we look in
|
|
|
490 |
*/
|
|
|
491 |
public function i_key_click_on_in_the($modifier, $element, $selectortype, $nodeelement, $nodeselectortype) {
|
|
|
492 |
behat_base::require_javascript_in_session($this->getSession());
|
|
|
493 |
|
|
|
494 |
$key = null;
|
|
|
495 |
switch (strtoupper(trim($modifier))) {
|
|
|
496 |
case '':
|
|
|
497 |
break;
|
|
|
498 |
case 'SHIFT':
|
|
|
499 |
$key = behat_keys::SHIFT;
|
|
|
500 |
break;
|
|
|
501 |
case 'CTRL':
|
|
|
502 |
$key = behat_keys::CONTROL;
|
|
|
503 |
break;
|
|
|
504 |
case 'ALT':
|
|
|
505 |
$key = behat_keys::ALT;
|
|
|
506 |
break;
|
|
|
507 |
case 'META':
|
|
|
508 |
$key = behat_keys::META;
|
|
|
509 |
break;
|
|
|
510 |
default:
|
|
|
511 |
throw new \coding_exception("Unknown modifier key '$modifier'}");
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
$node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
|
|
|
515 |
|
|
|
516 |
// KeyUP and KeyDown require the element to be displayed in the current window.
|
|
|
517 |
$this->execute_js_on_node($node, '{{ELEMENT}}.scrollIntoView();');
|
|
|
518 |
$node->keyDown($key);
|
|
|
519 |
$node->click();
|
|
|
520 |
// Any click action can move the scroll. Ensure the element is still displayed.
|
|
|
521 |
$this->execute_js_on_node($node, '{{ELEMENT}}.scrollIntoView();');
|
|
|
522 |
$node->keyUp($key);
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
/**
|
|
|
526 |
* Drags and drops the specified element to the specified container. This step does not work in all the browsers, consider it experimental.
|
|
|
527 |
*
|
|
|
528 |
* The steps definitions calling this step as part of them should
|
|
|
529 |
* manage the wait times by themselves as the times and when the
|
|
|
530 |
* waits should be done depends on what is being dragged & dropper.
|
|
|
531 |
*
|
|
|
532 |
* @Given /^I drag "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" and I drop it in "(?P<container_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
|
|
|
533 |
* @param string $element
|
|
|
534 |
* @param string $selectortype
|
|
|
535 |
* @param string $containerelement
|
|
|
536 |
* @param string $containerselectortype
|
|
|
537 |
*/
|
|
|
538 |
public function i_drag_and_i_drop_it_in($source, $sourcetype, $target, $targettype) {
|
|
|
539 |
if (!$this->running_javascript()) {
|
|
|
540 |
throw new DriverException('Drag and drop steps require javascript');
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
$source = $this->find($sourcetype, $source);
|
|
|
544 |
$target = $this->find($targettype, $target);
|
|
|
545 |
|
|
|
546 |
if (!$source->isVisible()) {
|
|
|
547 |
throw new ExpectationException("'{$source}' '{$sourcetype}' is not visible", $this->getSession());
|
|
|
548 |
}
|
|
|
549 |
if (!$target->isVisible()) {
|
|
|
550 |
throw new ExpectationException("'{$target}' '{$targettype}' is not visible", $this->getSession());
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
$this->getSession()->getDriver()->dragTo($source->getXpath(), $target->getXpath());
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
/**
|
|
|
557 |
* Checks, that the specified element is visible. Only available in tests using Javascript.
|
|
|
558 |
*
|
|
|
559 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
|
|
|
560 |
* @throws ElementNotFoundException
|
|
|
561 |
* @throws ExpectationException
|
|
|
562 |
* @throws DriverException
|
|
|
563 |
* @param string $element
|
|
|
564 |
* @param string $selectortype
|
|
|
565 |
* @return void
|
|
|
566 |
*/
|
|
|
567 |
public function should_be_visible($element, $selectortype) {
|
|
|
568 |
|
|
|
569 |
if (!$this->running_javascript()) {
|
|
|
570 |
throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
|
|
|
571 |
}
|
|
|
572 |
|
|
|
573 |
$node = $this->get_selected_node($selectortype, $element);
|
|
|
574 |
if (!$node->isVisible()) {
|
|
|
575 |
throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
|
|
|
576 |
}
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
/**
|
|
|
580 |
* Checks, that the existing element is not visible. Only available in tests using Javascript.
|
|
|
581 |
*
|
|
|
582 |
* As a "not" method, it's performance could not be good, but in this
|
|
|
583 |
* case the performance is good because the element must exist,
|
|
|
584 |
* otherwise there would be a ElementNotFoundException, also here we are
|
|
|
585 |
* not spinning until the element is visible.
|
|
|
586 |
*
|
|
|
587 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
|
|
|
588 |
* @throws ElementNotFoundException
|
|
|
589 |
* @throws ExpectationException
|
|
|
590 |
* @param string $element
|
|
|
591 |
* @param string $selectortype
|
|
|
592 |
* @return void
|
|
|
593 |
*/
|
|
|
594 |
public function should_not_be_visible($element, $selectortype) {
|
|
|
595 |
|
|
|
596 |
try {
|
|
|
597 |
$this->should_be_visible($element, $selectortype);
|
|
|
598 |
} catch (ExpectationException $e) {
|
|
|
599 |
// All as expected.
|
|
|
600 |
return;
|
|
|
601 |
}
|
|
|
602 |
throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
|
|
|
603 |
}
|
|
|
604 |
|
|
|
605 |
/**
|
|
|
606 |
* Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
|
|
|
607 |
*
|
|
|
608 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
|
|
|
609 |
* @throws ElementNotFoundException
|
|
|
610 |
* @throws DriverException
|
|
|
611 |
* @throws ExpectationException
|
|
|
612 |
* @param string $element Element we look for
|
|
|
613 |
* @param string $selectortype The type of what we look for
|
|
|
614 |
* @param string $nodeelement Element we look in
|
|
|
615 |
* @param string $nodeselectortype The type of selector where we look in
|
|
|
616 |
*/
|
|
|
617 |
public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
|
|
|
618 |
|
|
|
619 |
if (!$this->running_javascript()) {
|
|
|
620 |
throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
$node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
|
|
|
624 |
if (!$node->isVisible()) {
|
|
|
625 |
throw new ExpectationException(
|
|
|
626 |
'"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
|
|
|
627 |
$this->getSession()
|
|
|
628 |
);
|
|
|
629 |
}
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
/**
|
|
|
633 |
* Checks, that the existing element is not visible inside the existing container. Only available in tests using Javascript.
|
|
|
634 |
*
|
|
|
635 |
* As a "not" method, it's performance could not be good, but in this
|
|
|
636 |
* case the performance is good because the element must exist,
|
|
|
637 |
* otherwise there would be a ElementNotFoundException, also here we are
|
|
|
638 |
* not spinning until the element is visible.
|
|
|
639 |
*
|
|
|
640 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
|
|
|
641 |
* @throws ElementNotFoundException
|
|
|
642 |
* @throws ExpectationException
|
|
|
643 |
* @param string $element Element we look for
|
|
|
644 |
* @param string $selectortype The type of what we look for
|
|
|
645 |
* @param string $nodeelement Element we look in
|
|
|
646 |
* @param string $nodeselectortype The type of selector where we look in
|
|
|
647 |
*/
|
|
|
648 |
public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
|
|
|
649 |
|
|
|
650 |
try {
|
|
|
651 |
$this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
|
|
|
652 |
} catch (ExpectationException $e) {
|
|
|
653 |
// All as expected.
|
|
|
654 |
return;
|
|
|
655 |
}
|
|
|
656 |
throw new ExpectationException(
|
|
|
657 |
'"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
|
|
|
658 |
$this->getSession()
|
|
|
659 |
);
|
|
|
660 |
}
|
|
|
661 |
|
|
|
662 |
/**
|
|
|
663 |
* Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
|
|
|
664 |
*
|
|
|
665 |
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
|
|
|
666 |
* @throws ExpectationException
|
|
|
667 |
* @param string $text
|
|
|
668 |
*/
|
|
|
669 |
public function assert_page_contains_text($text) {
|
|
|
670 |
|
|
|
671 |
// Looking for all the matching nodes without any other descendant matching the
|
|
|
672 |
// same xpath (we are using contains(., ....).
|
|
|
673 |
$xpathliteral = behat_context_helper::escape($text);
|
|
|
674 |
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
|
|
|
675 |
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
|
|
|
676 |
|
|
|
677 |
try {
|
|
|
678 |
$nodes = $this->find_all('xpath', $xpath);
|
|
|
679 |
} catch (ElementNotFoundException $e) {
|
|
|
680 |
throw new ExpectationException('"' . $text . '" text was not found in the page', $this->getSession());
|
|
|
681 |
}
|
|
|
682 |
|
|
|
683 |
// If we are not running javascript we have enough with the
|
|
|
684 |
// element existing as we can't check if it is visible.
|
|
|
685 |
if (!$this->running_javascript()) {
|
|
|
686 |
return;
|
|
|
687 |
}
|
|
|
688 |
|
|
|
689 |
// We spin as we don't have enough checking that the element is there, we
|
|
|
690 |
// should also ensure that the element is visible. Using microsleep as this
|
|
|
691 |
// is a repeated step and global performance is important.
|
|
|
692 |
$this->spin(
|
|
|
693 |
function($context, $args) {
|
|
|
694 |
|
|
|
695 |
foreach ($args['nodes'] as $node) {
|
|
|
696 |
if ($node->isVisible()) {
|
|
|
697 |
return true;
|
|
|
698 |
}
|
|
|
699 |
}
|
|
|
700 |
|
|
|
701 |
// If non of the nodes is visible we loop again.
|
|
|
702 |
throw new ExpectationException('"' . $args['text'] . '" text was found but was not visible', $context->getSession());
|
|
|
703 |
},
|
|
|
704 |
array('nodes' => $nodes, 'text' => $text),
|
|
|
705 |
false,
|
|
|
706 |
false,
|
|
|
707 |
true
|
|
|
708 |
);
|
|
|
709 |
|
|
|
710 |
}
|
|
|
711 |
|
|
|
712 |
/**
|
|
|
713 |
* Checks, that page doesn't contain specified text. When running Javascript tests it also considers that texts may be hidden.
|
|
|
714 |
*
|
|
|
715 |
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
|
|
|
716 |
* @throws ExpectationException
|
|
|
717 |
* @param string $text
|
|
|
718 |
*/
|
|
|
719 |
public function assert_page_not_contains_text($text) {
|
|
|
720 |
|
|
|
721 |
// Looking for all the matching nodes without any other descendant matching the
|
|
|
722 |
// same xpath (we are using contains(., ....).
|
|
|
723 |
$xpathliteral = behat_context_helper::escape($text);
|
|
|
724 |
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
|
|
|
725 |
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
|
|
|
726 |
|
|
|
727 |
// We should wait a while to ensure that the page is not still loading elements.
|
|
|
728 |
// Waiting less than self::get_timeout() as we already waited for the DOM to be ready and
|
|
|
729 |
// all JS to be executed.
|
|
|
730 |
try {
|
|
|
731 |
$nodes = $this->find_all('xpath', $xpath, false, false, self::get_reduced_timeout());
|
|
|
732 |
} catch (ElementNotFoundException $e) {
|
|
|
733 |
// All ok.
|
|
|
734 |
return;
|
|
|
735 |
}
|
|
|
736 |
|
|
|
737 |
// If we are not running javascript we have enough with the
|
|
|
738 |
// element existing as we can't check if it is hidden.
|
|
|
739 |
if (!$this->running_javascript()) {
|
|
|
740 |
throw new ExpectationException('"' . $text . '" text was found in the page', $this->getSession());
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
// If the element is there we should be sure that it is not visible.
|
|
|
744 |
$this->spin(
|
|
|
745 |
function($context, $args) {
|
|
|
746 |
|
|
|
747 |
foreach ($args['nodes'] as $node) {
|
|
|
748 |
// If element is removed from dom, then just exit.
|
|
|
749 |
try {
|
|
|
750 |
// If element is visible then throw exception, so we keep spinning.
|
|
|
751 |
if ($node->isVisible()) {
|
|
|
752 |
throw new ExpectationException('"' . $args['text'] . '" text was found in the page',
|
|
|
753 |
$context->getSession());
|
|
|
754 |
}
|
|
|
755 |
} catch (NoSuchElementException $e) {
|
|
|
756 |
// Do nothing just return, as element is no more on page.
|
|
|
757 |
return true;
|
|
|
758 |
} catch (ElementNotFoundException $e) {
|
|
|
759 |
// Do nothing just return, as element is no more on page.
|
|
|
760 |
return true;
|
|
|
761 |
}
|
|
|
762 |
}
|
|
|
763 |
|
|
|
764 |
// If non of the found nodes is visible we consider that the text is not visible.
|
|
|
765 |
return true;
|
|
|
766 |
},
|
|
|
767 |
array('nodes' => $nodes, 'text' => $text),
|
|
|
768 |
behat_base::get_reduced_timeout(),
|
|
|
769 |
false,
|
|
|
770 |
true
|
|
|
771 |
);
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
/**
|
|
|
775 |
* Checks, that the specified element contains the specified text. When running Javascript tests it also considers that texts may be hidden.
|
|
|
776 |
*
|
|
|
777 |
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
|
778 |
* @throws ElementNotFoundException
|
|
|
779 |
* @throws ExpectationException
|
|
|
780 |
* @param string $text
|
|
|
781 |
* @param string $element Element we look in.
|
|
|
782 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
783 |
*/
|
|
|
784 |
public function assert_element_contains_text($text, $element, $selectortype) {
|
|
|
785 |
|
|
|
786 |
// Getting the container where the text should be found.
|
|
|
787 |
$container = $this->get_selected_node($selectortype, $element);
|
|
|
788 |
|
|
|
789 |
// Looking for all the matching nodes without any other descendant matching the
|
|
|
790 |
// same xpath (we are using contains(., ....).
|
|
|
791 |
$xpathliteral = behat_context_helper::escape($text);
|
|
|
792 |
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
|
|
|
793 |
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
|
|
|
794 |
|
|
|
795 |
// Wait until it finds the text inside the container, otherwise custom exception.
|
|
|
796 |
try {
|
|
|
797 |
$nodes = $this->find_all('xpath', $xpath, false, $container);
|
|
|
798 |
} catch (ElementNotFoundException $e) {
|
|
|
799 |
throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
|
|
|
800 |
}
|
|
|
801 |
|
|
|
802 |
// If we are not running javascript we have enough with the
|
|
|
803 |
// element existing as we can't check if it is visible.
|
|
|
804 |
if (!$this->running_javascript()) {
|
|
|
805 |
return;
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
// We also check the element visibility when running JS tests. Using microsleep as this
|
|
|
809 |
// is a repeated step and global performance is important.
|
|
|
810 |
$this->spin(
|
|
|
811 |
function($context, $args) {
|
|
|
812 |
|
|
|
813 |
foreach ($args['nodes'] as $node) {
|
|
|
814 |
if ($node->isVisible()) {
|
|
|
815 |
return true;
|
|
|
816 |
}
|
|
|
817 |
}
|
|
|
818 |
|
|
|
819 |
throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element but was not visible', $context->getSession());
|
|
|
820 |
},
|
|
|
821 |
array('nodes' => $nodes, 'text' => $text, 'element' => $element),
|
|
|
822 |
false,
|
|
|
823 |
false,
|
|
|
824 |
true
|
|
|
825 |
);
|
|
|
826 |
}
|
|
|
827 |
|
|
|
828 |
/**
|
|
|
829 |
* Checks, that the specified element does not contain the specified text. When running Javascript tests it also considers that texts may be hidden.
|
|
|
830 |
*
|
|
|
831 |
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
|
832 |
* @throws ElementNotFoundException
|
|
|
833 |
* @throws ExpectationException
|
|
|
834 |
* @param string $text
|
|
|
835 |
* @param string $element Element we look in.
|
|
|
836 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
837 |
*/
|
|
|
838 |
public function assert_element_not_contains_text($text, $element, $selectortype) {
|
|
|
839 |
|
|
|
840 |
// Getting the container where the text should be found.
|
|
|
841 |
$container = $this->get_selected_node($selectortype, $element);
|
|
|
842 |
|
|
|
843 |
// Looking for all the matching nodes without any other descendant matching the
|
|
|
844 |
// same xpath (we are using contains(., ....).
|
|
|
845 |
$xpathliteral = behat_context_helper::escape($text);
|
|
|
846 |
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
|
|
|
847 |
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
|
|
|
848 |
|
|
|
849 |
// We should wait a while to ensure that the page is not still loading elements.
|
|
|
850 |
// Giving preference to the reliability of the results rather than to the performance.
|
|
|
851 |
try {
|
|
|
852 |
$nodes = $this->find_all('xpath', $xpath, false, $container, self::get_reduced_timeout());
|
|
|
853 |
} catch (ElementNotFoundException $e) {
|
|
|
854 |
// All ok.
|
|
|
855 |
return;
|
|
|
856 |
}
|
|
|
857 |
|
|
|
858 |
// If we are not running javascript we have enough with the
|
|
|
859 |
// element not being found as we can't check if it is visible.
|
|
|
860 |
if (!$this->running_javascript()) {
|
|
|
861 |
throw new ExpectationException('"' . $text . '" text was found in the "' . $element . '" element', $this->getSession());
|
|
|
862 |
}
|
|
|
863 |
|
|
|
864 |
// We need to ensure all the found nodes are hidden.
|
|
|
865 |
$this->spin(
|
|
|
866 |
function($context, $args) {
|
|
|
867 |
|
|
|
868 |
foreach ($args['nodes'] as $node) {
|
|
|
869 |
if ($node->isVisible()) {
|
|
|
870 |
throw new ExpectationException('"' . $args['text'] . '" text was found in the "' . $args['element'] . '" element', $context->getSession());
|
|
|
871 |
}
|
|
|
872 |
}
|
|
|
873 |
|
|
|
874 |
// If all the found nodes are hidden we are happy.
|
|
|
875 |
return true;
|
|
|
876 |
},
|
|
|
877 |
array('nodes' => $nodes, 'text' => $text, 'element' => $element),
|
|
|
878 |
behat_base::get_reduced_timeout(),
|
|
|
879 |
false,
|
|
|
880 |
true
|
|
|
881 |
);
|
|
|
882 |
}
|
|
|
883 |
|
|
|
884 |
/**
|
|
|
885 |
* Checks, that the first specified element appears before the second one.
|
|
|
886 |
*
|
|
|
887 |
* @Then :preelement :preselectortype should appear before :postelement :postselectortype
|
|
|
888 |
* @Then :preelement :preselectortype should appear before :postelement :postselectortype in the :containerelement :containerselectortype
|
|
|
889 |
* @throws ExpectationException
|
|
|
890 |
* @param string $preelement The locator of the preceding element
|
|
|
891 |
* @param string $preselectortype The selector type of the preceding element
|
|
|
892 |
* @param string $postelement The locator of the latest element
|
|
|
893 |
* @param string $postselectortype The selector type of the latest element
|
|
|
894 |
* @param string $containerelement
|
|
|
895 |
* @param string $containerselectortype
|
|
|
896 |
*/
|
|
|
897 |
public function should_appear_before(
|
|
|
898 |
string $preelement,
|
|
|
899 |
string $preselectortype,
|
|
|
900 |
string $postelement,
|
|
|
901 |
string $postselectortype,
|
|
|
902 |
?string $containerelement = null,
|
|
|
903 |
?string $containerselectortype = null
|
|
|
904 |
) {
|
|
|
905 |
$msg = "'{$preelement}' '{$preselectortype}' does not appear before '{$postelement}' '{$postselectortype}'";
|
|
|
906 |
$this->check_element_order(
|
|
|
907 |
$containerelement,
|
|
|
908 |
$containerselectortype,
|
|
|
909 |
$preelement,
|
|
|
910 |
$preselectortype,
|
|
|
911 |
$postelement,
|
|
|
912 |
$postselectortype,
|
|
|
913 |
$msg
|
|
|
914 |
);
|
|
|
915 |
}
|
|
|
916 |
|
|
|
917 |
/**
|
|
|
918 |
* Checks, that the first specified element appears after the second one.
|
|
|
919 |
*
|
|
|
920 |
* @Then :postelement :postselectortype should appear after :preelement :preselectortype
|
|
|
921 |
* @Then :postelement :postselectortype should appear after :preelement :preselectortype in the :containerelement :containerselectortype
|
|
|
922 |
* @throws ExpectationException
|
|
|
923 |
* @param string $postelement The locator of the latest element
|
|
|
924 |
* @param string $postselectortype The selector type of the latest element
|
|
|
925 |
* @param string $preelement The locator of the preceding element
|
|
|
926 |
* @param string $preselectortype The selector type of the preceding element
|
|
|
927 |
* @param string $containerelement
|
|
|
928 |
* @param string $containerselectortype
|
|
|
929 |
*/
|
|
|
930 |
public function should_appear_after(
|
|
|
931 |
string $postelement,
|
|
|
932 |
string $postselectortype,
|
|
|
933 |
string $preelement,
|
|
|
934 |
string $preselectortype,
|
|
|
935 |
?string $containerelement = null,
|
|
|
936 |
?string $containerselectortype = null
|
|
|
937 |
) {
|
|
|
938 |
$msg = "'{$postelement}' '{$postselectortype}' does not appear after '{$preelement}' '{$preselectortype}'";
|
|
|
939 |
$this->check_element_order(
|
|
|
940 |
$containerelement,
|
|
|
941 |
$containerselectortype,
|
|
|
942 |
$preelement,
|
|
|
943 |
$preselectortype,
|
|
|
944 |
$postelement,
|
|
|
945 |
$postselectortype,
|
|
|
946 |
$msg
|
|
|
947 |
);
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
/**
|
|
|
951 |
* Shared code to check whether an element is before or after another one.
|
|
|
952 |
*
|
|
|
953 |
* @param string $containerelement
|
|
|
954 |
* @param string $containerselectortype
|
|
|
955 |
* @param string $preelement The locator of the preceding element
|
|
|
956 |
* @param string $preselectortype The locator of the preceding element
|
|
|
957 |
* @param string $postelement The locator of the following element
|
|
|
958 |
* @param string $postselectortype The selector type of the following element
|
|
|
959 |
* @param string $msg Message to output if this fails
|
|
|
960 |
*/
|
|
|
961 |
protected function check_element_order(
|
|
|
962 |
?string $containerelement,
|
|
|
963 |
?string $containerselectortype,
|
|
|
964 |
string $preelement,
|
|
|
965 |
string $preselectortype,
|
|
|
966 |
string $postelement,
|
|
|
967 |
string $postselectortype,
|
|
|
968 |
string $msg
|
|
|
969 |
) {
|
|
|
970 |
$containernode = false;
|
|
|
971 |
if ($containerselectortype && $containerelement) {
|
|
|
972 |
// Get the container node.
|
|
|
973 |
$containernode = $this->get_selected_node($containerselectortype, $containerelement);
|
|
|
974 |
$msg .= " in the '{$containerelement}' '{$containerselectortype}'";
|
|
|
975 |
}
|
|
|
976 |
|
|
|
977 |
list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
|
|
|
978 |
list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
|
|
|
979 |
|
|
|
980 |
$newlines = [
|
|
|
981 |
"\r\n",
|
|
|
982 |
"\r",
|
|
|
983 |
"\n",
|
|
|
984 |
];
|
|
|
985 |
$prexpath = str_replace($newlines, ' ', $this->find($preselector, $prelocator, false, $containernode)->getXpath());
|
|
|
986 |
$postxpath = str_replace($newlines, ' ', $this->find($postselector, $postlocator, false, $containernode)->getXpath());
|
|
|
987 |
|
|
|
988 |
if ($this->running_javascript()) {
|
|
|
989 |
// The xpath to do this was running really slowly on certain Chrome versions so we are using
|
|
|
990 |
// this DOM method instead.
|
|
|
991 |
$js = <<<EOF
|
|
|
992 |
(function() {
|
|
|
993 |
var a = document.evaluate("{$prexpath}", document, null, XPathResult.ANY_TYPE, null).iterateNext();
|
|
|
994 |
var b = document.evaluate("{$postxpath}", document, null, XPathResult.ANY_TYPE, null).iterateNext();
|
|
|
995 |
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING;
|
|
|
996 |
})()
|
|
|
997 |
EOF;
|
|
|
998 |
$ok = $this->evaluate_script($js);
|
|
|
999 |
} else {
|
|
|
1000 |
|
|
|
1001 |
// Using following xpath axe to find it.
|
|
|
1002 |
$xpath = "{$prexpath}/following::*[contains(., {$postxpath})]";
|
|
|
1003 |
$ok = $this->getSession()->getDriver()->find($xpath);
|
|
|
1004 |
}
|
|
|
1005 |
|
|
|
1006 |
if (!$ok) {
|
|
|
1007 |
throw new ExpectationException($msg, $this->getSession());
|
|
|
1008 |
}
|
|
|
1009 |
}
|
|
|
1010 |
|
|
|
1011 |
/**
|
|
|
1012 |
* Checks, that element of specified type is disabled.
|
|
|
1013 |
*
|
|
|
1014 |
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
|
|
|
1015 |
* @throws ExpectationException Thrown by behat_base::find
|
|
|
1016 |
* @param string $element Element we look in
|
|
|
1017 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
1018 |
*/
|
|
|
1019 |
public function the_element_should_be_disabled($element, $selectortype) {
|
|
|
1020 |
$this->the_attribute_of_should_be_set("disabled", $element, $selectortype, false);
|
|
|
1021 |
}
|
|
|
1022 |
|
|
|
1023 |
/**
|
|
|
1024 |
* Checks, that element of specified type is enabled.
|
|
|
1025 |
*
|
|
|
1026 |
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
|
|
|
1027 |
* @throws ExpectationException Thrown by behat_base::find
|
|
|
1028 |
* @param string $element Element we look on
|
|
|
1029 |
* @param string $selectortype The type of where we look
|
|
|
1030 |
*/
|
|
|
1031 |
public function the_element_should_be_enabled($element, $selectortype) {
|
|
|
1032 |
$this->the_attribute_of_should_be_set("disabled", $element, $selectortype, true);
|
|
|
1033 |
}
|
|
|
1034 |
|
|
|
1035 |
/**
|
|
|
1036 |
* Checks the provided element and selector type are readonly on the current page.
|
|
|
1037 |
*
|
|
|
1038 |
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be readonly$/
|
|
|
1039 |
* @throws ExpectationException Thrown by behat_base::find
|
|
|
1040 |
* @param string $element Element we look in
|
|
|
1041 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
1042 |
*/
|
|
|
1043 |
public function the_element_should_be_readonly($element, $selectortype) {
|
|
|
1044 |
$this->the_attribute_of_should_be_set("readonly", $element, $selectortype, false);
|
|
|
1045 |
}
|
|
|
1046 |
|
|
|
1047 |
/**
|
|
|
1048 |
* Checks the provided element and selector type are not readonly on the current page.
|
|
|
1049 |
*
|
|
|
1050 |
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not be readonly$/
|
|
|
1051 |
* @throws ExpectationException Thrown by behat_base::find
|
|
|
1052 |
* @param string $element Element we look in
|
|
|
1053 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
1054 |
*/
|
|
|
1055 |
public function the_element_should_not_be_readonly($element, $selectortype) {
|
|
|
1056 |
$this->the_attribute_of_should_be_set("readonly", $element, $selectortype, true);
|
|
|
1057 |
}
|
|
|
1058 |
|
|
|
1059 |
/**
|
|
|
1060 |
* Checks the provided element and selector type exists in the current page.
|
|
|
1061 |
*
|
|
|
1062 |
* This step is for advanced users, use it if you don't find anything else suitable for what you need.
|
|
|
1063 |
*
|
|
|
1064 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist$/
|
|
|
1065 |
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
|
1066 |
* @param string $element The locator of the specified selector
|
|
|
1067 |
* @param string $selectortype The selector type
|
|
|
1068 |
*/
|
|
|
1069 |
public function should_exist($element, $selectortype) {
|
|
|
1070 |
// Will throw an ElementNotFoundException if it does not exist.
|
|
|
1071 |
$this->find($selectortype, $element);
|
|
|
1072 |
}
|
|
|
1073 |
|
|
|
1074 |
/**
|
|
|
1075 |
* Checks that the provided element and selector type not exists in the current page.
|
|
|
1076 |
*
|
|
|
1077 |
* This step is for advanced users, use it if you don't find anything else suitable for what you need.
|
|
|
1078 |
*
|
|
|
1079 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist$/
|
|
|
1080 |
* @throws ExpectationException
|
|
|
1081 |
* @param string $element The locator of the specified selector
|
|
|
1082 |
* @param string $selectortype The selector type
|
|
|
1083 |
*/
|
|
|
1084 |
public function should_not_exist($element, $selectortype) {
|
|
|
1085 |
// Will throw an ElementNotFoundException if it does not exist, but, actually it should not exist, so we try &
|
|
|
1086 |
// catch it.
|
|
|
1087 |
try {
|
|
|
1088 |
// The exception does not really matter as we will catch it and will never "explode".
|
|
|
1089 |
$exception = new ElementNotFoundException($this->getSession(), $selectortype, null, $element);
|
|
|
1090 |
|
|
|
1091 |
// Using the spin method as we want a reduced timeout but there is no need for a 0.1 seconds interval
|
|
|
1092 |
// because in the optimistic case we will timeout.
|
|
|
1093 |
// If all goes good it will throw an ElementNotFoundExceptionn that we will catch.
|
|
|
1094 |
$this->find($selectortype, $element, $exception, false, behat_base::get_reduced_timeout());
|
|
|
1095 |
} catch (ElementNotFoundException $e) {
|
|
|
1096 |
// We expect the element to not be found.
|
|
|
1097 |
return;
|
|
|
1098 |
}
|
|
|
1099 |
|
|
|
1100 |
// The element was found and should not have been. Throw an exception.
|
|
|
1101 |
throw new ExpectationException("The '{$element}' '{$selectortype}' exists in the current page", $this->getSession());
|
|
|
1102 |
}
|
|
|
1103 |
|
|
|
1104 |
/**
|
|
|
1105 |
* Ensure that edit mode is (not) available on the current page.
|
|
|
1106 |
*
|
|
|
1107 |
* @Then edit mode should be available on the current page
|
|
|
1108 |
* @Then edit mode should :not be available on the current page
|
|
|
1109 |
* @param bool $not
|
|
|
1110 |
*/
|
|
|
1111 |
public function edit_mode_should_be_available(bool $not = false): void {
|
|
|
1112 |
$isavailable = $this->is_edit_mode_available();
|
|
|
1113 |
$shouldbeavailable = empty($not);
|
|
|
1114 |
|
|
|
1115 |
if ($isavailable && !$shouldbeavailable) {
|
|
|
1116 |
throw new ExpectationException("Edit mode is available and should not be", $this->getSession());
|
|
|
1117 |
} else if ($shouldbeavailable && !$isavailable) {
|
|
|
1118 |
throw new ExpectationException("Edit mode is not available and should be", $this->getSession());
|
|
|
1119 |
}
|
|
|
1120 |
}
|
|
|
1121 |
|
|
|
1122 |
/**
|
|
|
1123 |
* Check whether edit mode is available on the current page.
|
|
|
1124 |
*
|
|
|
1125 |
* @return bool
|
|
|
1126 |
*/
|
|
|
1127 |
public function is_edit_mode_available(): bool {
|
|
|
1128 |
// If the course is already in editing mode then it will have the class 'editing' on the body.
|
|
|
1129 |
// This is a 'cheap' way of telling if the course is in editing mode and therefore if edit mode is available.
|
|
|
1130 |
$body = $this->find('css', 'body');
|
|
|
1131 |
if ($body->hasClass('editing')) {
|
|
|
1132 |
return true;
|
|
|
1133 |
}
|
|
|
1134 |
|
|
|
1135 |
try {
|
|
|
1136 |
$this->find('field', get_string('editmode'), false, false, 0);
|
|
|
1137 |
return true;
|
|
|
1138 |
} catch (ElementNotFoundException $e) {
|
|
|
1139 |
return false;
|
|
|
1140 |
}
|
|
|
1141 |
}
|
|
|
1142 |
|
|
|
1143 |
/**
|
|
|
1144 |
* This step triggers cron like a user would do going to admin/cron.php.
|
|
|
1145 |
*
|
|
|
1146 |
* @Given /^I trigger cron$/
|
|
|
1147 |
*/
|
|
|
1148 |
public function i_trigger_cron() {
|
|
|
1149 |
$this->execute('behat_general::i_visit', ['/admin/cron.php']);
|
|
|
1150 |
}
|
|
|
1151 |
|
|
|
1152 |
/**
|
|
|
1153 |
* Runs a scheduled task immediately, given full class name.
|
|
|
1154 |
*
|
|
|
1155 |
* This is faster and more reliable than running cron (running cron won't
|
|
|
1156 |
* work more than once in the same test, for instance). However it is
|
|
|
1157 |
* a little less 'realistic'.
|
|
|
1158 |
*
|
|
|
1159 |
* While the task is running, we suppress mtrace output because it makes
|
|
|
1160 |
* the Behat result look ugly.
|
|
|
1161 |
*
|
|
|
1162 |
* Note: Most of the code relating to running a task is based on
|
|
|
1163 |
* admin/cli/scheduled_task.php.
|
|
|
1164 |
*
|
|
|
1165 |
* @Given /^I run the scheduled task "(?P<task_name>[^"]+)"$/
|
|
|
1166 |
* @param string $taskname Name of task e.g. 'mod_whatever\task\do_something'
|
|
|
1167 |
*/
|
|
|
1168 |
public function i_run_the_scheduled_task($taskname) {
|
|
|
1169 |
$task = \core\task\manager::get_scheduled_task($taskname);
|
|
|
1170 |
if (!$task) {
|
|
|
1171 |
throw new DriverException('The "' . $taskname . '" scheduled task does not exist');
|
|
|
1172 |
}
|
|
|
1173 |
|
|
|
1174 |
// Do setup for cron task.
|
|
|
1175 |
raise_memory_limit(MEMORY_EXTRA);
|
|
|
1176 |
\core\cron::setup_user();
|
|
|
1177 |
|
|
|
1178 |
// Get lock.
|
|
|
1179 |
$cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
|
|
|
1180 |
if (!$cronlock = $cronlockfactory->get_lock('core_cron', 10)) {
|
|
|
1181 |
throw new DriverException('Unable to obtain core_cron lock for scheduled task');
|
|
|
1182 |
}
|
|
|
1183 |
if (!$lock = $cronlockfactory->get_lock('\\' . get_class($task), 10)) {
|
|
|
1184 |
$cronlock->release();
|
|
|
1185 |
throw new DriverException('Unable to obtain task lock for scheduled task');
|
|
|
1186 |
}
|
|
|
1187 |
$task->set_lock($lock);
|
|
|
1188 |
$cronlock->release();
|
|
|
1189 |
|
|
|
1190 |
try {
|
|
|
1191 |
// Prepare the renderer.
|
|
|
1192 |
\core\cron::prepare_core_renderer();
|
|
|
1193 |
|
|
|
1194 |
// Discard task output as not appropriate for Behat output!
|
|
|
1195 |
ob_start();
|
|
|
1196 |
$task->execute();
|
|
|
1197 |
ob_end_clean();
|
|
|
1198 |
|
|
|
1199 |
// Restore the previous renderer.
|
|
|
1200 |
\core\cron::prepare_core_renderer(true);
|
|
|
1201 |
|
|
|
1202 |
// Mark task complete.
|
|
|
1203 |
\core\task\manager::scheduled_task_complete($task);
|
|
|
1204 |
} catch (Exception $e) {
|
|
|
1205 |
// Restore the previous renderer.
|
|
|
1206 |
\core\cron::prepare_core_renderer(true);
|
|
|
1207 |
|
|
|
1208 |
// Mark task failed and throw exception.
|
|
|
1209 |
\core\task\manager::scheduled_task_failed($task);
|
|
|
1210 |
|
|
|
1211 |
throw new DriverException('The "' . $taskname . '" scheduled task failed', 0, $e);
|
|
|
1212 |
}
|
|
|
1213 |
}
|
|
|
1214 |
|
|
|
1215 |
/**
|
|
|
1216 |
* Runs all ad-hoc tasks in the queue.
|
|
|
1217 |
*
|
|
|
1218 |
* This is faster and more reliable than running cron (running cron won't
|
|
|
1219 |
* work more than once in the same test, for instance). However it is
|
|
|
1220 |
* a little less 'realistic'.
|
|
|
1221 |
*
|
|
|
1222 |
* While the task is running, we suppress mtrace output because it makes
|
|
|
1223 |
* the Behat result look ugly.
|
|
|
1224 |
*
|
|
|
1225 |
* @Given /^I run all adhoc tasks$/
|
|
|
1226 |
* @throws DriverException
|
|
|
1227 |
*/
|
|
|
1228 |
public function i_run_all_adhoc_tasks() {
|
|
|
1229 |
global $DB;
|
|
|
1230 |
|
|
|
1231 |
// Do setup for cron task.
|
|
|
1232 |
\core\cron::setup_user();
|
|
|
1233 |
|
|
|
1234 |
// Discard task output as not appropriate for Behat output!
|
|
|
1235 |
ob_start();
|
|
|
1236 |
|
|
|
1237 |
// Run all tasks which have a scheduled runtime of before now.
|
|
|
1238 |
$timenow = time();
|
|
|
1239 |
|
|
|
1240 |
while (!\core\task\manager::static_caches_cleared_since($timenow) &&
|
|
|
1241 |
$task = \core\task\manager::get_next_adhoc_task($timenow)) {
|
|
|
1242 |
// Clean the output buffer between tasks.
|
|
|
1243 |
ob_clean();
|
|
|
1244 |
|
|
|
1245 |
// Run the task.
|
|
|
1246 |
\core\cron::run_inner_adhoc_task($task);
|
|
|
1247 |
|
|
|
1248 |
// Check whether the task record still exists.
|
|
|
1249 |
// If a task was successful it will be removed.
|
|
|
1250 |
// If it failed then it will still exist.
|
|
|
1251 |
if ($DB->record_exists('task_adhoc', ['id' => $task->get_id()])) {
|
|
|
1252 |
// End ouptut buffering and flush the current buffer.
|
|
|
1253 |
// This should be from just the current task.
|
|
|
1254 |
ob_end_flush();
|
|
|
1255 |
|
|
|
1256 |
throw new DriverException('An adhoc task failed', 0);
|
|
|
1257 |
}
|
|
|
1258 |
}
|
|
|
1259 |
ob_end_clean();
|
|
|
1260 |
}
|
|
|
1261 |
|
|
|
1262 |
/**
|
|
|
1263 |
* Checks that an element and selector type exists in another element and selector type on the current page.
|
|
|
1264 |
*
|
|
|
1265 |
* This step is for advanced users, use it if you don't find anything else suitable for what you need.
|
|
|
1266 |
*
|
|
|
1267 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
|
|
|
1268 |
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
|
1269 |
* @param string $element The locator of the specified selector
|
|
|
1270 |
* @param string $selectortype The selector type
|
|
|
1271 |
* @param NodeElement|string $containerelement The locator of the container selector
|
|
|
1272 |
* @param string $containerselectortype The container selector type
|
|
|
1273 |
*/
|
|
|
1274 |
public function should_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
|
|
|
1275 |
// Will throw an ElementNotFoundException if it does not exist.
|
|
|
1276 |
$this->get_node_in_container($selectortype, $element, $containerselectortype, $containerelement);
|
|
|
1277 |
}
|
|
|
1278 |
|
|
|
1279 |
/**
|
|
|
1280 |
* Checks that an element and selector type does not exist in another element and selector type on the current page.
|
|
|
1281 |
*
|
|
|
1282 |
* This step is for advanced users, use it if you don't find anything else suitable for what you need.
|
|
|
1283 |
*
|
|
|
1284 |
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exist in the "(?P<element2_string>(?:[^"]|\\")*)" "(?P<selector2_string>[^"]*)"$/
|
|
|
1285 |
* @throws ExpectationException
|
|
|
1286 |
* @param string $element The locator of the specified selector
|
|
|
1287 |
* @param string $selectortype The selector type
|
|
|
1288 |
* @param NodeElement|string $containerelement The locator of the container selector
|
|
|
1289 |
* @param string $containerselectortype The container selector type
|
|
|
1290 |
*/
|
|
|
1291 |
public function should_not_exist_in_the($element, $selectortype, $containerelement, $containerselectortype) {
|
|
|
1292 |
// Get the container node.
|
|
|
1293 |
$containernode = $this->find($containerselectortype, $containerelement);
|
|
|
1294 |
|
|
|
1295 |
// Will throw an ElementNotFoundException if it does not exist, but, actually it should not exist, so we try &
|
|
|
1296 |
// catch it.
|
|
|
1297 |
try {
|
|
|
1298 |
// Looks for the requested node inside the container node.
|
|
|
1299 |
$this->find($selectortype, $element, false, $containernode, behat_base::get_reduced_timeout());
|
|
|
1300 |
} catch (ElementNotFoundException $e) {
|
|
|
1301 |
// We expect the element to not be found.
|
|
|
1302 |
return;
|
|
|
1303 |
}
|
|
|
1304 |
|
|
|
1305 |
// The element was found and should not have been. Throw an exception.
|
|
|
1306 |
$elementdescription = $this->get_selector_description($selectortype, $element);
|
|
|
1307 |
$containerdescription = $this->get_selector_description($containerselectortype, $containerelement);
|
|
|
1308 |
throw new ExpectationException(
|
|
|
1309 |
"The {$elementdescription} exists in the {$containerdescription}",
|
|
|
1310 |
$this->getSession()
|
|
|
1311 |
);
|
|
|
1312 |
}
|
|
|
1313 |
|
|
|
1314 |
/**
|
|
|
1315 |
* Change browser window size
|
|
|
1316 |
*
|
|
|
1317 |
* Allowed sizes:
|
|
|
1318 |
* - mobile: 425x750
|
|
|
1319 |
* - tablet: 768x1024
|
|
|
1320 |
* - small: 1024x768
|
|
|
1321 |
* - medium: 1366x768
|
|
|
1322 |
* - large: 2560x1600
|
|
|
1323 |
* - custom: widthxheight
|
|
|
1324 |
*
|
|
|
1325 |
* Example: I change window size to "small" or I change window size to "1024x768"
|
|
|
1326 |
* or I change viewport size to "800x600". The viewport option is useful to guarantee that the
|
|
|
1327 |
* browser window has same viewport size even when you run Behat on multiple operating systems.
|
|
|
1328 |
*
|
|
|
1329 |
* @throws ExpectationException
|
|
|
1330 |
* @Then /^I change (window|viewport) size to "(mobile|tablet|small|medium|large|\d+x\d+)"$/
|
|
|
1331 |
* @Then /^I change the (window|viewport) size to "(mobile|tablet|small|medium|large|\d+x\d+)"$/
|
|
|
1332 |
* @param string $windowsize size of the window (mobile|tablet|small|medium|large|wxh).
|
|
|
1333 |
*/
|
|
|
1334 |
public function i_change_window_size_to($windowviewport, $windowsize) {
|
|
|
1335 |
$this->resize_window($windowsize, $windowviewport === 'viewport');
|
|
|
1336 |
}
|
|
|
1337 |
|
|
|
1338 |
/**
|
|
|
1339 |
* Checks whether there the specified attribute is set or not.
|
|
|
1340 |
*
|
|
|
1341 |
* @Then the :attribute attribute of :element :selectortype should be set
|
|
|
1342 |
* @Then the :attribute attribute of :element :selectortype should :not be set
|
|
|
1343 |
*
|
|
|
1344 |
* @throws ExpectationException
|
|
|
1345 |
* @param string $attribute Name of attribute
|
|
|
1346 |
* @param string $element The locator of the specified selector
|
|
|
1347 |
* @param string $selectortype The selector type
|
|
|
1348 |
* @param string $not
|
|
|
1349 |
*/
|
|
|
1350 |
public function the_attribute_of_should_be_set($attribute, $element, $selectortype, $not = null) {
|
|
|
1351 |
// Get the container node (exception if it doesn't exist).
|
|
|
1352 |
$containernode = $this->get_selected_node($selectortype, $element);
|
|
|
1353 |
$hasattribute = $containernode->hasAttribute($attribute);
|
|
|
1354 |
|
|
|
1355 |
if ($not && $hasattribute) {
|
|
|
1356 |
$value = $containernode->getAttribute($attribute);
|
|
|
1357 |
// Should not be set but is.
|
|
|
1358 |
throw new ExpectationException(
|
|
|
1359 |
"The attribute \"{$attribute}\" should not be set but has a value of '{$value}'",
|
|
|
1360 |
$this->getSession()
|
|
|
1361 |
);
|
|
|
1362 |
} else if (!$not && !$hasattribute) {
|
|
|
1363 |
// Should be set but is not.
|
|
|
1364 |
throw new ExpectationException(
|
|
|
1365 |
"The attribute \"{$attribute}\" should be set but is not",
|
|
|
1366 |
$this->getSession()
|
|
|
1367 |
);
|
|
|
1368 |
}
|
|
|
1369 |
}
|
|
|
1370 |
|
|
|
1371 |
/**
|
|
|
1372 |
* Checks whether there is an attribute on the given element that contains the specified text.
|
|
|
1373 |
*
|
|
|
1374 |
* @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should contain "(?P<text_string>(?:[^"]|\\")*)"$/
|
|
|
1375 |
* @throws ExpectationException
|
|
|
1376 |
* @param string $attribute Name of attribute
|
|
|
1377 |
* @param string $element The locator of the specified selector
|
|
|
1378 |
* @param string $selectortype The selector type
|
|
|
1379 |
* @param string $text Expected substring
|
|
|
1380 |
*/
|
|
|
1381 |
public function the_attribute_of_should_contain($attribute, $element, $selectortype, $text) {
|
|
|
1382 |
// Get the container node (exception if it doesn't exist).
|
|
|
1383 |
$containernode = $this->get_selected_node($selectortype, $element);
|
|
|
1384 |
$value = $containernode->getAttribute($attribute);
|
|
|
1385 |
if ($value == null) {
|
|
|
1386 |
throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
|
|
|
1387 |
$this->getSession());
|
|
|
1388 |
} else if (strpos($value, $text) === false) {
|
|
|
1389 |
throw new ExpectationException('The attribute "' . $attribute .
|
|
|
1390 |
'" does not contain "' . $text . '" (actual value: "' . $value . '")',
|
|
|
1391 |
$this->getSession());
|
|
|
1392 |
}
|
|
|
1393 |
}
|
|
|
1394 |
|
|
|
1395 |
/**
|
|
|
1396 |
* Checks that the attribute on the given element does not contain the specified text.
|
|
|
1397 |
*
|
|
|
1398 |
* @Then /^the "(?P<attribute_string>[^"]*)" attribute of "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not contain "(?P<text_string>(?:[^"]|\\")*)"$/
|
|
|
1399 |
* @throws ExpectationException
|
|
|
1400 |
* @param string $attribute Name of attribute
|
|
|
1401 |
* @param string $element The locator of the specified selector
|
|
|
1402 |
* @param string $selectortype The selector type
|
|
|
1403 |
* @param string $text Expected substring
|
|
|
1404 |
*/
|
|
|
1405 |
public function the_attribute_of_should_not_contain($attribute, $element, $selectortype, $text) {
|
|
|
1406 |
// Get the container node (exception if it doesn't exist).
|
|
|
1407 |
$containernode = $this->get_selected_node($selectortype, $element);
|
|
|
1408 |
$value = $containernode->getAttribute($attribute);
|
|
|
1409 |
if ($value == null) {
|
|
|
1410 |
throw new ExpectationException('The attribute "' . $attribute. '" does not exist',
|
|
|
1411 |
$this->getSession());
|
|
|
1412 |
} else if (strpos($value, $text) !== false) {
|
|
|
1413 |
throw new ExpectationException('The attribute "' . $attribute .
|
|
|
1414 |
'" contains "' . $text . '" (value: "' . $value . '")',
|
|
|
1415 |
$this->getSession());
|
|
|
1416 |
}
|
|
|
1417 |
}
|
|
|
1418 |
|
|
|
1419 |
/**
|
|
|
1420 |
* Checks the provided value exists in specific row/column of table.
|
|
|
1421 |
*
|
|
|
1422 |
* @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should contain "(?P<value_string>[^"]*)"$/
|
|
|
1423 |
* @throws ElementNotFoundException
|
|
|
1424 |
* @param string $row row text which will be looked in.
|
|
|
1425 |
* @param string $column column text to search (or numeric value for the column position)
|
|
|
1426 |
* @param string $table table id/class/caption
|
|
|
1427 |
* @param string $value text to check.
|
|
|
1428 |
*/
|
|
|
1429 |
public function row_column_of_table_should_contain($row, $column, $table, $value) {
|
|
|
1430 |
$tablenode = $this->get_selected_node('table', $table);
|
|
|
1431 |
$tablexpath = $tablenode->getXpath();
|
|
|
1432 |
|
|
|
1433 |
$rowliteral = behat_context_helper::escape($row);
|
|
|
1434 |
$valueliteral = behat_context_helper::escape($value);
|
|
|
1435 |
|
11 |
efrain |
1436 |
$columnpositionxpath = $this->get_table_column_xpath($table, $column);
|
1 |
efrain |
1437 |
|
|
|
1438 |
// Check if value exists in specific row/column.
|
|
|
1439 |
// Get row xpath.
|
|
|
1440 |
// Some drivers make XPath relative to the current context, so use descendant.
|
|
|
1441 |
$rowxpath = $tablexpath . "/tbody/tr[descendant::*[@class='rowtitle'][normalize-space(.)=" . $rowliteral . "] | " . "
|
|
|
1442 |
descendant::th[normalize-space(.)=" . $rowliteral . "] | descendant::td[normalize-space(.)=" . $rowliteral . "]]";
|
|
|
1443 |
|
|
|
1444 |
$columnvaluexpath = $rowxpath . $columnpositionxpath . "[contains(normalize-space(.)," . $valueliteral . ")]";
|
|
|
1445 |
|
|
|
1446 |
// Looks for the requested node inside the container node.
|
|
|
1447 |
$coumnnode = $this->getSession()->getDriver()->find($columnvaluexpath);
|
|
|
1448 |
if (empty($coumnnode)) {
|
|
|
1449 |
$locatorexceptionmsg = $value . '" in "' . $row . '" row with column "' . $column;
|
|
|
1450 |
throw new ElementNotFoundException($this->getSession(), "\n$columnvaluexpath\n\n".'Column value', null, $locatorexceptionmsg);
|
|
|
1451 |
}
|
|
|
1452 |
}
|
|
|
1453 |
|
|
|
1454 |
/**
|
|
|
1455 |
* Checks the provided value should not exist in specific row/column of table.
|
|
|
1456 |
*
|
|
|
1457 |
* @Then /^"(?P<row_string>[^"]*)" row "(?P<column_string>[^"]*)" column of "(?P<table_string>[^"]*)" table should not contain "(?P<value_string>[^"]*)"$/
|
|
|
1458 |
* @throws ElementNotFoundException
|
|
|
1459 |
* @param string $row row text which will be looked in.
|
|
|
1460 |
* @param string $column column text to search
|
|
|
1461 |
* @param string $table table id/class/caption
|
|
|
1462 |
* @param string $value text to check.
|
|
|
1463 |
*/
|
|
|
1464 |
public function row_column_of_table_should_not_contain($row, $column, $table, $value) {
|
|
|
1465 |
try {
|
|
|
1466 |
$this->row_column_of_table_should_contain($row, $column, $table, $value);
|
|
|
1467 |
} catch (ElementNotFoundException $e) {
|
|
|
1468 |
// Table row/column doesn't contain this value. Nothing to do.
|
|
|
1469 |
return;
|
|
|
1470 |
}
|
|
|
1471 |
// Throw exception if found.
|
|
|
1472 |
throw new ExpectationException(
|
|
|
1473 |
'"' . $column . '" with value "' . $value . '" is present in "' . $row . '" row for table "' . $table . '"',
|
|
|
1474 |
$this->getSession()
|
|
|
1475 |
);
|
|
|
1476 |
}
|
|
|
1477 |
|
|
|
1478 |
/**
|
11 |
efrain |
1479 |
* Get xpath for a row child that corresponds to the specified column header
|
|
|
1480 |
*
|
|
|
1481 |
* @param string $table table identifier that can be used with 'table' node selector (i.e. table title or CSS class)
|
|
|
1482 |
* @param string $column either text in the column header or the column number, such as -1-, -2-, etc
|
|
|
1483 |
* When matching the column header it has to be either exact match of the whole header or an exact
|
|
|
1484 |
* match of a text inside a link in the header.
|
|
|
1485 |
* For example, to match "<a>First name</a> / <a>Last name</a>" you need to specify either "First name" or "Last name"
|
|
|
1486 |
* @return string
|
|
|
1487 |
*/
|
|
|
1488 |
protected function get_table_column_xpath(string $table, string $column): string {
|
|
|
1489 |
$tablenode = $this->get_selected_node('table', $table);
|
|
|
1490 |
$tablexpath = $tablenode->getXpath();
|
|
|
1491 |
$columnliteral = behat_context_helper::escape($column);
|
|
|
1492 |
if (preg_match('/^-?(\d+)-?$/', $column, $columnasnumber)) {
|
|
|
1493 |
// Column indicated as a number, just use it as position of the column.
|
|
|
1494 |
$columnpositionxpath = "/child::*[position() = {$columnasnumber[1]}]";
|
|
|
1495 |
} else {
|
|
|
1496 |
// Header can be in thead or tbody (first row), following xpath should work.
|
|
|
1497 |
$theadheaderxpath = "thead/tr[1]/th[(normalize-space(.)={$columnliteral} or a[normalize-space(text())=" .
|
|
|
1498 |
$columnliteral . "] or div[normalize-space(text())={$columnliteral}])]";
|
|
|
1499 |
$tbodyheaderxpath = "tbody/tr[1]/td[(normalize-space(.)={$columnliteral} or a[normalize-space(text())=" .
|
|
|
1500 |
$columnliteral . "] or div[normalize-space(text())={$columnliteral}])]";
|
|
|
1501 |
|
|
|
1502 |
// Check if column exists.
|
|
|
1503 |
$columnheaderxpath = "{$tablexpath}[{$theadheaderxpath} | {$tbodyheaderxpath}]";
|
|
|
1504 |
$columnheader = $this->getSession()->getDriver()->find($columnheaderxpath);
|
|
|
1505 |
if (empty($columnheader)) {
|
|
|
1506 |
if (strpos($column, '/') !== false) {
|
|
|
1507 |
// We are not able to match headers consisting of several links, such as "First name / Last name".
|
|
|
1508 |
// Instead we can match "First name" or "Last name" or "-1-" (column number).
|
|
|
1509 |
throw new Exception("Column matching locator \"$column\" not found. ".
|
|
|
1510 |
"If the column header contains multiple links, specify only one of the link texts. ".
|
|
|
1511 |
"Otherwise, use the column number as the locator");
|
|
|
1512 |
}
|
|
|
1513 |
$columnexceptionmsg = $column . '" in table "' . $table . '"';
|
|
|
1514 |
throw new ElementNotFoundException($this->getSession(), "\n$columnheaderxpath\n\n".'Column',
|
|
|
1515 |
null, $columnexceptionmsg);
|
|
|
1516 |
}
|
|
|
1517 |
// Following conditions were considered before finding column count.
|
|
|
1518 |
// 1. Table header can be in thead/tr/th or tbody/tr/td[1].
|
|
|
1519 |
// 2. First column can have th (Gradebook -> user report), so having lenient sibling check.
|
|
|
1520 |
$columnpositionxpath = "/child::*[position() = count({$tablexpath}/{$theadheaderxpath}" .
|
|
|
1521 |
"/preceding-sibling::*) + 1]";
|
|
|
1522 |
}
|
|
|
1523 |
return $columnpositionxpath;
|
|
|
1524 |
}
|
|
|
1525 |
|
|
|
1526 |
/**
|
|
|
1527 |
* Find a table row where each of the specified columns matches and throw exception if not found
|
|
|
1528 |
*
|
|
|
1529 |
* @param string $table table locator
|
|
|
1530 |
* @param array $cells key is the column locator (name or index such as '-1-') and value is the text contents of the table cell
|
|
|
1531 |
*/
|
|
|
1532 |
protected function ensure_table_row_exists(string $table, array $cells): void {
|
|
|
1533 |
$tablenode = $this->get_selected_node('table', $table);
|
|
|
1534 |
$tablexpath = $tablenode->getXpath();
|
|
|
1535 |
|
|
|
1536 |
$columnconditions = [];
|
|
|
1537 |
foreach ($cells as $columnname => $value) {
|
|
|
1538 |
$valueliteral = behat_context_helper::escape($value);
|
|
|
1539 |
$columnpositionxpath = $this->get_table_column_xpath($table, $columnname);
|
|
|
1540 |
$columnconditions[] = '.' . $columnpositionxpath . "[contains(normalize-space(.)," . $valueliteral . ")]";
|
|
|
1541 |
}
|
|
|
1542 |
$rowxpath = $tablexpath . "/tbody/tr[" . join(' and ', $columnconditions) . ']';
|
|
|
1543 |
|
|
|
1544 |
$rownode = $this->getSession()->getDriver()->find($rowxpath);
|
|
|
1545 |
if (empty($rownode)) {
|
|
|
1546 |
$rowlocator = array_map(fn($k) => "{$k} => {$cells[$k]}", array_keys($cells));
|
|
|
1547 |
throw new ElementNotFoundException($this->getSession(), "\n$rowxpath\n\n".'Table row', null, join(', ', $rowlocator));
|
|
|
1548 |
}
|
|
|
1549 |
}
|
|
|
1550 |
|
|
|
1551 |
/**
|
|
|
1552 |
* Find a table row where each of the specified columns matches and throw exception if found
|
|
|
1553 |
*
|
|
|
1554 |
* @param string $table table locator
|
|
|
1555 |
* @param array $cells key is the column locator (name or index such as '-1-') and value is the text contents of the table cell
|
|
|
1556 |
*/
|
|
|
1557 |
protected function ensure_table_row_does_not_exist(string $table, array $cells): void {
|
|
|
1558 |
try {
|
|
|
1559 |
$this->ensure_table_row_exists($table, $cells);
|
|
|
1560 |
// Throw exception if found.
|
|
|
1561 |
} catch (ElementNotFoundException $e) {
|
|
|
1562 |
// Table row/column doesn't contain this value. Nothing to do.
|
|
|
1563 |
return;
|
|
|
1564 |
}
|
|
|
1565 |
$rowlocator = array_map(fn($k) => "{$k} => {$cells[$k]}", array_keys($cells));
|
|
|
1566 |
throw new ExpectationException('Table row "' . join(', ', $rowlocator) .
|
|
|
1567 |
'" is present in the table "' . $table . '"', $this->getSession()
|
|
|
1568 |
);
|
|
|
1569 |
}
|
|
|
1570 |
|
|
|
1571 |
/**
|
1 |
efrain |
1572 |
* Checks that the provided value exist in table.
|
|
|
1573 |
*
|
|
|
1574 |
* First row may contain column headers or numeric indexes of the columns
|
|
|
1575 |
* (syntax -1- is also considered to be column index). Column indexes are
|
|
|
1576 |
* useful in case of multirow headers and/or presence of cells with colspan.
|
|
|
1577 |
*
|
|
|
1578 |
* @Then /^the following should exist in the "(?P<table_string>[^"]*)" table:$/
|
|
|
1579 |
* @throws ExpectationException
|
|
|
1580 |
* @param string $table name of table
|
|
|
1581 |
* @param TableNode $data table with first row as header and following values
|
|
|
1582 |
* | Header 1 | Header 2 | Header 3 |
|
|
|
1583 |
* | Value 1 | Value 2 | Value 3|
|
|
|
1584 |
*/
|
|
|
1585 |
public function following_should_exist_in_the_table($table, TableNode $data) {
|
|
|
1586 |
$datahash = $data->getHash();
|
11 |
efrain |
1587 |
if ($datahash && count($data->getRow(0)) != count($datahash[0])) {
|
|
|
1588 |
// Check that the number of columns in the hash is the same as the number of the columns in the first row.
|
|
|
1589 |
throw new coding_exception('Table contains duplicate column headers');
|
|
|
1590 |
}
|
1 |
efrain |
1591 |
|
|
|
1592 |
foreach ($datahash as $row) {
|
11 |
efrain |
1593 |
$this->ensure_table_row_exists($table, $row);
|
1 |
efrain |
1594 |
}
|
|
|
1595 |
}
|
|
|
1596 |
|
|
|
1597 |
/**
|
|
|
1598 |
* Checks that the provided values do not exist in a table.
|
|
|
1599 |
*
|
11 |
efrain |
1600 |
* If there are more than two columns, we check that NEITHER of the columns 2..n match
|
|
|
1601 |
* in the row where the first column matches
|
|
|
1602 |
*
|
1 |
efrain |
1603 |
* @Then /^the following should not exist in the "(?P<table_string>[^"]*)" table:$/
|
|
|
1604 |
* @throws ExpectationException
|
|
|
1605 |
* @param string $table name of table
|
|
|
1606 |
* @param TableNode $data table with first row as header and following values
|
|
|
1607 |
* | Header 1 | Header 2 | Header 3 |
|
|
|
1608 |
* | Value 1 | Value 2 | Value 3|
|
|
|
1609 |
*/
|
|
|
1610 |
public function following_should_not_exist_in_the_table($table, TableNode $data) {
|
|
|
1611 |
$datahash = $data->getHash();
|
11 |
efrain |
1612 |
if ($datahash && count($data->getRow(0)) != count($datahash[0])) {
|
|
|
1613 |
// Check that the number of columns in the hash is the same as the number of the columns in the first row.
|
|
|
1614 |
throw new coding_exception('Table contains duplicate column headers');
|
|
|
1615 |
}
|
1 |
efrain |
1616 |
|
|
|
1617 |
foreach ($datahash as $value) {
|
11 |
efrain |
1618 |
if (count($value) > 2) {
|
|
|
1619 |
// When there are more than two columns, what we really want to check is that for the rows
|
|
|
1620 |
// where the first column matches, NEITHER of the other columns match.
|
|
|
1621 |
$columns = array_keys($value);
|
|
|
1622 |
for ($i = 1; $i < count($columns); $i++) {
|
|
|
1623 |
$this->ensure_table_row_does_not_exist($table, [
|
|
|
1624 |
$columns[0] => $value[$columns[0]],
|
|
|
1625 |
$columns[$i] => $value[$columns[$i]],
|
|
|
1626 |
]);
|
|
|
1627 |
}
|
1 |
efrain |
1628 |
} else {
|
11 |
efrain |
1629 |
$this->ensure_table_row_does_not_exist($table, $value);
|
1 |
efrain |
1630 |
}
|
|
|
1631 |
}
|
|
|
1632 |
}
|
|
|
1633 |
|
|
|
1634 |
/**
|
|
|
1635 |
* Given the text of a link, download the linked file and return the contents.
|
|
|
1636 |
*
|
|
|
1637 |
* This is a helper method used by {@link following_should_download_bytes()}
|
|
|
1638 |
* and {@link following_should_download_between_and_bytes()}
|
|
|
1639 |
*
|
|
|
1640 |
* @param string $link the text of the link.
|
|
|
1641 |
* @return string the content of the downloaded file.
|
|
|
1642 |
*/
|
|
|
1643 |
public function download_file_from_link($link) {
|
|
|
1644 |
// Find the link.
|
|
|
1645 |
$linknode = $this->find_link($link);
|
|
|
1646 |
$this->ensure_node_is_visible($linknode);
|
|
|
1647 |
|
|
|
1648 |
// Get the href and check it.
|
|
|
1649 |
$url = $linknode->getAttribute('href');
|
|
|
1650 |
if (!$url) {
|
|
|
1651 |
throw new ExpectationException('Download link does not have href attribute',
|
|
|
1652 |
$this->getSession());
|
|
|
1653 |
}
|
|
|
1654 |
if (!preg_match('~^https?://~', $url)) {
|
|
|
1655 |
throw new ExpectationException('Download link not an absolute URL: ' . $url,
|
|
|
1656 |
$this->getSession());
|
|
|
1657 |
}
|
|
|
1658 |
|
|
|
1659 |
// Download the URL and check the size.
|
|
|
1660 |
$session = $this->getSession()->getCookie('MoodleSession');
|
|
|
1661 |
return download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
|
|
|
1662 |
}
|
|
|
1663 |
|
|
|
1664 |
/**
|
|
|
1665 |
* Downloads the file from a link on the page and checks the size.
|
|
|
1666 |
*
|
|
|
1667 |
* Only works if the link has an href attribute. Javascript downloads are
|
|
|
1668 |
* not supported. Currently, the href must be an absolute URL.
|
|
|
1669 |
*
|
|
|
1670 |
* @Then /^following "(?P<link_string>[^"]*)" should download "(?P<expected_bytes>\d+)" bytes$/
|
|
|
1671 |
* @throws ExpectationException
|
|
|
1672 |
* @param string $link the text of the link.
|
|
|
1673 |
* @param number $expectedsize the expected file size in bytes.
|
|
|
1674 |
*/
|
|
|
1675 |
public function following_should_download_bytes($link, $expectedsize) {
|
|
|
1676 |
$exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
|
|
|
1677 |
|
|
|
1678 |
// It will stop spinning once file is downloaded or time out.
|
|
|
1679 |
$result = $this->spin(
|
|
|
1680 |
function($context, $args) {
|
|
|
1681 |
$link = $args['link'];
|
|
|
1682 |
return $this->download_file_from_link($link);
|
|
|
1683 |
},
|
|
|
1684 |
array('link' => $link),
|
|
|
1685 |
behat_base::get_extended_timeout(),
|
|
|
1686 |
$exception
|
|
|
1687 |
);
|
|
|
1688 |
|
|
|
1689 |
// Check download size.
|
|
|
1690 |
$actualsize = (int)strlen($result);
|
|
|
1691 |
if ($actualsize !== (int)$expectedsize) {
|
|
|
1692 |
throw new ExpectationException('Downloaded data was ' . $actualsize .
|
|
|
1693 |
' bytes, expecting ' . $expectedsize, $this->getSession());
|
|
|
1694 |
}
|
|
|
1695 |
}
|
|
|
1696 |
|
|
|
1697 |
/**
|
|
|
1698 |
* Downloads the file from a link on the page and checks the size is in a given range.
|
|
|
1699 |
*
|
|
|
1700 |
* Only works if the link has an href attribute. Javascript downloads are
|
|
|
1701 |
* not supported. Currently, the href must be an absolute URL.
|
|
|
1702 |
*
|
|
|
1703 |
* The range includes the endpoints. That is, a 10 byte file in considered to
|
|
|
1704 |
* be between "5" and "10" bytes, and between "10" and "20" bytes.
|
|
|
1705 |
*
|
|
|
1706 |
* @Then /^following "(?P<link_string>[^"]*)" should download between "(?P<min_bytes>\d+)" and "(?P<max_bytes>\d+)" bytes$/
|
|
|
1707 |
* @throws ExpectationException
|
|
|
1708 |
* @param string $link the text of the link.
|
|
|
1709 |
* @param number $minexpectedsize the minimum expected file size in bytes.
|
|
|
1710 |
* @param number $maxexpectedsize the maximum expected file size in bytes.
|
|
|
1711 |
*/
|
|
|
1712 |
public function following_should_download_between_and_bytes($link, $minexpectedsize, $maxexpectedsize) {
|
|
|
1713 |
// If the minimum is greater than the maximum then swap the values.
|
|
|
1714 |
if ((int)$minexpectedsize > (int)$maxexpectedsize) {
|
|
|
1715 |
list($minexpectedsize, $maxexpectedsize) = array($maxexpectedsize, $minexpectedsize);
|
|
|
1716 |
}
|
|
|
1717 |
|
|
|
1718 |
$exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
|
|
|
1719 |
|
|
|
1720 |
// It will stop spinning once file is downloaded or time out.
|
|
|
1721 |
$result = $this->spin(
|
|
|
1722 |
function($context, $args) {
|
|
|
1723 |
$link = $args['link'];
|
|
|
1724 |
|
|
|
1725 |
return $this->download_file_from_link($link);
|
|
|
1726 |
},
|
|
|
1727 |
array('link' => $link),
|
|
|
1728 |
behat_base::get_extended_timeout(),
|
|
|
1729 |
$exception
|
|
|
1730 |
);
|
|
|
1731 |
|
|
|
1732 |
// Check download size.
|
|
|
1733 |
$actualsize = (int)strlen($result);
|
|
|
1734 |
if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) {
|
|
|
1735 |
throw new ExpectationException('Downloaded data was ' . $actualsize .
|
|
|
1736 |
' bytes, expecting between ' . $minexpectedsize . ' and ' .
|
|
|
1737 |
$maxexpectedsize, $this->getSession());
|
|
|
1738 |
}
|
|
|
1739 |
}
|
|
|
1740 |
|
|
|
1741 |
/**
|
|
|
1742 |
* Checks that the image on the page is the same as one of the fixture files
|
|
|
1743 |
*
|
|
|
1744 |
* @Then /^the image at "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be identical to "(?P<filepath_string>(?:[^"]|\\")*)"$/
|
|
|
1745 |
* @throws ExpectationException
|
|
|
1746 |
* @param string $element The locator of the image
|
|
|
1747 |
* @param string $selectortype The selector type
|
|
|
1748 |
* @param string $filepath path to the fixture file
|
|
|
1749 |
*/
|
|
|
1750 |
public function the_image_at_should_be_identical_to($element, $selectortype, $filepath) {
|
|
|
1751 |
global $CFG;
|
|
|
1752 |
|
|
|
1753 |
// Get the container node (exception if it doesn't exist).
|
|
|
1754 |
$containernode = $this->get_selected_node($selectortype, $element);
|
|
|
1755 |
$url = $containernode->getAttribute('src');
|
|
|
1756 |
if ($url == null) {
|
|
|
1757 |
throw new ExpectationException('Element does not have src attribute',
|
|
|
1758 |
$this->getSession());
|
|
|
1759 |
}
|
|
|
1760 |
$session = $this->getSession()->getCookie('MoodleSession');
|
|
|
1761 |
$content = download_file_content($url, array('Cookie' => 'MoodleSession=' . $session));
|
|
|
1762 |
|
|
|
1763 |
// Get the content of the fixture file.
|
|
|
1764 |
// Replace 'admin/' if it is in start of path with $CFG->admin .
|
|
|
1765 |
if (substr($filepath, 0, 6) === 'admin/') {
|
|
|
1766 |
$filepath = $CFG->admin . DIRECTORY_SEPARATOR . substr($filepath, 6);
|
|
|
1767 |
}
|
|
|
1768 |
$filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
|
|
|
1769 |
$filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
|
|
|
1770 |
if (!is_readable($filepath)) {
|
|
|
1771 |
throw new ExpectationException('The file to compare to does not exist.', $this->getSession());
|
|
|
1772 |
}
|
|
|
1773 |
$expectedcontent = file_get_contents($filepath);
|
|
|
1774 |
|
|
|
1775 |
if ($content !== $expectedcontent) {
|
|
|
1776 |
throw new ExpectationException('Image is not identical to the fixture. Received ' .
|
|
|
1777 |
strlen($content) . ' bytes and expected ' . strlen($expectedcontent) . ' bytes', $this->getSession());
|
|
|
1778 |
}
|
|
|
1779 |
}
|
|
|
1780 |
|
|
|
1781 |
/**
|
|
|
1782 |
* Prepare to detect whether or not a new page has loaded (or the same page reloaded) some time in the future.
|
|
|
1783 |
*
|
|
|
1784 |
* @Given /^I start watching to see if a new page loads$/
|
|
|
1785 |
*/
|
|
|
1786 |
public function i_start_watching_to_see_if_a_new_page_loads() {
|
|
|
1787 |
if (!$this->running_javascript()) {
|
|
|
1788 |
throw new DriverException('Page load detection requires JavaScript.');
|
|
|
1789 |
}
|
|
|
1790 |
|
|
|
1791 |
$session = $this->getSession();
|
|
|
1792 |
|
|
|
1793 |
if ($this->pageloaddetectionrunning || $session->getPage()->find('xpath', $this->get_page_load_xpath())) {
|
|
|
1794 |
// If we find this node at this point we are already watching for a reload and the behat steps
|
|
|
1795 |
// are out of order. We will treat this as an error - really it needs to be fixed as it indicates a problem.
|
|
|
1796 |
throw new ExpectationException(
|
|
|
1797 |
'Page load expectation error: page reloads are already been watched for.', $session);
|
|
|
1798 |
}
|
|
|
1799 |
|
|
|
1800 |
$this->pageloaddetectionrunning = true;
|
|
|
1801 |
|
|
|
1802 |
$this->execute_script(
|
|
|
1803 |
'var span = document.createElement("span");
|
|
|
1804 |
span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '");
|
|
|
1805 |
span.setAttribute("style", "display: none;");
|
|
|
1806 |
document.body.appendChild(span);'
|
|
|
1807 |
);
|
|
|
1808 |
}
|
|
|
1809 |
|
|
|
1810 |
/**
|
|
|
1811 |
* Verify that a new page has loaded (or the same page has reloaded) since the
|
|
|
1812 |
* last "I start watching to see if a new page loads" step.
|
|
|
1813 |
*
|
|
|
1814 |
* @Given /^a new page should have loaded since I started watching$/
|
|
|
1815 |
*/
|
|
|
1816 |
public function a_new_page_should_have_loaded_since_i_started_watching() {
|
|
|
1817 |
$session = $this->getSession();
|
|
|
1818 |
|
|
|
1819 |
// Make sure page load tracking was started.
|
|
|
1820 |
if (!$this->pageloaddetectionrunning) {
|
|
|
1821 |
throw new ExpectationException(
|
|
|
1822 |
'Page load expectation error: page load tracking was not started.', $session);
|
|
|
1823 |
}
|
|
|
1824 |
|
|
|
1825 |
// As the node is inserted by code above it is either there or not, and we do not need spin and it is safe
|
|
|
1826 |
// to use the native API here which is great as exception handling (the alternative is slow).
|
|
|
1827 |
if ($session->getPage()->find('xpath', $this->get_page_load_xpath())) {
|
|
|
1828 |
// We don't want to find this node, if we do we have an error.
|
|
|
1829 |
throw new ExpectationException(
|
|
|
1830 |
'Page load expectation error: a new page has not been loaded when it should have been.', $session);
|
|
|
1831 |
}
|
|
|
1832 |
|
|
|
1833 |
// Cancel the tracking of pageloaddetectionrunning.
|
|
|
1834 |
$this->pageloaddetectionrunning = false;
|
|
|
1835 |
}
|
|
|
1836 |
|
|
|
1837 |
/**
|
|
|
1838 |
* Verify that a new page has not loaded (or the same page has reloaded) since the
|
|
|
1839 |
* last "I start watching to see if a new page loads" step.
|
|
|
1840 |
*
|
|
|
1841 |
* @Given /^a new page should not have loaded since I started watching$/
|
|
|
1842 |
*/
|
|
|
1843 |
public function a_new_page_should_not_have_loaded_since_i_started_watching() {
|
|
|
1844 |
$session = $this->getSession();
|
|
|
1845 |
|
|
|
1846 |
// Make sure page load tracking was started.
|
|
|
1847 |
if (!$this->pageloaddetectionrunning) {
|
|
|
1848 |
throw new ExpectationException(
|
|
|
1849 |
'Page load expectation error: page load tracking was not started.', $session);
|
|
|
1850 |
}
|
|
|
1851 |
|
|
|
1852 |
// We use our API here as we can use the exception handling provided by it.
|
|
|
1853 |
$this->find(
|
|
|
1854 |
'xpath',
|
|
|
1855 |
$this->get_page_load_xpath(),
|
|
|
1856 |
new ExpectationException(
|
|
|
1857 |
'Page load expectation error: A new page has been loaded when it should not have been.',
|
|
|
1858 |
$this->getSession()
|
|
|
1859 |
)
|
|
|
1860 |
);
|
|
|
1861 |
}
|
|
|
1862 |
|
|
|
1863 |
/**
|
|
|
1864 |
* Helper used by {@link a_new_page_should_have_loaded_since_i_started_watching}
|
|
|
1865 |
* and {@link a_new_page_should_not_have_loaded_since_i_started_watching}
|
|
|
1866 |
* @return string xpath expression.
|
|
|
1867 |
*/
|
|
|
1868 |
protected function get_page_load_xpath() {
|
|
|
1869 |
return "//span[@data-rel = '" . self::PAGE_LOAD_DETECTION_STRING . "']";
|
|
|
1870 |
}
|
|
|
1871 |
|
|
|
1872 |
/**
|
|
|
1873 |
* Wait unit user press Enter/Return key. Useful when debugging a scenario.
|
|
|
1874 |
*
|
|
|
1875 |
* @Then /^(?:|I )pause(?:| scenario execution)$/
|
|
|
1876 |
*/
|
|
|
1877 |
public function i_pause_scenario_execution() {
|
|
|
1878 |
$message = "<colour:lightYellow>Paused. Press <colour:lightRed>Enter/Return<colour:lightYellow> to continue.";
|
|
|
1879 |
behat_util::pause($this->getSession(), $message);
|
|
|
1880 |
}
|
|
|
1881 |
|
|
|
1882 |
/**
|
|
|
1883 |
* Presses a given button in the browser.
|
|
|
1884 |
* NOTE: Phantomjs and browserkit driver reloads page while navigating back and forward.
|
|
|
1885 |
*
|
|
|
1886 |
* @Then /^I press the "(back|forward|reload)" button in the browser$/
|
|
|
1887 |
* @param string $button the button to press.
|
|
|
1888 |
* @throws ExpectationException
|
|
|
1889 |
*/
|
|
|
1890 |
public function i_press_in_the_browser($button) {
|
|
|
1891 |
$session = $this->getSession();
|
|
|
1892 |
|
|
|
1893 |
if ($button == 'back') {
|
|
|
1894 |
$session->back();
|
|
|
1895 |
} else if ($button == 'forward') {
|
|
|
1896 |
$session->forward();
|
|
|
1897 |
} else if ($button == 'reload') {
|
|
|
1898 |
$session->reload();
|
|
|
1899 |
} else {
|
|
|
1900 |
throw new ExpectationException('Unknown browser button.', $session);
|
|
|
1901 |
}
|
|
|
1902 |
}
|
|
|
1903 |
|
|
|
1904 |
/**
|
|
|
1905 |
* Send key presses to the browser without first changing focusing, or applying the key presses to a specific
|
|
|
1906 |
* element.
|
|
|
1907 |
*
|
|
|
1908 |
* Example usage of this step:
|
|
|
1909 |
* When I type "Penguin"
|
|
|
1910 |
*
|
|
|
1911 |
* @When I type :keys
|
|
|
1912 |
* @param string $keys The key, or list of keys, to type
|
|
|
1913 |
*/
|
|
|
1914 |
public function i_type(string $keys): void {
|
|
|
1915 |
// Certain keys, such as the newline character, must be converted to the appropriate character code.
|
|
|
1916 |
// Without this, keys will behave differently depending on the browser.
|
|
|
1917 |
$keylist = array_map(function($key): string {
|
|
|
1918 |
switch ($key) {
|
|
|
1919 |
case "\n":
|
|
|
1920 |
return behat_keys::ENTER;
|
|
|
1921 |
default:
|
|
|
1922 |
return $key;
|
|
|
1923 |
}
|
|
|
1924 |
}, str_split($keys));
|
|
|
1925 |
behat_base::type_keys($this->getSession(), $keylist);
|
|
|
1926 |
}
|
|
|
1927 |
|
|
|
1928 |
/**
|
|
|
1929 |
* Press a named or character key with an optional set of modifiers.
|
|
|
1930 |
*
|
|
|
1931 |
* Supported named keys are:
|
|
|
1932 |
* - up
|
|
|
1933 |
* - down
|
|
|
1934 |
* - left
|
|
|
1935 |
* - right
|
|
|
1936 |
* - pageup|page_up
|
|
|
1937 |
* - pagedown|page_down
|
|
|
1938 |
* - home
|
|
|
1939 |
* - end
|
|
|
1940 |
* - insert
|
|
|
1941 |
* - delete
|
|
|
1942 |
* - backspace
|
|
|
1943 |
* - escape
|
|
|
1944 |
* - enter
|
|
|
1945 |
* - tab
|
|
|
1946 |
*
|
|
|
1947 |
* You can also use a single character for the key name e.g. 'Ctrl C'.
|
|
|
1948 |
*
|
|
|
1949 |
* Supported moderators are:
|
|
|
1950 |
* - shift
|
|
|
1951 |
* - ctrl
|
|
|
1952 |
* - alt
|
|
|
1953 |
* - meta
|
|
|
1954 |
*
|
|
|
1955 |
* Example usage of this new step:
|
|
|
1956 |
* When I press the up key
|
|
|
1957 |
* When I press the space key
|
|
|
1958 |
* When I press the shift tab key
|
|
|
1959 |
*
|
|
|
1960 |
* Multiple moderator keys can be combined using the '+' operator, for example:
|
|
|
1961 |
* When I press the ctrl+shift enter key
|
|
|
1962 |
* When I press the ctrl + shift enter key
|
|
|
1963 |
*
|
|
|
1964 |
* @When /^I press the (?P<modifiers_string>.* )?(?P<key_string>.*) key$/
|
|
|
1965 |
* @param string $modifiers A list of keyboard modifiers, separated by the `+` character
|
|
|
1966 |
* @param string $key The name of the key to press
|
|
|
1967 |
*/
|
|
|
1968 |
public function i_press_named_key(string $modifiers, string $key): void {
|
|
|
1969 |
behat_base::require_javascript_in_session($this->getSession());
|
|
|
1970 |
|
|
|
1971 |
$keys = [];
|
|
|
1972 |
|
|
|
1973 |
foreach (explode('+', $modifiers) as $modifier) {
|
|
|
1974 |
switch (strtoupper(trim($modifier))) {
|
|
|
1975 |
case '':
|
|
|
1976 |
break;
|
|
|
1977 |
case 'SHIFT':
|
|
|
1978 |
$keys[] = behat_keys::SHIFT;
|
|
|
1979 |
break;
|
|
|
1980 |
case 'CTRL':
|
|
|
1981 |
$keys[] = behat_keys::CONTROL;
|
|
|
1982 |
break;
|
|
|
1983 |
case 'ALT':
|
|
|
1984 |
$keys[] = behat_keys::ALT;
|
|
|
1985 |
break;
|
|
|
1986 |
case 'META':
|
|
|
1987 |
$keys[] = behat_keys::META;
|
|
|
1988 |
break;
|
|
|
1989 |
default:
|
|
|
1990 |
throw new \coding_exception("Unknown modifier key '$modifier'}");
|
|
|
1991 |
}
|
|
|
1992 |
}
|
|
|
1993 |
|
|
|
1994 |
$modifier = trim($key);
|
|
|
1995 |
switch (strtoupper($key)) {
|
|
|
1996 |
case 'UP':
|
|
|
1997 |
$keys[] = behat_keys::ARROW_UP;
|
|
|
1998 |
break;
|
|
|
1999 |
case 'DOWN':
|
|
|
2000 |
$keys[] = behat_keys::ARROW_DOWN;
|
|
|
2001 |
break;
|
|
|
2002 |
case 'LEFT':
|
|
|
2003 |
$keys[] = behat_keys::ARROW_LEFT;
|
|
|
2004 |
break;
|
|
|
2005 |
case 'RIGHT':
|
|
|
2006 |
$keys[] = behat_keys::ARROW_RIGHT;
|
|
|
2007 |
break;
|
|
|
2008 |
case 'HOME':
|
|
|
2009 |
$keys[] = behat_keys::HOME;
|
|
|
2010 |
break;
|
|
|
2011 |
case 'END':
|
|
|
2012 |
$keys[] = behat_keys::END;
|
|
|
2013 |
break;
|
|
|
2014 |
case 'INSERT':
|
|
|
2015 |
$keys[] = behat_keys::INSERT;
|
|
|
2016 |
break;
|
|
|
2017 |
case 'BACKSPACE':
|
|
|
2018 |
$keys[] = behat_keys::BACKSPACE;
|
|
|
2019 |
break;
|
|
|
2020 |
case 'DELETE':
|
|
|
2021 |
$keys[] = behat_keys::DELETE;
|
|
|
2022 |
break;
|
|
|
2023 |
case 'PAGEUP':
|
|
|
2024 |
case 'PAGE_UP':
|
|
|
2025 |
$keys[] = behat_keys::PAGE_UP;
|
|
|
2026 |
break;
|
|
|
2027 |
case 'PAGEDOWN':
|
|
|
2028 |
case 'PAGE_DOWN':
|
|
|
2029 |
$keys[] = behat_keys::PAGE_DOWN;
|
|
|
2030 |
break;
|
|
|
2031 |
case 'ESCAPE':
|
|
|
2032 |
$keys[] = behat_keys::ESCAPE;
|
|
|
2033 |
break;
|
|
|
2034 |
case 'ENTER':
|
|
|
2035 |
$keys[] = behat_keys::ENTER;
|
|
|
2036 |
break;
|
|
|
2037 |
case 'TAB':
|
|
|
2038 |
$keys[] = behat_keys::TAB;
|
|
|
2039 |
break;
|
|
|
2040 |
case 'SPACE':
|
|
|
2041 |
$keys[] = behat_keys::SPACE;
|
|
|
2042 |
break;
|
|
|
2043 |
case 'MULTIPLY':
|
|
|
2044 |
$keys[] = behat_keys::MULTIPLY;
|
|
|
2045 |
break;
|
|
|
2046 |
default:
|
|
|
2047 |
// You can enter a single ASCII character (e.g. a letter) to directly type that key.
|
|
|
2048 |
if (strlen($key) === 1) {
|
|
|
2049 |
$keys[] = strtolower($key);
|
|
|
2050 |
} else {
|
|
|
2051 |
throw new \coding_exception("Unknown key '$key'}");
|
|
|
2052 |
}
|
|
|
2053 |
}
|
|
|
2054 |
|
|
|
2055 |
behat_base::type_keys($this->getSession(), $keys);
|
|
|
2056 |
}
|
|
|
2057 |
|
|
|
2058 |
/**
|
|
|
2059 |
* Trigger a keydown event for a key on a specific element.
|
|
|
2060 |
*
|
|
|
2061 |
* @When /^I press key "(?P<key_string>(?:[^"]|\\")*)" in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
|
2062 |
* @param string $key either char-code or character itself,
|
|
|
2063 |
* may optionally be prefixed with ctrl-, alt-, shift- or meta-
|
|
|
2064 |
* @param string $element Element we look for
|
|
|
2065 |
* @param string $selectortype The type of what we look for
|
|
|
2066 |
* @throws DriverException
|
|
|
2067 |
* @throws ExpectationException
|
|
|
2068 |
*/
|
|
|
2069 |
public function i_press_key_in_element($key, $element, $selectortype) {
|
|
|
2070 |
if (!$this->running_javascript()) {
|
|
|
2071 |
throw new DriverException('Key down step is not available with Javascript disabled');
|
|
|
2072 |
}
|
|
|
2073 |
// Gets the node based on the requested selector type and locator.
|
|
|
2074 |
$node = $this->get_selected_node($selectortype, $element);
|
|
|
2075 |
$modifier = null;
|
|
|
2076 |
$validmodifiers = array('ctrl', 'alt', 'shift', 'meta');
|
|
|
2077 |
$char = $key;
|
|
|
2078 |
if (strpos($key, '-')) {
|
|
|
2079 |
list($modifier, $char) = preg_split('/-/', $key, 2);
|
|
|
2080 |
$modifier = strtolower($modifier);
|
|
|
2081 |
if (!in_array($modifier, $validmodifiers)) {
|
|
|
2082 |
throw new ExpectationException(sprintf('Unknown key modifier: %s.', $modifier),
|
|
|
2083 |
$this->getSession());
|
|
|
2084 |
}
|
|
|
2085 |
}
|
|
|
2086 |
if (is_numeric($char)) {
|
|
|
2087 |
$char = (int)$char;
|
|
|
2088 |
}
|
|
|
2089 |
|
|
|
2090 |
$node->keyDown($char, $modifier);
|
|
|
2091 |
$node->keyPress($char, $modifier);
|
|
|
2092 |
$node->keyUp($char, $modifier);
|
|
|
2093 |
}
|
|
|
2094 |
|
|
|
2095 |
/**
|
|
|
2096 |
* Press tab key on a specific element.
|
|
|
2097 |
*
|
|
|
2098 |
* @When /^I press tab key in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
|
2099 |
* @param string $element Element we look for
|
|
|
2100 |
* @param string $selectortype The type of what we look for
|
|
|
2101 |
* @throws DriverException
|
|
|
2102 |
* @throws ExpectationException
|
|
|
2103 |
*/
|
|
|
2104 |
public function i_post_tab_key_in_element($element, $selectortype) {
|
|
|
2105 |
if (!$this->running_javascript()) {
|
|
|
2106 |
throw new DriverException('Tab press step is not available with Javascript disabled');
|
|
|
2107 |
}
|
|
|
2108 |
// Gets the node based on the requested selector type and locator.
|
|
|
2109 |
$node = $this->get_selected_node($selectortype, $element);
|
|
|
2110 |
$this->execute('behat_general::i_click_on', [$node, 'NodeElement']);
|
|
|
2111 |
$this->execute('behat_general::i_press_named_key', ['', 'tab']);
|
|
|
2112 |
}
|
|
|
2113 |
|
|
|
2114 |
/**
|
|
|
2115 |
* Checks if database family used is using one of the specified, else skip. (mysql, postgres, mssql, oracle, etc.)
|
|
|
2116 |
*
|
|
|
2117 |
* @Given /^database family used is one of the following:$/
|
|
|
2118 |
* @param TableNode $databasefamilies list of database.
|
|
|
2119 |
* @return void.
|
|
|
2120 |
* @throws \Moodle\BehatExtension\Exception\SkippedException
|
|
|
2121 |
*/
|
|
|
2122 |
public function database_family_used_is_one_of_the_following(TableNode $databasefamilies) {
|
|
|
2123 |
global $DB;
|
|
|
2124 |
|
|
|
2125 |
$dbfamily = $DB->get_dbfamily();
|
|
|
2126 |
|
|
|
2127 |
// Check if used db family is one of the specified ones. If yes then return.
|
|
|
2128 |
foreach ($databasefamilies->getRows() as $dbfamilytocheck) {
|
|
|
2129 |
if ($dbfamilytocheck[0] == $dbfamily) {
|
|
|
2130 |
return;
|
|
|
2131 |
}
|
|
|
2132 |
}
|
|
|
2133 |
|
|
|
2134 |
throw new \Moodle\BehatExtension\Exception\SkippedException();
|
|
|
2135 |
}
|
|
|
2136 |
|
|
|
2137 |
/**
|
|
|
2138 |
* Checks if given plugin is installed, and skips the current scenario if not.
|
|
|
2139 |
*
|
|
|
2140 |
* @Given the :plugin plugin is installed
|
|
|
2141 |
* @param string $plugin frankenstyle plugin name, e.g. 'filter_embedquestion'.
|
|
|
2142 |
* @throws \Moodle\BehatExtension\Exception\SkippedException
|
|
|
2143 |
*/
|
|
|
2144 |
public function plugin_is_installed(string $plugin): void {
|
|
|
2145 |
$path = core_component::get_component_directory($plugin);
|
|
|
2146 |
if (!is_readable($path . '/version.php')) {
|
|
|
2147 |
throw new \Moodle\BehatExtension\Exception\SkippedException(
|
|
|
2148 |
'Skipping this scenario because the ' . $plugin . ' is not installed.');
|
|
|
2149 |
}
|
|
|
2150 |
}
|
|
|
2151 |
|
|
|
2152 |
/**
|
|
|
2153 |
* Checks focus is with the given element.
|
|
|
2154 |
*
|
|
|
2155 |
* @Then /^the focused element is( not)? "(?P<node_string>(?:[^"]|\\")*)" "(?P<node_selector_string>[^"]*)"$/
|
|
|
2156 |
* @param string $not optional step verifier
|
|
|
2157 |
* @param string $nodeelement Element identifier
|
|
|
2158 |
* @param string $nodeselectortype Element type
|
|
|
2159 |
* @throws DriverException If not using JavaScript
|
|
|
2160 |
* @throws ExpectationException
|
|
|
2161 |
*/
|
|
|
2162 |
public function the_focused_element_is($not, $nodeelement, $nodeselectortype) {
|
|
|
2163 |
if (!$this->running_javascript()) {
|
|
|
2164 |
throw new DriverException('Checking focus on an element requires JavaScript');
|
|
|
2165 |
}
|
|
|
2166 |
|
|
|
2167 |
$element = $this->find($nodeselectortype, $nodeelement);
|
|
|
2168 |
$xpath = addslashes_js($element->getXpath());
|
|
|
2169 |
$script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
|
|
|
2170 |
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
|
|
|
2171 |
$targetisfocused = $this->evaluate_script($script);
|
|
|
2172 |
if ($not == ' not') {
|
|
|
2173 |
if ($targetisfocused) {
|
|
|
2174 |
throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
|
|
|
2175 |
}
|
|
|
2176 |
} else {
|
|
|
2177 |
if (!$targetisfocused) {
|
|
|
2178 |
throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
|
|
|
2179 |
}
|
|
|
2180 |
}
|
|
|
2181 |
}
|
|
|
2182 |
|
|
|
2183 |
/**
|
|
|
2184 |
* Checks focus is with the given element.
|
|
|
2185 |
*
|
|
|
2186 |
* @Then /^the focused element is( not)? "(?P<n>(?:[^"]|\\")*)" "(?P<ns>[^"]*)" in the "(?P<c>(?:[^"]|\\")*)" "(?P<cs>[^"]*)"$/
|
|
|
2187 |
* @param string $not string optional step verifier
|
|
|
2188 |
* @param string $element Element identifier
|
|
|
2189 |
* @param string $selectortype Element type
|
|
|
2190 |
* @param string $nodeelement Element we look in
|
|
|
2191 |
* @param string $nodeselectortype The type of selector where we look in
|
|
|
2192 |
* @throws DriverException If not using JavaScript
|
|
|
2193 |
* @throws ExpectationException
|
|
|
2194 |
*/
|
|
|
2195 |
public function the_focused_element_is_in_the($not, $element, $selectortype, $nodeelement, $nodeselectortype) {
|
|
|
2196 |
if (!$this->running_javascript()) {
|
|
|
2197 |
throw new DriverException('Checking focus on an element requires JavaScript');
|
|
|
2198 |
}
|
|
|
2199 |
$element = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
|
|
|
2200 |
$xpath = addslashes_js($element->getXpath());
|
|
|
2201 |
$script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
|
|
|
2202 |
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
|
|
|
2203 |
$targetisfocused = $this->evaluate_script($script);
|
|
|
2204 |
if ($not == ' not') {
|
|
|
2205 |
if ($targetisfocused) {
|
|
|
2206 |
throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
|
|
|
2207 |
}
|
|
|
2208 |
} else {
|
|
|
2209 |
if (!$targetisfocused) {
|
|
|
2210 |
throw new ExpectationException("$nodeelement $nodeselectortype is not focused", $this->getSession());
|
|
|
2211 |
}
|
|
|
2212 |
}
|
|
|
2213 |
}
|
|
|
2214 |
|
|
|
2215 |
/**
|
|
|
2216 |
* Manually press tab key.
|
|
|
2217 |
*
|
|
|
2218 |
* @When /^I press( shift)? tab$/
|
|
|
2219 |
* @param string $shift string optional step verifier
|
|
|
2220 |
* @throws DriverException
|
|
|
2221 |
*/
|
|
|
2222 |
public function i_manually_press_tab($shift = '') {
|
|
|
2223 |
if (empty($shift)) {
|
|
|
2224 |
$this->execute('behat_general::i_press_named_key', ['', 'tab']);
|
|
|
2225 |
} else {
|
|
|
2226 |
$this->execute('behat_general::i_press_named_key', ['shift', 'tab']);
|
|
|
2227 |
}
|
|
|
2228 |
}
|
|
|
2229 |
|
|
|
2230 |
/**
|
|
|
2231 |
* Trigger click on node via javascript instead of actually clicking on it via pointer.
|
|
|
2232 |
* This function resolves the issue of nested elements.
|
|
|
2233 |
*
|
|
|
2234 |
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" skipping visibility check$/
|
|
|
2235 |
* @param string $element
|
|
|
2236 |
* @param string $selectortype
|
|
|
2237 |
*/
|
|
|
2238 |
public function i_click_on_skipping_visibility_check($element, $selectortype) {
|
|
|
2239 |
|
|
|
2240 |
// Gets the node based on the requested selector type and locator.
|
|
|
2241 |
$node = $this->get_selected_node($selectortype, $element);
|
|
|
2242 |
$this->js_trigger_click($node);
|
|
|
2243 |
}
|
|
|
2244 |
|
|
|
2245 |
/**
|
|
|
2246 |
* Checks, that the specified element contains the specified text a certain amount of times.
|
|
|
2247 |
* When running Javascript tests it also considers that texts may be hidden.
|
|
|
2248 |
*
|
|
|
2249 |
* @Then /^I should see "(?P<elementscount_number>\d+)" occurrences of "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
|
2250 |
* @throws ElementNotFoundException
|
|
|
2251 |
* @throws ExpectationException
|
|
|
2252 |
* @param int $elementscount How many occurrences of the element we look for.
|
|
|
2253 |
* @param string $text
|
|
|
2254 |
* @param string $element Element we look in.
|
|
|
2255 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
2256 |
*/
|
|
|
2257 |
public function i_should_see_occurrences_of_in_element($elementscount, $text, $element, $selectortype) {
|
|
|
2258 |
|
|
|
2259 |
// Getting the container where the text should be found.
|
|
|
2260 |
$container = $this->get_selected_node($selectortype, $element);
|
|
|
2261 |
|
|
|
2262 |
// Looking for all the matching nodes without any other descendant matching the
|
|
|
2263 |
// same xpath (we are using contains(., ....).
|
|
|
2264 |
$xpathliteral = behat_context_helper::escape($text);
|
|
|
2265 |
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
|
|
|
2266 |
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
|
|
|
2267 |
|
|
|
2268 |
$nodes = $this->find_all('xpath', $xpath, false, $container);
|
|
|
2269 |
|
|
|
2270 |
if ($this->running_javascript()) {
|
|
|
2271 |
$nodes = array_filter($nodes, function($node) {
|
|
|
2272 |
return $node->isVisible();
|
|
|
2273 |
});
|
|
|
2274 |
}
|
|
|
2275 |
|
|
|
2276 |
if ($elementscount != count($nodes)) {
|
|
|
2277 |
throw new ExpectationException('Found '.count($nodes).' elements in column. Expected '.$elementscount,
|
|
|
2278 |
$this->getSession());
|
|
|
2279 |
}
|
|
|
2280 |
}
|
|
|
2281 |
|
|
|
2282 |
/**
|
|
|
2283 |
* Checks, that the specified element contains the specified node type a certain amount of times.
|
|
|
2284 |
* When running Javascript tests it also considers that texts may be hidden.
|
|
|
2285 |
*
|
|
|
2286 |
* @Then /^I should see "(?P<elementscount_number>\d+)" node occurrences of type "(?P<node_type>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
|
2287 |
* @throws ElementNotFoundException
|
|
|
2288 |
* @throws ExpectationException
|
|
|
2289 |
* @param int $elementscount How many occurrences of the element we look for.
|
|
|
2290 |
* @param string $nodetype
|
|
|
2291 |
* @param string $element Element we look in.
|
|
|
2292 |
* @param string $selectortype The type of element where we are looking in.
|
|
|
2293 |
*/
|
|
|
2294 |
public function i_should_see_node_occurrences_of_type_in_element(int $elementscount, string $nodetype, string $element, string $selectortype) {
|
|
|
2295 |
|
|
|
2296 |
// Getting the container where the text should be found.
|
|
|
2297 |
$container = $this->get_selected_node($selectortype, $element);
|
|
|
2298 |
|
|
|
2299 |
$xpath = "/descendant-or-self::$nodetype [count(descendant::$nodetype) = 0]";
|
|
|
2300 |
|
|
|
2301 |
$nodes = $this->find_all('xpath', $xpath, false, $container);
|
|
|
2302 |
|
|
|
2303 |
if ($this->running_javascript()) {
|
|
|
2304 |
$nodes = array_filter($nodes, function($node) {
|
|
|
2305 |
return $node->isVisible();
|
|
|
2306 |
});
|
|
|
2307 |
}
|
|
|
2308 |
|
|
|
2309 |
if ($elementscount != count($nodes)) {
|
|
|
2310 |
throw new ExpectationException('Found '.count($nodes).' elements in column. Expected '.$elementscount,
|
|
|
2311 |
$this->getSession());
|
|
|
2312 |
}
|
|
|
2313 |
}
|
|
|
2314 |
|
|
|
2315 |
/**
|
|
|
2316 |
* Manually press enter key.
|
|
|
2317 |
*
|
|
|
2318 |
* @When /^I press enter/
|
|
|
2319 |
* @throws DriverException
|
|
|
2320 |
*/
|
|
|
2321 |
public function i_manually_press_enter() {
|
|
|
2322 |
$this->execute('behat_general::i_press_named_key', ['', 'enter']);
|
|
|
2323 |
}
|
|
|
2324 |
|
|
|
2325 |
/**
|
|
|
2326 |
* Visit a local URL relative to the behat root.
|
|
|
2327 |
*
|
|
|
2328 |
* @When I visit :localurl
|
|
|
2329 |
*
|
|
|
2330 |
* @param string|moodle_url $localurl The URL relative to the behat_wwwroot to visit.
|
|
|
2331 |
*/
|
|
|
2332 |
public function i_visit($localurl): void {
|
|
|
2333 |
$localurl = new moodle_url($localurl);
|
|
|
2334 |
$this->getSession()->visit($this->locate_path($localurl->out_as_local_url(false)));
|
|
|
2335 |
}
|
|
|
2336 |
|
|
|
2337 |
/**
|
|
|
2338 |
* Increase the webdriver timeouts.
|
|
|
2339 |
*
|
|
|
2340 |
* This should be reset between scenarios, or can be called again to decrease the timeouts.
|
|
|
2341 |
*
|
|
|
2342 |
* @Given I mark this test as slow setting a timeout factor of :factor
|
|
|
2343 |
*/
|
|
|
2344 |
public function i_mark_this_test_as_long_running(int $factor = 2): void {
|
|
|
2345 |
$this->set_test_timeout_factor($factor);
|
|
|
2346 |
}
|
|
|
2347 |
|
|
|
2348 |
/**
|
|
|
2349 |
* Click on a dynamic tab to load its content
|
|
|
2350 |
*
|
|
|
2351 |
* @Given /^I click on the "(?P<tab_string>(?:[^"]|\\")*)" dynamic tab$/
|
|
|
2352 |
*
|
|
|
2353 |
* @param string $tabname
|
|
|
2354 |
*/
|
|
|
2355 |
public function i_click_on_the_dynamic_tab(string $tabname): void {
|
|
|
2356 |
$xpath = "//*[@id='dynamictabs-tabs'][descendant::a[contains(text(), '" . $this->escape($tabname) . "')]]";
|
|
|
2357 |
$this->execute('behat_general::i_click_on_in_the',
|
|
|
2358 |
[$tabname, 'link', $xpath, 'xpath_element']);
|
|
|
2359 |
}
|
|
|
2360 |
|
|
|
2361 |
/**
|
|
|
2362 |
* Enable an specific plugin.
|
|
|
2363 |
*
|
|
|
2364 |
* @When /^I enable "(?P<plugin_string>(?:[^"]|\\")*)" "(?P<plugintype_string>[^"]*)" plugin$/
|
|
|
2365 |
* @param string $plugin Plugin we look for
|
|
|
2366 |
* @param string $plugintype The type of the plugin
|
|
|
2367 |
*/
|
|
|
2368 |
public function i_enable_plugin($plugin, $plugintype) {
|
|
|
2369 |
$class = core_plugin_manager::resolve_plugininfo_class($plugintype);
|
|
|
2370 |
$class::enable_plugin($plugin, true);
|
|
|
2371 |
}
|
|
|
2372 |
|
|
|
2373 |
/**
|
|
|
2374 |
* Set the default text editor to the named text editor.
|
|
|
2375 |
*
|
|
|
2376 |
* @Given the default editor is set to :editor
|
|
|
2377 |
* @param string $editor
|
|
|
2378 |
* @throws ExpectationException If the specified editor is not available.
|
|
|
2379 |
*/
|
|
|
2380 |
public function the_default_editor_is_set_to(string $editor): void {
|
|
|
2381 |
global $CFG;
|
|
|
2382 |
|
|
|
2383 |
// Check if the provided editor is available.
|
|
|
2384 |
if (!array_key_exists($editor, editors_get_available())) {
|
|
|
2385 |
throw new ExpectationException(
|
|
|
2386 |
"Unable to set the editor to {$editor} as it is not installed. The available editors are: " .
|
|
|
2387 |
implode(', ', array_keys(editors_get_available())),
|
|
|
2388 |
$this->getSession()
|
|
|
2389 |
);
|
|
|
2390 |
}
|
|
|
2391 |
|
|
|
2392 |
// Make the provided editor the default one in $CFG->texteditors by
|
|
|
2393 |
// moving it to the first [editor],atto,tiny,textarea on the list.
|
|
|
2394 |
$list = explode(',', $CFG->texteditors);
|
|
|
2395 |
array_unshift($list, $editor);
|
|
|
2396 |
$list = array_unique($list);
|
|
|
2397 |
|
|
|
2398 |
// Set the list new list of editors.
|
|
|
2399 |
set_config('texteditors', implode(',', $list));
|
|
|
2400 |
}
|
|
|
2401 |
|
|
|
2402 |
/**
|
|
|
2403 |
* Allow to check for minimal Moodle version.
|
|
|
2404 |
*
|
|
|
2405 |
* @Given the site is running Moodle version :minversion or higher
|
|
|
2406 |
* @param string $minversion The minimum version of Moodle required (inclusive).
|
|
|
2407 |
*/
|
|
|
2408 |
public function the_site_is_running_moodle_version_or_higher(string $minversion): void {
|
|
|
2409 |
global $CFG;
|
|
|
2410 |
require_once($CFG->libdir . '/environmentlib.php');
|
|
|
2411 |
|
|
|
2412 |
$currentversion = normalize_version(get_config('', 'release'));
|
|
|
2413 |
|
|
|
2414 |
if (version_compare($currentversion, $minversion, '<')) {
|
|
|
2415 |
throw new Moodle\BehatExtension\Exception\SkippedException(
|
|
|
2416 |
'Site must be running Moodle version ' . $minversion . ' or higher'
|
|
|
2417 |
);
|
|
|
2418 |
}
|
|
|
2419 |
}
|
|
|
2420 |
|
|
|
2421 |
/**
|
|
|
2422 |
* Allow to check for maximum Moodle version.
|
|
|
2423 |
*
|
|
|
2424 |
* @Given the site is running Moodle version :maxversion or lower
|
|
|
2425 |
* @param string $maxversion The maximum version of Moodle required (inclusive).
|
|
|
2426 |
*/
|
|
|
2427 |
public function the_site_is_running_moodle_version_or_lower(string $maxversion): void {
|
|
|
2428 |
global $CFG;
|
|
|
2429 |
require_once($CFG->libdir . '/environmentlib.php');
|
|
|
2430 |
|
|
|
2431 |
$currentversion = normalize_version(get_config('', 'release'));
|
|
|
2432 |
|
|
|
2433 |
if (version_compare($currentversion, $maxversion, '>')) {
|
|
|
2434 |
throw new Moodle\BehatExtension\Exception\SkippedException(
|
|
|
2435 |
'Site must be running Moodle version ' . $maxversion . ' or lower'
|
|
|
2436 |
);
|
|
|
2437 |
}
|
|
|
2438 |
}
|
|
|
2439 |
|
|
|
2440 |
/**
|
|
|
2441 |
* Check that the page title contains a given string.
|
|
|
2442 |
*
|
|
|
2443 |
* @Given the page title should contain ":title"
|
|
|
2444 |
* @param string $title The string that should be present on the page title.
|
|
|
2445 |
*/
|
|
|
2446 |
public function the_page_title_should_contain(string $title): void {
|
|
|
2447 |
$session = $this->getSession();
|
|
|
2448 |
if ($this->running_javascript()) {
|
|
|
2449 |
// When running on JS, the page title can be changed via JS, so it's more reliable to get the actual page title via JS.
|
|
|
2450 |
$actualtitle = $session->evaluateScript("return document.title");
|
|
|
2451 |
} else {
|
|
|
2452 |
$titleelement = $session->getPage()->find('css', 'head title');
|
|
|
2453 |
if ($titleelement === null) {
|
|
|
2454 |
// Throw an exception if a page title is not present on the page.
|
|
|
2455 |
throw new ElementNotFoundException(
|
|
|
2456 |
$this->getSession(),
|
|
|
2457 |
'<title> element',
|
|
|
2458 |
'css',
|
|
|
2459 |
'head title'
|
|
|
2460 |
);
|
|
|
2461 |
}
|
|
|
2462 |
$actualtitle = $titleelement->getText();
|
|
|
2463 |
}
|
|
|
2464 |
|
|
|
2465 |
if (!str_contains($actualtitle, $title)) {
|
|
|
2466 |
throw new ExpectationException(
|
|
|
2467 |
"'$title' was not found from the current page title '$actualtitle'",
|
|
|
2468 |
$session
|
|
|
2469 |
);
|
|
|
2470 |
}
|
|
|
2471 |
}
|
|
|
2472 |
}
|