MDL-39348 behat: Chaining strings instead of splitting strings in lines
[moodle.git] / lib / tests / behat / behat_forms.php
blobf785d1690a80f326627df40fb15c7dcc838ded05
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.
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/>.
17 /**
18 * Steps definitions related with forms.
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
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\Behat\Context\Step\Given as Given,
32 Behat\Behat\Context\Step\When as When,
33 Behat\Behat\Context\Step\Then as Then,
34 Behat\Gherkin\Node\TableNode as TableNode,
35 Behat\Mink\Element\NodeElement as NodeElement,
36 Behat\Mink\Exception\ExpectationException as ExpectationException,
37 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
39 /**
40 * Forms-related steps definitions.
42 * @package core
43 * @category test
44 * @copyright 2012 David MonllaĆ³
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 class behat_forms extends behat_base {
49 /**
50 * Presses button with specified id|name|title|alt|value.
52 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
53 * @throws ElementNotFoundException Thrown by behat_base::find
54 * @param string $button
56 public function press_button($button) {
58 // Ensures the button is present.
59 $buttonnode = $this->find_button($button);
60 $buttonnode->press();
63 /**
64 * Fills a moodle form with field/value data.
66 * @Given /^I fill the moodle form with:$/
67 * @throws ElementNotFoundException Thrown by behat_base::find
68 * @param TableNode $data
70 public function i_fill_the_moodle_form_with(TableNode $data) {
72 // Expand all fields in case we have.
73 $this->expand_all_fields();
75 $datahash = $data->getRowsHash();
77 // The action depends on the field type.
78 foreach ($datahash as $locator => $value) {
80 // Getting the node element pointed by the label.
81 $fieldnode = $this->find_field($locator);
83 // Gets the field type from a parent node.
84 $field = behat_field_manager::get_field($fieldnode, $locator, $this->getSession());
86 // Delegates to the field class.
87 $field->set_value($value);
91 /**
92 * Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present.
93 * @Given /^I expand all fieldsets$/
95 public function i_expand_all_fieldsets() {
96 $this->expand_all_fields();
99 /**
100 * Expands all moodle form fieldsets if they exists.
102 * Externalized from i_expand_all_fields to call it from
103 * other form-related steps without having to use steps-group calls.
105 * @throws ElementNotFoundException Thrown by behat_base::find_all
106 * @return void
108 protected function expand_all_fields() {
110 // behat_base::find() throws an exception if there are no elements, we should not fail a test because of this.
111 try {
113 // Expand fieldsets.
114 $fieldsets = $this->find_all('css', 'fieldset.collapsed a.fheader');
116 // We are supposed to have fieldsets here, otherwise exception.
118 // Funny thing about this, with find_all() we specify a pattern and each element matching the pattern is added to the array
119 // with of xpaths with a [0], [1]... sufix, but when we click on an element it does not matches the specified xpath
120 // anymore (is not collapsed) so [1] becomes [0], that's why we always click on the first XPath match, will be always the next one.
121 $iterations = count($fieldsets);
122 for ($i = 0; $i < $iterations; $i++) {
123 $fieldsets[0]->click();
126 } catch (ElementNotFoundException $e) {
127 // We continue if there are not expanded fields.
130 // Different try & catch as we can have expanded fieldsets with advanced fields on them.
131 try {
133 // Show all fields.
134 $showmorestr = get_string('showmore', 'form');
135 $showmores = $this->find_all('xpath', "//a[contains(concat(' ', normalize-space(.), ' '), '" . $showmorestr . "')]" .
136 "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]");
138 // We are supposed to have 'show more's here, otherwise exception.
140 // Same funny case, after clicking on the element the [1] showmore link becomes the [0].
141 $iterations = count($showmores);
142 for ($i = 0; $i < $iterations; $i++) {
143 $showmores[0]->click();
146 } catch (ElementNotFoundException $e) {
147 // We continue with the test.
153 * Fills in form field with specified id|name|label|value.
155 * @When /^I fill in "(?P<field_string>(?:[^"]|\\")*)" with "(?P<value_string>(?:[^"]|\\")*)"$/
156 * @throws ElementNotFoundException Thrown by behat_base::find
157 * @param string $field
158 * @param string $value
160 public function fill_field($field, $value) {
162 $fieldnode = $this->find_field($field);
163 $fieldnode->setValue($value);
167 * Selects option in select field with specified id|name|label|value.
169 * @When /^I select "(?P<option_string>(?:[^"]|\\")*)" from "(?P<select_string>(?:[^"]|\\")*)"$/
170 * @throws ElementNotFoundException Thrown by behat_base::find
171 * @param string $option
172 * @param string $select
174 public function select_option($option, $select) {
176 $selectnode = $this->find_field($select);
177 $selectnode->selectOption($option);
179 // Adding a click as Selenium requires it to fire some JS events.
180 if ($this->running_javascript()) {
181 $selectnode->click();
186 * Selects the specified id|name|label from the specified radio button.
188 * @When /^I select "(?P<radio_button_string>(?:[^"]|\\")*)" radio button$/
189 * @throws ElementNotFoundException Thrown by behat_base::find
190 * @param string $radio The radio button id, name or label value
192 public function select_radio($radio) {
194 $radionode = $this->find_radio($radio);
195 $radionode->check();
197 // Adding a click as Selenium requires it to fire some JS events.
198 if ($this->running_javascript()) {
199 $radionode->click();
204 * Checks checkbox with specified id|name|label|value.
206 * @When /^I check "(?P<option_string>(?:[^"]|\\")*)"$/
207 * @throws ElementNotFoundException Thrown by behat_base::find
208 * @param string $option
210 public function check_option($option) {
212 $checkboxnode = $this->find_field($option);
213 $checkboxnode->check();
217 * Unchecks checkbox with specified id|name|label|value.
219 * @When /^I uncheck "(?P<option_string>(?:[^"]|\\")*)"$/
220 * @throws ElementNotFoundException Thrown by behat_base::find
221 * @param string $option
223 public function uncheck_option($option) {
225 $checkboxnode = $this->find_field($option);
226 $checkboxnode->uncheck();
230 * Checks that the form element field have the specified value.
232 * @Then /^the "(?P<field_string>(?:[^"]|\\")*)" field should match "(?P<value_string>(?:[^"]|\\")*)" value$/
233 * @throws ExpectationException
234 * @throws ElementNotFoundException Thrown by behat_base::find
235 * @param string $locator
236 * @param string $value
238 public function the_field_should_match_value($locator, $value) {
240 $fieldnode = $this->find_field($locator);
242 // Get the field.
243 $field = behat_field_manager::get_field($fieldnode, $locator, $this->getSession());
244 $fieldvalue = $field->get_value();
246 // Checks if the provided value matches the current field value.
247 if (trim($value) != trim($fieldvalue)) {
248 throw new ExpectationException(
249 'The \'' . $locator . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
250 $this->getSession()
256 * Checks, that checkbox with specified in|name|label|value is checked.
258 * @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should be checked$/
259 * @see Behat\MinkExtension\Context\MinkContext
260 * @param string $checkbox
262 public function assert_checkbox_checked($checkbox) {
263 $this->assertSession()->checkboxChecked($checkbox);
267 * Checks, that checkbox with specified in|name|label|value is unchecked.
269 * @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should not be checked$/
270 * @see Behat\MinkExtension\Context\MinkContext
271 * @param string $checkbox
273 public function assert_checkbox_not_checked($checkbox) {
274 $this->assertSession()->checkboxNotChecked($checkbox);
278 * Checks, that given select box contains the specified option.
280 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should contain "(?P<option_string>(?:[^"]|\\")*)"$/
281 * @throws ExpectationException
282 * @throws ElementNotFoundException Thrown by behat_base::find
283 * @param string $select The select element name
284 * @param string $option The option text/value
286 public function the_select_box_should_contain($select, $option) {
288 $selectnode = $this->find_field($select);
290 $regex = '/' . preg_quote($option, '/') . '/ui';
291 if (!preg_match($regex, $selectnode->getText())) {
292 throw new ExpectationException(
293 'The select box "' . $select . '" does not contains the option "' . $option . '"',
294 $this->getSession()
301 * Checks, that given select box does not contain the specified option.
303 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should not contain "(?P<option_string>(?:[^"]|\\")*)"$/
304 * @throws ExpectationException
305 * @throws ElementNotFoundException Thrown by behat_base::find
306 * @param string $select The select element name
307 * @param string $option The option text/value
309 public function the_select_box_should_not_contain($select, $option) {
311 $selectnode = $this->find_field($select);
313 $regex = '/' . preg_quote($option, '/') . '/ui';
314 if (preg_match($regex, $selectnode->getText())) {
315 throw new ExpectationException(
316 'The select box "' . $select . '" contains the option "' . $option . '"',
317 $this->getSession()