Files
moodle/lib/tests/behat/behat_forms.php
T
David Monllao fa58e13891 MDL-45192 behat: Replacing specific-selector steps by a generic one
Having a I click on we can use all our selectors rather
than having multiple steps for the same purpose. This
step allow us to manage JS native dialogues without
having to fail a scenario.

Credit for Mohamed Alsharaf.
2014-05-13 20:37:39 +07:00

490 lines
18 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Steps definitions related with forms.
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
use Behat\Behat\Context\Step\Given as Given,
Behat\Behat\Context\Step\When as When,
Behat\Behat\Context\Step\Then as Then,
Behat\Gherkin\Node\TableNode as TableNode,
Behat\Mink\Element\NodeElement as NodeElement,
Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
/**
* Forms-related steps definitions.
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_forms extends behat_base {
/**
* Presses button with specified id|name|title|alt|value.
*
* @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $button
*/
public function press_button($button) {
// Ensures the button is present.
$buttonnode = $this->find_button($button);
$buttonnode->press();
}
/**
* Fills a form with field/value data. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
*
* Backport of Moodle 2.7 method to help backporting
* features to 2.6.
*
* @Given /^I set the following fields to these values:$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param TableNode $data
*/
public function i_set_the_following_fields_to_these_values(TableNode $data) {
// Expand all fields in case we have.
$this->expand_all_fields();
$datahash = $data->getRowsHash();
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
$this->set_field_value($locator, $value);
}
}
/**
* Fills a moodle form with field/value data. This step will be deprecated in Moodle 2.7 in favour of 'I set the following fields to these values:'.
*
* @Given /^I fill the moodle form with:$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param TableNode $data
*/
public function i_fill_the_moodle_form_with(TableNode $data) {
// Expand all fields in case we have.
$this->expand_all_fields();
$datahash = $data->getRowsHash();
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
$field = behat_field_manager::get_form_field_from_label($locator, $this);
// Delegates to the field class.
$field->set_value($value);
}
}
/**
* Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present.
* @Given /^I expand all fieldsets$/
*/
public function i_expand_all_fieldsets() {
$this->expand_all_fields();
}
/**
* Expands all moodle form fieldsets if they exists.
*
* Externalized from i_expand_all_fields to call it from
* other form-related steps without having to use steps-group calls.
*
* @throws ElementNotFoundException Thrown by behat_base::find_all
* @return void
*/
protected function expand_all_fields() {
// We ensure that all the editors are loaded and we can interact with them.
$this->ensure_editors_are_loaded();
// We already know that we waited for the DOM and the JS to be loaded, even the editor
// so, we will use the reduced timeout as it is a common task and we should save time.
try {
// Expand fieldsets link.
$xpath = "//div[@class='collapsible-actions']" .
"/descendant::a[contains(concat(' ', @class, ' '), ' collapseexpand ')]" .
"[not(contains(concat(' ', @class, ' '), ' collapse-all '))]";
$collapseexpandlink = $this->find('xpath', $xpath, false, false, self::REDUCED_TIMEOUT);
$collapseexpandlink->click();
} catch (ElementNotFoundException $e) {
// The behat_base::find() method throws an exception if there are no elements,
// we should not fail a test because of this. We continue if there are not expandable fields.
}
// Different try & catch as we can have expanded fieldsets with advanced fields on them.
try {
// Expand all fields xpath.
$showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" .
"[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]";
// We don't wait here as we already waited when getting the expand fieldsets links.
if (!$showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath)) {
return;
}
// Funny thing about this, with findAll() we specify a pattern and each element matching the pattern is added to the array
// with of xpaths with a [0], [1]... sufix, but when we click on an element it does not matches the specified xpath
// anymore (now is a "Show less..." link) so [1] becomes [0], that's why we always click on the first XPath match,
// will be always the next one.
$iterations = count($showmores);
for ($i = 0; $i < $iterations; $i++) {
$showmores[0]->click();
}
} catch (ElementNotFoundException $e) {
// We continue with the test.
}
}
/**
* Sets the specified value to the field.
*
* Backport of Moodle 2.7 method to help backporting
* features to 2.6.
*
* @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $value
* @return void
*/
public function i_set_the_field_to($field, $value) {
$this->set_field_value($field, $value);
}
/**
* Fills in form text field with specified id|name|label|value. It works with text-based fields. This step will be deprecated in Moodle 2.7 in favour of 'I set the field "FIELD_STRING" to "VALUE_STRING"'.
*
* @When /^I fill in "(?P<field_string>(?:[^"]|\\")*)" with "(?P<value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $value
*/
public function fill_field($field, $value) {
$this->set_field_value($field, $value);
}
/**
* Selects option in select field with specified id|name|label|value.
*
* @When /^I select "(?P<option_string>(?:[^"]|\\")*)" from "(?P<select_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $option
* @param string $select
*/
public function select_option($option, $select) {
$this->set_field_value($select, $option);
}
/**
* Selects the specified id|name|label from the specified radio button.
*
* @When /^I select "(?P<radio_button_string>(?:[^"]|\\")*)" radio button$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $radio The radio button id, name or label value
*/
public function select_radio($radio) {
$this->set_field_value($radio, 1);
}
/**
* Checks checkbox with specified id|name|label|value.
*
* @When /^I check "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $option
*/
public function check_option($option) {
$this->set_field_value($option, 1);
}
/**
* Unchecks checkbox with specified id|name|label|value.
*
* @When /^I uncheck "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $option
*/
public function uncheck_option($option) {
$this->set_field_value($option, '');
}
/**
* Checks that the field matches the specified value. When using multi-select fields use commas to separate selected options. This step will be deprecated in Moodle 2.7 in favour of 'the field "FIELD_STRING" matches value "VALUE_STRING"'.
*
* @Then /^the "(?P<field_string>(?:[^"]|\\")*)" field should match "(?P<value_string>(?:[^"]|\\")*)" value$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $locator
* @param string $value
*/
public function the_field_should_match_value($locator, $value) {
$field = behat_field_manager::get_form_field_from_label($locator, $this);
// Checks if the provided value matches the current field value.
if (!$field->matches($value)) {
$fieldvalue = $field->get_value();
throw new ExpectationException(
'The \'' . $locator . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
$this->getSession()
);
}
}
/**
* Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
*
* Backport of Moodle 2.7 method to help backporting
* features to 2.6.
*
* @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $value
* @return void
*/
public function the_field_matches_value($field, $value) {
// Get the field.
$formfield = behat_field_manager::get_form_field_from_label($field, $this);
// Checks if the provided value matches the current field value.
if (!$formfield->matches($value)) {
$fieldvalue = $formfield->get_value();
throw new ExpectationException(
'The \'' . $field . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
$this->getSession()
);
}
}
/**
* Checks that the form element field does not match the specified value.
*
* @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" does not match value "(?P<value_string>(?:[^"]|\\")*)"$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $value
* @return void
*/
public function the_field_does_not_match_value($field, $value) {
// Get the field.
$formfield = behat_field_manager::get_form_field_from_label($field, $this);
// Checks if the provided value matches the current field value.
if ($formfield->matches($value)) {
$fieldvalue = $formfield->get_value();
throw new ExpectationException(
'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' ,
$this->getSession()
);
}
}
/**
* Checks if fields values matches the provided values. Provide a table with field/value data.
*
* @Then /^the following fields match these values:$/
* @throws ExpectationException
* @param TableNode $data Pairs of | field | value |
*/
public function the_following_fields_match_these_values(TableNode $data) {
// Expand all fields in case we have.
$this->expand_all_fields();
$datahash = $data->getRowsHash();
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
$this->the_field_should_match_value($locator, $value);
}
}
/**
* Checks that fields values do not match the provided values. Provide a table with field/value data.
*
* @Then /^the following fields do not match these values:$/
* @throws ExpectationException
* @param TableNode $data Pairs of | field | value |
*/
public function the_following_fields_do_not_match_these_values(TableNode $data) {
// Expand all fields in case we have.
$this->expand_all_fields();
$datahash = $data->getRowsHash();
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
$this->the_field_does_not_match_value($locator, $value);
}
}
/**
* Checks, that checkbox with specified in|name|label|value is checked.
*
* @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should be checked$/
* @param string $checkbox
*/
public function assert_checkbox_checked($checkbox) {
$this->the_field_should_match_value($checkbox, 1);
}
/**
* Checks, that checkbox with specified in|name|label|value is unchecked.
*
* @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should not be checked$/
* @param string $checkbox
*/
public function assert_checkbox_not_checked($checkbox) {
$this->the_field_should_match_value($checkbox, '');
}
/**
* Checks, that given select box contains the specified option.
*
* @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should contain "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $select The select element name
* @param string $option The option text/value. Plain value or comma separated
* values if multiple. Commas in multiple values escaped with backslash.
*/
public function the_select_box_should_contain($select, $option) {
$selectnode = $this->find_field($select);
$multiple = $selectnode->hasAttribute('multiple');
$optionsarr = array(); // Array of passed value/text options to test.
if ($multiple) {
// Can pass multiple comma separated, with valuable commas escaped with backslash.
foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) {
$optionsarr[] = trim($opt);
}
} else {
// Only one option has been passed.
$optionsarr[] = trim($option);
}
// Now get all the values and texts in the select.
$options = $selectnode->findAll('xpath', '//option');
$values = array();
foreach ($options as $opt) {
$values[trim($opt->getValue())] = trim($opt->getText());
}
foreach ($optionsarr as $opt) {
// Verify every option is a valid text or value.
if (!in_array($opt, $values) && !array_key_exists($opt, $values)) {
throw new ExpectationException(
'The select box "' . $select . '" does not contain the option "' . $opt . '"',
$this->getSession()
);
}
}
}
/**
* Checks, that given select box does not contain the specified option.
*
* @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should not contain "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $select The select element name
* @param string $option The option text/value. Plain value or comma separated
* values if multiple. Commas in multiple values escaped with backslash.
*/
public function the_select_box_should_not_contain($select, $option) {
$selectnode = $this->find_field($select);
$multiple = $selectnode->hasAttribute('multiple');
$optionsarr = array(); // Array of passed value/text options to test.
if ($multiple) {
// Can pass multiple comma separated, with valuable commas escaped with backslash.
foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) {
$optionsarr[] = trim($opt);
}
} else {
// Only one option has been passed.
$optionsarr[] = trim($option);
}
// Now get all the values and texts in the select.
$options = $selectnode->findAll('xpath', '//option');
$values = array();
foreach ($options as $opt) {
$values[trim($opt->getValue())] = trim($opt->getText());
}
foreach ($optionsarr as $opt) {
// Verify every option is not a valid text or value.
if (in_array($opt, $values) || array_key_exists($opt, $values)) {
throw new ExpectationException(
'The select box "' . $select . '" contains the option "' . $opt . '"',
$this->getSession()
);
}
}
}
/**
* Generic field setter.
*
* Internal API method, a generic *I set "VALUE" to "FIELD" field*
* could be created based on it.
*
* @param string $fieldlocator The pointer to the field, it will depend on the field type.
* @param string $value
* @return void
*/
protected function set_field_value($fieldlocator, $value) {
// We delegate to behat_form_field class, it will
// guess the type properly as it is a select tag.
$field = behat_field_manager::get_form_field_from_label($fieldlocator, $this);
$field->set_value($value);
}
}