Skip to content

Commit

Permalink
MDL-66835 behat: steps for setting/checking fields in containers
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Oct 9, 2019
1 parent 2821f78 commit bf4904c
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 1 deletion.
@@ -0,0 +1,27 @@
@tool_behat
Feature: Behat steps for interacting with form work
In order to test my Moodle code
As a developer
I need the Behat steps for form elements to work reliably

@javascript
Scenario: Test fields in containers
Given the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
When I log in as "admin"
And I am on "Course 1" course homepage
# Just get to any form.
And I navigate to "Edit settings" in current page administration
And I set the field "Course full name" in the "General" "fieldset" to "Frog"
And I set the following fields in the "Appearance" "fieldset" to these values:
| Show activity reports | Yes |
| Number of announcements | 1 |
Then the field "Show activity reports" in the "Appearance" "fieldset" matches value "Yes"
And the field "Show activity reports" in the "Appearance" "fieldset" does not match value "No"
And the following fields in the "region-main" "region" match these values:
| Course full name | Frog |
| Number of announcements | 1 |
And the following fields in the "region-main" "region" do not match these values:
| Course full name | Course 1 |
| Number of announcements | 5 |
159 changes: 158 additions & 1 deletion lib/tests/behat/behat_forms.php
Expand Up @@ -36,6 +36,9 @@
/**
* Forms-related steps definitions.
*
* Note, Behat tests to verify that the steps defined here work as advertised
* are kept in admin/tool/behat/tests/behat.
*
* @package core
* @category test
* @copyright 2012 David Monllaó
Expand Down Expand Up @@ -90,6 +93,29 @@ public function i_set_the_following_fields_to_these_values(TableNode $data) {
}
}

/**
* Fills a form with field/value data.
*
* @Given /^I set the following fields in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" to these values:$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $containerelement Element we look in
* @param string $containerselectortype The type of selector where we look in
* @param TableNode $data
*/
public function i_set_the_following_fields_in_container_to_these_values(
$containerelement, $containerselectortype, 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_in_container($locator, $value, $containerselectortype, $containerelement);
}
}

/**
* Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present.
* @Given /^I expand all fieldsets$/
Expand Down Expand Up @@ -195,6 +221,20 @@ public function i_set_the_field_to($field, $value) {
$this->set_field_value($field, $value);
}

/**
* Sets the specified value to the field.
*
* @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $containerelement Element we look in
* @param string $containerselectortype The type of selector where we look in
* @param string $value
*/
public function i_set_the_field_in_container_to($field, $containerelement, $containerselectortype, $value) {
$this->set_field_value_in_container($field, $value, $containerselectortype, $containerelement);
}

/**
* Press the key in the field to trigger the javascript keypress event
*
Expand Down Expand Up @@ -284,7 +324,6 @@ public function the_field_matches_value($field, $value) {
* @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) {

Expand All @@ -300,6 +339,58 @@ public function the_field_does_not_match_value($field, $value) {
}
}

/**
* Checks, the field matches the value.
*
* @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $containerelement Element we look in
* @param string $containerselectortype The type of selector where we look in
* @param string $value
*/
public function the_field_in_container_matches_value($field, $containerelement, $containerselectortype, $value) {

// Get the field.
$node = $this->get_node_in_container('field', $field, $containerselectortype, $containerelement);
$formfield = behat_field_manager::get_form_field($node, $this->getSession());

// 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, the field does not match the value.
*
* @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $containerelement Element we look in
* @param string $containerselectortype The type of selector where we look in
* @param string $value
*/
public function the_field_in_container_does_not_match_value($field, $containerelement, $containerselectortype, $value) {

// Get the field.
$node = $this->get_node_in_container('field', $field, $containerselectortype, $containerelement);
$formfield = behat_field_manager::get_form_field($node, $this->getSession());

// Checks if the provided value matches the current field value.
if ($formfield->matches($value)) {
throw new ExpectationException(
'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' ,
$this->getSession()
);
}
}

/**
* Checks, the field matches the value.
*
Expand Down Expand Up @@ -391,6 +482,52 @@ public function the_following_fields_do_not_match_these_values(TableNode $data)
}
}

/**
* Checks, the provided field/value matches.
*
* @Then /^the following fields in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" match these values:$/
* @throws ExpectationException
* @param string $containerelement Element we look in
* @param string $containerselectortype The type of selector where we look in
* @param TableNode $data Pairs of | field | value |
*/
public function the_following_fields_in_container_match_these_values(
$containerelement, $containerselectortype, 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_in_container_matches_value($locator, $containerelement, $containerselectortype, $value);
}
}

/**
* Checks that the provided field/value pairs don't match.
*
* @Then /^the following fields in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" do not match these values:$/
* @throws ExpectationException
* @param string $containerelement Element we look in
* @param string $containerselectortype The type of selector where we look in
* @param TableNode $data Pairs of | field | value |
*/
public function the_following_fields_in_container_do_not_match_these_values(
$containerelement, $containerselectortype, 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_in_container_does_not_match_value($locator, $containerelement, $containerselectortype, $value);
}
}

/**
* Checks, that given select box contains the specified option.
*
Expand Down Expand Up @@ -497,6 +634,26 @@ protected function set_field_value($fieldlocator, $value) {
$field->set_value($value);
}

/**
* 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 the value to set
* @param string $containerselectortype The type of selector where we look in
* @param string $containerelement Element we look in
*/
protected function set_field_value_in_container($fieldlocator, $value, $containerselectortype, $containerelement) {

$node = $this->get_node_in_container('field', $fieldlocator, $containerselectortype, $containerelement);
// 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($node, $this->getSession());
$field->set_value($value);
}

/**
* Select a value from single select and redirect.
*
Expand Down

0 comments on commit bf4904c

Please sign in to comment.