2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Steps definitions related with forms.
22 * @copyright 2012 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29 require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
31 use Behat\Gherkin\Node\TableNode as TableNode,
32 Behat\Gherkin\Node\PyStringNode as PyStringNode,
33 Behat\Mink\Exception\ExpectationException as ExpectationException,
34 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
37 * Forms-related steps definitions.
41 * @copyright 2012 David Monllaó
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class behat_forms extends behat_base {
47 * Presses button with specified id|name|title|alt|value.
49 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
50 * @throws ElementNotFoundException Thrown by behat_base::find
51 * @param string $button
53 public function press_button($button) {
55 // Ensures the button is present.
56 $buttonnode = $this->find_button($button);
57 // Focus on button to ensure it is in viewport, before pressing it.
58 if ($this->running_javascript()) {
65 * Press button with specified id|name|title|alt|value and switch to main window.
67 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)" and switch to main window$/
68 * @throws ElementNotFoundException Thrown by behat_base::find
69 * @param string $button
71 public function press_button_and_switch_to_main_window($button) {
72 // Ensures the button is present, before pressing.
73 $buttonnode = $this->find_button($button);
76 // Switch to main window.
77 $this->getSession()->switchToWindow(behat_general::MAIN_WINDOW_NAME);
81 * Fills a form with field/value data. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
83 * @Given /^I set the following fields to these values:$/
84 * @throws ElementNotFoundException Thrown by behat_base::find
85 * @param TableNode $data
87 public function i_set_the_following_fields_to_these_values(TableNode $data) {
89 // Expand all fields in case we have.
90 $this->expand_all_fields();
92 $datahash = $data->getRowsHash();
94 // The action depends on the field type.
95 foreach ($datahash as $locator => $value) {
96 $this->set_field_value($locator, $value);
101 * Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present.
102 * @Given /^I expand all fieldsets$/
104 public function i_expand_all_fieldsets() {
105 $this->expand_all_fields();
109 * Expands all moodle form fieldsets if they exists.
111 * Externalized from i_expand_all_fields to call it from
112 * other form-related steps without having to use steps-group calls.
114 * @throws ElementNotFoundException Thrown by behat_base::find_all
117 protected function expand_all_fields() {
118 // Expand only if JS mode, else not needed.
119 if (!$this->running_javascript()) {
123 // We already know that we waited for the DOM and the JS to be loaded, even the editor
124 // so, we will use the reduced timeout as it is a common task and we should save time.
127 // Expand fieldsets link.
128 $xpath = "//div[@class='collapsible-actions']" .
129 "/descendant::a[contains(concat(' ', @class, ' '), ' collapseexpand ')]" .
130 "[not(contains(concat(' ', @class, ' '), ' collapse-all '))]";
131 $collapseexpandlink = $this->find('xpath', $xpath, false, false, self::REDUCED_TIMEOUT);
132 $collapseexpandlink->click();
134 } catch (ElementNotFoundException $e) {
135 // The behat_base::find() method throws an exception if there are no elements,
136 // we should not fail a test because of this. We continue if there are not expandable fields.
139 // Different try & catch as we can have expanded fieldsets with advanced fields on them.
142 // Expand all fields xpath.
143 $showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" .
144 "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]";
146 // We don't wait here as we already waited when getting the expand fieldsets links.
147 if (!$showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath)) {
151 if ($this->getSession()->getDriver() instanceof \DMore\ChromeDriver\ChromeDriver) {
152 // Chrome Driver produces unique xpaths for each element.
153 foreach ($showmores as $showmore) {
157 // Funny thing about this, with findAll() we specify a pattern and each element matching the pattern
158 // is added to the array with of xpaths with a [0], [1]... sufix, but when we click on an element it
159 // does not matches the specified xpath anymore (now is a "Show less..." link) so [1] becomes [0],
160 // that's why we always click on the first XPath match, will be always the next one.
161 $iterations = count($showmores);
162 for ($i = 0; $i < $iterations; $i++) {
163 $showmores[0]->click();
167 } catch (ElementNotFoundException $e) {
168 // We continue with the test.
174 * Sets the field to wwwroot plus the given path. Include the first slash.
176 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to local url "(?P<field_path_string>(?:[^"]|\\")*)"$/
177 * @throws ElementNotFoundException Thrown by behat_base::find
178 * @param string $field
179 * @param string $path
182 public function i_set_the_field_to_local_url($field, $path) {
184 $this->set_field_value($field, $CFG->wwwroot . $path);
188 * Sets the specified value to the field.
190 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/
191 * @throws ElementNotFoundException Thrown by behat_base::find
192 * @param string $field
193 * @param string $value
196 public function i_set_the_field_to($field, $value) {
197 $this->set_field_value($field, $value);
201 * Press the key in the field to trigger the javascript keypress event
203 * Note that the character key will not actually be typed in the input field
205 * @Given /^I press key "(?P<key_string>(?:[^"]|\\")*)" in the field "(?P<field_string>(?:[^"]|\\")*)"$/
206 * @throws ElementNotFoundException Thrown by behat_base::find
207 * @param string $key either char-code or character itself,
208 * may optionally be prefixed with ctrl-, alt-, shift- or meta-
209 * @param string $field
212 public function i_press_key_in_the_field($key, $field) {
213 if (!$this->running_javascript()) {
214 throw new DriverException('Key press step is not available with Javascript disabled');
216 $fld = behat_field_manager::get_form_field_from_label($field, $this);
219 if (preg_match('/-/', $key)) {
220 list($modifier, $char) = preg_split('/-/', $key, 2);
222 if (is_numeric($char)) {
225 $fld->key_press($char, $modifier);
229 * Sets the specified value to the field.
231 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to multiline:$/
232 * @throws ElementNotFoundException Thrown by behat_base::find
233 * @param string $field
234 * @param PyStringNode $value
237 public function i_set_the_field_to_multiline($field, PyStringNode $value) {
238 $this->set_field_value($field, (string)$value);
242 * Sets the specified value to the field with xpath.
244 * @Given /^I set the field with xpath "(?P<fieldxpath_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/
245 * @throws ElementNotFoundException Thrown by behat_base::find
246 * @param string $field
247 * @param string $value
250 public function i_set_the_field_with_xpath_to($fieldxpath, $value) {
251 $fieldnode = $this->find('xpath', $fieldxpath);
252 $this->ensure_node_is_visible($fieldnode);
253 $field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
254 $field->set_value($value);
258 * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
260 * @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/
261 * @throws ElementNotFoundException Thrown by behat_base::find
262 * @param string $field
263 * @param string $value
266 public function the_field_matches_value($field, $value) {
269 $formfield = behat_field_manager::get_form_field_from_label($field, $this);
271 // Checks if the provided value matches the current field value.
272 if (!$formfield->matches($value)) {
273 $fieldvalue = $formfield->get_value();
274 throw new ExpectationException(
275 'The \'' . $field . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
282 * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
284 * @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/
285 * @throws ExpectationException
286 * @throws ElementNotFoundException Thrown by behat_base::find
287 * @param string $field
288 * @param string $value
291 public function the_field_does_not_match_value($field, $value) {
294 $formfield = behat_field_manager::get_form_field_from_label($field, $this);
296 // Checks if the provided value matches the current field value.
297 if ($formfield->matches($value)) {
298 throw new ExpectationException(
299 'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' ,
306 * Checks, the field matches the value.
308 * @Then /^the field with xpath "(?P<xpath_string>(?:[^"]|\\")*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/
309 * @throws ExpectationException
310 * @throws ElementNotFoundException Thrown by behat_base::find
311 * @param string $fieldxpath
312 * @param string $value
315 public function the_field_with_xpath_matches_value($fieldxpath, $value) {
318 $fieldnode = $this->find('xpath', $fieldxpath);
319 $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
321 // Checks if the provided value matches the current field value.
322 if (!$formfield->matches($value)) {
323 $fieldvalue = $formfield->get_value();
324 throw new ExpectationException(
325 'The \'' . $fieldxpath . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
332 * Checks, the field does not match the value.
334 * @Then /^the field with xpath "(?P<xpath_string>(?:[^"]|\\")*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/
335 * @throws ExpectationException
336 * @throws ElementNotFoundException Thrown by behat_base::find
337 * @param string $fieldxpath
338 * @param string $value
341 public function the_field_with_xpath_does_not_match_value($fieldxpath, $value) {
344 $fieldnode = $this->find('xpath', $fieldxpath);
345 $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
347 // Checks if the provided value matches the current field value.
348 if ($formfield->matches($value)) {
349 throw new ExpectationException(
350 'The \'' . $fieldxpath . '\' value matches \'' . $value . '\' and it should not match it' ,
357 * Checks, the provided field/value matches. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
359 * @Then /^the following fields match these values:$/
360 * @throws ExpectationException
361 * @param TableNode $data Pairs of | field | value |
363 public function the_following_fields_match_these_values(TableNode $data) {
365 // Expand all fields in case we have.
366 $this->expand_all_fields();
368 $datahash = $data->getRowsHash();
370 // The action depends on the field type.
371 foreach ($datahash as $locator => $value) {
372 $this->the_field_matches_value($locator, $value);
377 * Checks that the provided field/value pairs don't match. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
379 * @Then /^the following fields do not match these values:$/
380 * @throws ExpectationException
381 * @param TableNode $data Pairs of | field | value |
383 public function the_following_fields_do_not_match_these_values(TableNode $data) {
385 // Expand all fields in case we have.
386 $this->expand_all_fields();
388 $datahash = $data->getRowsHash();
390 // The action depends on the field type.
391 foreach ($datahash as $locator => $value) {
392 $this->the_field_does_not_match_value($locator, $value);
397 * Checks, that given select box contains the specified option.
399 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should contain "(?P<option_string>(?:[^"]|\\")*)"$/
400 * @throws ExpectationException
401 * @throws ElementNotFoundException Thrown by behat_base::find
402 * @param string $select The select element name
403 * @param string $option The option text/value. Plain value or comma separated
404 * values if multiple. Commas in multiple values escaped with backslash.
406 public function the_select_box_should_contain($select, $option) {
408 $selectnode = $this->find_field($select);
409 $multiple = $selectnode->hasAttribute('multiple');
410 $optionsarr = array(); // Array of passed value/text options to test.
413 // Can pass multiple comma separated, with valuable commas escaped with backslash.
414 foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) {
415 $optionsarr[] = trim($opt);
418 // Only one option has been passed.
419 $optionsarr[] = trim($option);
422 // Now get all the values and texts in the select.
423 $options = $selectnode->findAll('xpath', '//option');
425 foreach ($options as $opt) {
426 $values[trim($opt->getValue())] = trim($opt->getText());
429 foreach ($optionsarr as $opt) {
430 // Verify every option is a valid text or value.
431 if (!in_array($opt, $values) && !array_key_exists($opt, $values)) {
432 throw new ExpectationException(
433 'The select box "' . $select . '" does not contain the option "' . $opt . '"',
441 * Checks, that given select box does not contain the specified option.
443 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should not contain "(?P<option_string>(?:[^"]|\\")*)"$/
444 * @throws ExpectationException
445 * @throws ElementNotFoundException Thrown by behat_base::find
446 * @param string $select The select element name
447 * @param string $option The option text/value. Plain value or comma separated
448 * values if multiple. Commas in multiple values escaped with backslash.
450 public function the_select_box_should_not_contain($select, $option) {
452 $selectnode = $this->find_field($select);
453 $multiple = $selectnode->hasAttribute('multiple');
454 $optionsarr = array(); // Array of passed value/text options to test.
457 // Can pass multiple comma separated, with valuable commas escaped with backslash.
458 foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) {
459 $optionsarr[] = trim($opt);
462 // Only one option has been passed.
463 $optionsarr[] = trim($option);
466 // Now get all the values and texts in the select.
467 $options = $selectnode->findAll('xpath', '//option');
469 foreach ($options as $opt) {
470 $values[trim($opt->getValue())] = trim($opt->getText());
473 foreach ($optionsarr as $opt) {
474 // Verify every option is not a valid text or value.
475 if (in_array($opt, $values) || array_key_exists($opt, $values)) {
476 throw new ExpectationException(
477 'The select box "' . $select . '" contains the option "' . $opt . '"',
485 * Generic field setter.
487 * Internal API method, a generic *I set "VALUE" to "FIELD" field*
488 * could be created based on it.
490 * @param string $fieldlocator The pointer to the field, it will depend on the field type.
491 * @param string $value
494 protected function set_field_value($fieldlocator, $value) {
496 // We delegate to behat_form_field class, it will
497 // guess the type properly as it is a select tag.
498 $field = behat_field_manager::get_form_field_from_label($fieldlocator, $this);
499 $field->set_value($value);
503 * Select a value from single select and redirect.
505 * @Given /^I select "(?P<singleselect_option_string>(?:[^"]|\\")*)" from the "(?P<singleselect_name_string>(?:[^"]|\\")*)" singleselect$/
507 public function i_select_from_the_singleselect($option, $singleselect) {
509 $this->execute('behat_forms::i_set_the_field_to', array($this->escape($singleselect), $this->escape($option)));
511 if (!$this->running_javascript()) {
512 // Press button in the specified select container.
513 $containerxpath = "//div[" .
514 "(contains(concat(' ', normalize-space(@class), ' '), ' singleselect ') " .
515 "or contains(concat(' ', normalize-space(@class), ' '), ' urlselect ')".
517 .//label[contains(normalize-space(string(.)), '" . $singleselect . "')] " .
518 "or .//select[(./@name='" . $singleselect . "' or ./@id='". $singleselect . "')]" .
521 $this->execute('behat_general::i_click_on_in_the',
522 array(get_string('go'), "button", $containerxpath, "xpath_element")
528 * Select item from autocomplete list.
530 * @Given /^I click on "([^"]*)" item in the autocomplete list$/
532 * @param string $item
534 public function i_click_on_item_in_the_autocomplete_list($item) {
535 $xpathtarget = "//ul[@class='form-autocomplete-suggestions']//*[contains(.,'" . $item . "')]";
537 $this->execute('behat_general::i_click_on', [$xpathtarget, 'xpath_element']);
539 $this->execute('behat_general::i_press_key_in_element', ['13', 'body', 'xpath_element']);
543 * Open the auto-complete suggestions list (Assuming there is only one on the page.).
545 * @Given /^I open the autocomplete suggestions list$/
547 public function i_open_the_autocomplete_suggestions_list() {
548 $csstarget = ".form-autocomplete-downarrow";
549 $this->execute('behat_general::i_click_on', [$csstarget, 'css_element']);