From 08544e3bdb69f74ffd02d7b68006de71e1f816b3 Mon Sep 17 00:00:00 2001 From: Andrew Hancox Date: Tue, 28 Jul 2015 11:21:24 +0100 Subject: [PATCH 1/2] MDL-50940 behat: Added ability to check field value by xpath --- lib/tests/behat/behat_forms.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index 63634ede7ac..d53c8d4b601 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -198,6 +198,31 @@ class behat_forms extends behat_base { } } + /** + * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. + * + * @Then /^the field with xpath "(?P(?:[^"]|\\")*)" matches value "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $fieldxpath + * @param string $value + * @return void + */ + public function the_field_with_xpath_matches_value($fieldxpath, $value) { + + // Get the field. + $fieldnode = $this->find('xpath', $fieldxpath); + $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession()); + + // Checks if the provided value matches the current field value. + if (!$formfield->matches($value)) { + $fieldvalue = $formfield->get_value(); + throw new ExpectationException( + 'The \'' . $fieldxpath . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' , + $this->getSession() + ); + } + } + /** * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * From b5b3a37049c0304040137f3b394c2e58f9f12aa4 Mon Sep 17 00:00:00 2001 From: rajesh Taneja Date: Wed, 23 Sep 2015 13:36:54 +0800 Subject: [PATCH 2/2] MDL-50940 behat: Added support for field matching with xpath --- .../tests/behat/get_and_set_fields.feature | 11 +++++ lib/tests/behat/behat_forms.php | 43 +++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/admin/tool/behat/tests/behat/get_and_set_fields.feature b/admin/tool/behat/tests/behat/get_and_set_fields.feature index 02c924e3c0c..7a195da8a65 100644 --- a/admin/tool/behat/tests/behat/get_and_set_fields.feature +++ b/admin/tool/behat/tests/behat/get_and_set_fields.feature @@ -137,6 +137,17 @@ Feature: Verify that all form fields values can be get and set And I press "Save my choice" And the field "one" matches value "1" And the field "two" matches value "" + # Check if field xpath set/match works. + And I am on site homepage + And I follow "Course 1" + And I navigate to "Edit settings" node in "Course administration" + And I set the field with xpath "//input[@id='id_idnumber']" to "Course id number" + And the field with xpath "//input[@name='idnumber']" matches value "Course id number" + And the field with xpath "//input[@name='idnumber']" does not match value "" + And I press "Save changes" + And I navigate to "Edit settings" node in "Course administration" + And the field "Course ID number" matches value "Course id number" + Scenario: with JS disabled all form fields getters and setters works as expected diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index d53c8d4b601..e9e1053b5fe 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -199,9 +199,34 @@ class behat_forms extends behat_base { } /** - * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. + * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * - * @Then /^the field with xpath "(?P(?:[^"]|\\")*)" matches value "(?P(?:[^"]|\\")*)"$/ + * @Then /^the field "(?P(?:[^"]|\\")*)" does not match value "(?P(?:[^"]|\\")*)"$/ + * @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)) { + throw new ExpectationException( + 'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' , + $this->getSession() + ); + } + } + + /** + * Checks, the field matches the value. + * + * @Then /^the field with xpath "(?P(?:[^"]|\\")*)" matches value "(?P(?:[^"]|\\")*)"$/ + * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find * @param string $fieldxpath * @param string $value @@ -224,25 +249,25 @@ class behat_forms extends behat_base { } /** - * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. + * Checks, the field does not match the value. * - * @Then /^the field "(?P(?:[^"]|\\")*)" does not match value "(?P(?:[^"]|\\")*)"$/ + * @Then /^the field with xpath "(?P(?:[^"]|\\")*)" does not match value "(?P(?:[^"]|\\")*)"$/ * @throws ExpectationException * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $field + * @param string $fieldxpath * @param string $value * @return void */ - public function the_field_does_not_match_value($field, $value) { + public function the_field_with_xpath_does_not_match_value($fieldxpath, $value) { // Get the field. - $formfield = behat_field_manager::get_form_field_from_label($field, $this); + $fieldnode = $this->find('xpath', $fieldxpath); + $formfield = behat_field_manager::get_form_field($fieldnode, $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 matches \'' . $value . '\' and it should not match it' , + 'The \'' . $fieldxpath . '\' value matches \'' . $value . '\' and it should not match it' , $this->getSession() ); }