From fb624374bcedf2b209cfc434941ea1295b98b129 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 12 Mar 2013 10:54:25 +0800 Subject: [PATCH 1/3] MDL-38410 behat: Step to select from radio inputs --- lib/tests/behat/behat_forms.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index 4ce80619172..e2f92923f59 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -179,6 +179,22 @@ class behat_forms extends behat_base { $selectnode->click(); } + /** + * Selects the specified id|name|label from the specified radio button. + * + * @When /^I select "(?P(?:[^"]|\\")*)" radio button$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $radio + */ + public function select_radio($radio) { + + $radionode = $this->find_radio($radio); + $radionode->check(); + + // Adding a click as Selenium requires it to fire some JS events. + $radionode->click(); + } + /** * Checks checkbox with specified id|name|label|value. * From 5cde7298e3f0c3b6d57cb94ba53a1373930cf89d Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 12 Mar 2013 10:54:57 +0800 Subject: [PATCH 2/3] MDL-38410 behat: Adding new form fields --- lib/behat/form_field/behat_form_checkbox.php | 68 +++++++++++++++++++ .../form_field/behat_form_date_selector.php | 45 ++++++++++++ .../behat_form_date_time_selector.php | 45 ++++++++++++ lib/tests/behat/behat_forms.php | 2 +- 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 lib/behat/form_field/behat_form_checkbox.php create mode 100644 lib/behat/form_field/behat_form_date_selector.php create mode 100644 lib/behat/form_field/behat_form_date_time_selector.php diff --git a/lib/behat/form_field/behat_form_checkbox.php b/lib/behat/form_field/behat_form_checkbox.php new file mode 100644 index 00000000000..73f9b61d213 --- /dev/null +++ b/lib/behat/form_field/behat_form_checkbox.php @@ -0,0 +1,68 @@ +. + +/** + * Single checkbox form element. + * + * @package core_form + * @category test + * @copyright 2013 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__ . '/behat_form_field.php'); + +/** + * Checkbox form field. + * + * @package core_form + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_form_checkbox extends behat_form_field { + + /** + * Sets the value of a checkbox. + * + * Anything !empty() is considered checked. + * + * @param string $value + * @return void + */ + public function set_value($value) { + + if (!empty($value) && !$this->field->isChecked()) { + // Check it if it should be checked and it is not. + $this->field->click(); + + } else if (empty($value) && $this->field->isChecked()) { + // Uncheck if it is checked and shouldn't. + $this->field->click(); + } + } + + /** + * Returns whether the field is checked or not. + * + * @return bool True if it is checked and false if it's not. + */ + public function get_value() { + return $this->field->isChecked(); + } +} diff --git a/lib/behat/form_field/behat_form_date_selector.php b/lib/behat/form_field/behat_form_date_selector.php new file mode 100644 index 00000000000..2d86a768cf6 --- /dev/null +++ b/lib/behat/form_field/behat_form_date_selector.php @@ -0,0 +1,45 @@ +. + +/** + * Date form field class. + * + * @package core_form + * @category test + * @copyright 2013 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__ . '/behat_form_select.php'); + +/** + * Date form field. + * + * Simple extension of behat_form_select to allow date-type + * select fields to be filled like select elements instead of + * text elements. + * + * This class will be refactored in case we are interested in + * creating more complex formats to fill date and date-time fields. + * + * @package core_form + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_form_date_selector extends behat_form_select {} diff --git a/lib/behat/form_field/behat_form_date_time_selector.php b/lib/behat/form_field/behat_form_date_time_selector.php new file mode 100644 index 00000000000..22c4befa610 --- /dev/null +++ b/lib/behat/form_field/behat_form_date_time_selector.php @@ -0,0 +1,45 @@ +. + +/** + * Date time form field class. + * + * @package core_form + * @category test + * @copyright 2013 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__ . '/behat_form_select.php'); + +/** + * Date time form field. + * + * Simple extension of behat_form_select to allow datetime-type + * select fields to be filled like select elements instead of + * text elements. + * + * This class will be refactored in case we are interested in + * creating more complex formats to fill date and date-time fields. + * + * @package core_form + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_form_date_time_selector extends behat_form_select {} diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index e2f92923f59..23a53fa49c4 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -184,7 +184,7 @@ class behat_forms extends behat_base { * * @When /^I select "(?P(?:[^"]|\\")*)" radio button$/ * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $radio + * @param string $radio The radio button id, name or label value */ public function select_radio($radio) { From 4b61cc91ac23081d6b474e27eeb0fa6c604c127c Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 12 Mar 2013 11:00:54 +0800 Subject: [PATCH 3/3] MDL-38410 behat: Adding basic step and test for mod_choice --- mod/choice/tests/behat/add_choice.feature | 33 ++++++++++++ mod/choice/tests/behat/behat_mod_choice.php | 59 +++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 mod/choice/tests/behat/add_choice.feature create mode 100644 mod/choice/tests/behat/behat_mod_choice.php diff --git a/mod/choice/tests/behat/add_choice.feature b/mod/choice/tests/behat/add_choice.feature new file mode 100644 index 00000000000..22c42394095 --- /dev/null +++ b/mod/choice/tests/behat/add_choice.feature @@ -0,0 +1,33 @@ +@mod_choice +Feature: Add choice activity + In order to ask questions as a choice of multiple responses + As a moodle teacher + I need to add choice activities to courses + + @javascript + Scenario: Add a choice activity and complete the activity as a student + Given the following "users" exists: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@asd.com | + | student1 | Student | 1 | student1@asd.com | + And the following "courses" exists: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exists: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + And I add a "choice" to section "1" and I fill the form with: + | Choice name | Choice name | + | Description | Choice Description | + | option[0] | Option 1 | + | option[1] | Option 2 | + And I log out + When I log in as "student1" + And I follow "Course 1" + And I choose "Option 1" from "Choice name" choice activity + Then I should see "Your selection: Option 1" + And I should see "Your choice has been saved" diff --git a/mod/choice/tests/behat/behat_mod_choice.php b/mod/choice/tests/behat/behat_mod_choice.php new file mode 100644 index 00000000000..286e41e92c7 --- /dev/null +++ b/mod/choice/tests/behat/behat_mod_choice.php @@ -0,0 +1,59 @@ +. + +/** + * Steps definitions for choice activity. + * + * @package mod_choice + * @category test + * @copyright 2013 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'); + +use Behat\Behat\Context\Step\Given as Given; + +/** + * Choice activity definitions. + * + * @package mod_choice + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_choice extends behat_base { + + /** + * Chooses the specified option from the choice activity named as specified. You should be located in the activity's course page. + * + * @Given /^I choose "(?P(?:[^"]|\\")*)" from "(?P(?:[^"]|\\")*)" choice activity$/ + * @param string $option + * @param string $choiceactivity + */ + public function I_choose_option_from_activity($option, $choiceactivity) { + + // Escaping again the strings as backslashes have been removed by the automatic transformation. + return array( + new Given('I follow "' . $this->escape($choiceactivity) . '"'), + new Given('I select "' . $this->escape($option) . '" radio button'), + new Given('I press "Save my choice"') + ); + } + +}