From eb9ca848fa418032ced937c1c4ebccb194051d38 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Fri, 18 Mar 2016 12:12:42 +0800 Subject: [PATCH] MDL-53381 behat: Replaced chained steps with api. Behat 3.x doesn't support chained steps and hence this is first step to get rid of chained steps in core. --- auth/tests/behat/behat_auth.php | 32 +- blocks/tests/behat/behat_blocks.php | 30 +- calendar/tests/behat/behat_calendar.php | 41 ++- cohort/tests/behat/behat_cohort.php | 31 +- completion/tests/behat/behat_completion.php | 49 +-- course/tests/behat/behat_course.php | 287 +++++++++++------- enrol/tests/behat/behat_enrol.php | 53 ++-- .../tests/behat/behat_gradingform_guide.php | 13 +- .../tests/behat/behat_gradingform_rubric.php | 16 +- grade/grading/tests/behat/behat_grading.php | 83 +++-- .../tests/behat/behat_gradereport_grader.php | 27 +- grade/tests/behat/behat_grade.php | 76 ++--- group/tests/behat/behat_groups.php | 7 +- lib/behat/behat_base.php | 119 +++++++- lib/tests/behat/behat_deprecated.php | 2 - lib/tests/behat/behat_forms.php | 15 +- lib/tests/behat/behat_general.php | 6 +- lib/tests/behat/behat_hooks.php | 78 +---- lib/tests/behat/behat_navigation.php | 18 +- lib/tests/behat/behat_permissions.php | 57 ++-- message/tests/behat/behat_message.php | 31 +- mod/choice/tests/behat/behat_mod_choice.php | 13 +- mod/data/tests/behat/behat_mod_data.php | 37 +-- .../tests/behat/behat_mod_feedback.php | 38 +-- mod/forum/tests/behat/behat_mod_forum.php | 40 ++- .../tests/behat/behat_mod_glossary.php | 31 +- mod/quiz/tests/behat/behat_mod_quiz.php | 120 ++++---- .../tests/behat/behat_mod_workshop.php | 56 ++-- question/tests/behat/behat_question.php | 16 +- question/tests/behat/behat_question_base.php | 15 +- 30 files changed, 737 insertions(+), 700 deletions(-) diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index 53349b4a3a8..d5ddb59e716 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -28,9 +28,6 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given; -use Moodle\BehatExtension\Context\Step\When as When; - /** * Log in log out steps definitions. * @@ -51,12 +48,11 @@ class behat_auth extends behat_base { $this->getSession()->visit($this->locate_path('login/index.php')); // Enter username and password. - $behatforms = behat_context_helper::get('behat_forms'); - $behatforms->i_set_the_field_to('Username', $this->escape($username)); - $behatforms->i_set_the_field_to('Password', $this->escape($username)); + $this->execute('behat_forms::i_set_the_field_to', array('Username', $this->escape($username))); + $this->execute('behat_forms::i_set_the_field_to', array('Password', $this->escape($username))); - // Press log in button. - $behatforms->press_button(get_string('login')); + // Press log in button, no need to check for exceptions as it will checked after this step execution. + $this->execute('behat_forms::press_button', get_string('login')); } /** @@ -65,22 +61,18 @@ class behat_auth extends behat_base { * @Given /^I log out$/ */ public function i_log_out() { - - $steps = array(new When('I follow "' . get_string('logout') . '"')); - - // No need to check anything else if we run without JS. - if (!$this->running_javascript()) { - return $steps; - } - // There is no longer any need to worry about whether the navigation // bar needs to be expanded; user_menu now lives outside the // hamburger. - // However, the user menu *always* needs to be expanded. - $xpath = "//div[@class='usermenu']//a[contains(concat(' ', @class, ' '), ' toggle-display ')]"; - array_unshift($steps, new When('I click on "'.$xpath.'" "xpath_element"')); + // However, the user menu *always* needs to be expanded. if running JS. + if ($this->running_javascript()) { + $xpath = "//div[@class='usermenu']//a[contains(concat(' ', @class, ' '), ' toggle-display ')]"; + + $this->execute('behat_general::i_click_on', array($xpath, "xpath_element")); + } - return $steps; + // No need to check for exceptions as it will checked after this step execution. + $this->execute('behat_general::click_link', get_string('logout')); } } diff --git a/blocks/tests/behat/behat_blocks.php b/blocks/tests/behat/behat_blocks.php index 2d8143ca38c..37086879b60 100644 --- a/blocks/tests/behat/behat_blocks.php +++ b/blocks/tests/behat/behat_blocks.php @@ -27,8 +27,6 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given; - /** * Blocks management steps definitions. * @@ -46,16 +44,16 @@ class behat_blocks extends behat_base { * @param string $blockname */ public function i_add_the_block($blockname) { - $steps = new Given('I set the field "bui_addblock" to "' . $this->escape($blockname) . '"'); + $this->execute('behat_forms::i_set_the_field_to', + array("bui_addblock", $this->escape($blockname)) + ); // If we are running without javascript we need to submit the form. if (!$this->running_javascript()) { - $steps = array( - $steps, - new Given('I click on "' . get_string('go') . '" "button" in the "#add_block" "css_element"') + $this->execute('behat_general::i_click_on_in_the', + array(get_string('go'), "button", "#add_block", "css_element") ); } - return $steps; } /** @@ -63,13 +61,14 @@ class behat_blocks extends behat_base { * * @Given /^I dock "(?P(?:[^"]|\\")*)" block$/ * @param string $blockname - * @return Given */ public function i_dock_block($blockname) { // Looking for both title and alt. $xpath = "//input[@type='image'][@title='" . get_string('dockblock', 'block', $blockname) . "' or @alt='" . get_string('addtodock', 'block') . "']"; - return new Given('I click on " ' . $xpath . '" "xpath_element" in the "' . $this->escape($blockname) . '" "block"'); + $this->execute('behat_general::i_click_on_in_the', + array($xpath, "xpath_element", $this->escape($blockname), "block") + ); } /** @@ -78,7 +77,6 @@ class behat_blocks extends behat_base { * @Given /^I open the "(?P(?:[^"]|\\")*)" blocks action menu$/ * @throws DriverException The step is not available when Javascript is disabled * @param string $blockname - * @return Given */ public function i_open_the_blocks_action_menu($blockname) { @@ -93,7 +91,9 @@ class behat_blocks extends behat_base { return; } - return new Given('I click on "a[role=\'menuitem\']" "css_element" in the "' . $this->escape($blockname) . '" "block"'); + $this->execute('behat_general::i_click_on_in_the', + array("a[role='menuitem']", "css_element", $this->escape($blockname), "block") + ); } /** @@ -106,9 +106,11 @@ class behat_blocks extends behat_base { */ public function i_configure_the_block($blockname) { // Note that since $blockname may be either block name or CSS class, we can not use the exact label of "Configure" link. - return array( - new Given('I open the "'.$this->escape($blockname).'" blocks action menu'), - new Given('I click on "Configure" "link" in the "'.$this->escape($blockname).'" "block"') + + $this->execute("behat_blocks::i_open_the_blocks_action_menu", $this->escape($blockname)); + + $this->execute('behat_general::i_click_on_in_the', + array("Configure", "link", $this->escape($blockname), "block") ); } } diff --git a/calendar/tests/behat/behat_calendar.php b/calendar/tests/behat/behat_calendar.php index 9dc24a251e6..d485235dc45 100644 --- a/calendar/tests/behat/behat_calendar.php +++ b/calendar/tests/behat/behat_calendar.php @@ -26,7 +26,6 @@ // NOTE: no MOODLE_INTERNAL used, this file may be required by behat before including /config.php. require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given; use Behat\Gherkin\Node\TableNode as TableNode; /** @@ -44,17 +43,13 @@ class behat_calendar extends behat_base { * * @Given /^I create a calendar event with form data:$/ * @param TableNode $data - * @return array the list of actions to perform */ public function i_create_a_calendar_event_with_form_data($data) { - // Get the event name. - $eventname = $data->getRow(1); - $eventname = $eventname[1]; + // Go to current month page. + $this->execute("behat_general::click_link", get_string('monththis', 'calendar')); - return array( - new Given('I follow "' . get_string('monththis', 'calendar') . '"'), - new Given('I create a calendar event:', $data), - ); + // Create event. + $this->i_create_a_calendar_event($data); } /** @@ -62,19 +57,24 @@ class behat_calendar extends behat_base { * * @Given /^I create a calendar event:$/ * @param TableNode $data - * @return array the list of actions to perform */ public function i_create_a_calendar_event($data) { // Get the event name. $eventname = $data->getRow(1); $eventname = $eventname[1]; - return array( - new Given('I click on "' . get_string('newevent', 'calendar') .'" "button"'), - new Given('I set the following fields to these values:', $data), - new Given('I press "' . get_string('savechanges') . '"'), - new Given('I should see "' . $eventname . '"') - ); + // Click to create new event. + $this->execute("behat_general::i_click_on", array(get_string('newevent', 'calendar'), "button")); + + // Set form fields. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + + // Save event. + $this->execute("behat_forms::press_button", get_string('savechanges')); + + // Check if event is created. Being last step, don't need to wait or check for exceptions. + $this->execute("behat_general::assert_page_contains_text", $eventname); + } /** @@ -82,7 +82,6 @@ class behat_calendar extends behat_base { * * @Given /^I hover over day "(?P\d+)" of this month in the calendar$/ * @param int $day The day of the current month - * @return Given[] */ public function i_hover_over_day_of_this_month_in_calendar($day) { $summarytitle = get_string('calendarheading', 'calendar', userdate(time(), get_string('strftimemonthyear'))); @@ -94,16 +93,16 @@ class behat_calendar extends behat_base { $daycontains = "text()[contains(concat(' ', normalize-space(.), ' '), ' {$day} ')]"; $daycell = "td[{$cellclasses}]"; $dayofmonth = "a[{$daycontains}]"; - return array( - new Given('I hover "//' . $currentmonth . '/descendant::' . $daycell . '/' . $dayofmonth . '" "xpath_element"'), - ); + + $xpath = '//' . $currentmonth . '/descendant::' . $daycell . '/' . $dayofmonth; + $this->execute("behat_general::i_hover", array($xpath, "xpath_element")); + } /** * Hover over today in the calendar. * * @Given /^I hover over today in the calendar$/ - * @return Given[] */ public function i_hover_over_today_in_the_calendar() { // For window's compatibility, using %d and not %e. diff --git a/cohort/tests/behat/behat_cohort.php b/cohort/tests/behat/behat_cohort.php index 6a18092d424..aad2b9c1b2f 100644 --- a/cohort/tests/behat/behat_cohort.php +++ b/cohort/tests/behat/behat_cohort.php @@ -27,8 +27,6 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given; - /** * Steps definitions for cohort actions. * @@ -48,13 +46,6 @@ class behat_cohort extends behat_base { */ public function i_add_user_to_cohort_members($user, $cohortidnumber) { - $steps = array( - new Given('I click on "' . get_string('assign', 'cohort') . '" "link" in the "' . $this->escape($cohortidnumber) . '" "table_row"'), - new Given('I set the field "' . get_string('potusers', 'cohort') . '" to "' . $this->escape($user) . '"'), - new Given('I press "' . get_string('add') . '"'), - new Given('I press "' . get_string('backtocohorts', 'cohort') . '"') - ); - // If we are not in the cohorts management we should move there before anything else. if (!$this->getSession()->getPage()->find('css', 'input#cohort_search_q')) { @@ -62,15 +53,23 @@ class behat_cohort extends behat_base { $parentnodes = get_string('administrationsite') . ' > ' . get_string('users', 'admin') . ' > ' . get_string('accounts', 'admin'); - $steps = array_merge( - array( - new Given('I am on homepage'), - new Given('I navigate to "' . get_string('cohorts', 'cohort') . '" node in "' . $parentnodes . '"') - ), - $steps + + $this->execute("behat_general::i_am_on_homepage"); + $this->execute("behat_navigation::i_navigate_to_node_in", + array(get_string('cohorts', 'cohort'), $parentnodes) ); } - return $steps; + $this->execute('behat_general::i_click_on_in_the', + array(get_string('assign', 'cohort'), "link", $this->escape($cohortidnumber), "table_row") + ); + + $this->execute("behat_forms::i_set_the_field_to", + array(get_string('potusers', 'cohort'), $this->escape($user)) + ); + + $this->execute("behat_forms::press_button", get_string('add')); + $this->execute("behat_forms::press_button", get_string('backtocohorts', 'cohort')); + } } diff --git a/completion/tests/behat/behat_completion.php b/completion/tests/behat/behat_completion.php index 0bd4e809f05..7b687f0370a 100644 --- a/completion/tests/behat/behat_completion.php +++ b/completion/tests/behat/behat_completion.php @@ -27,9 +27,7 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given, - Moodle\BehatExtension\Context\Step\Then, - Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; +use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** * Steps definitions to deal with course and activities completion. @@ -55,9 +53,9 @@ class behat_completion extends behat_base { $xpath = "//table[@id='completion-progress']" . "/descendant::img[contains(@title, $titleliteral)]"; - return array( - new Given('I go to the current course activity completion report'), - new Then('"' . $this->escape($xpath) . '" "xpath_element" should exist') + $this->execute("behat_completion::go_to_the_current_course_activity_completion_report"); + $this->execute("behat_general::should_exist", + array($this->escape($xpath), "xpath_element") ); } @@ -74,12 +72,9 @@ class behat_completion extends behat_base { $titleliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname . ", " . $activityname . ": Not completed"); $xpath = "//table[@id='completion-progress']" . "/descendant::img[contains(@title, $titleliteral)]"; - return array( - new Given('I go to the current course activity completion report'), - new Then('"' . $this->escape($xpath) . '" "xpath_element" should exist') - ); - return $steps; + $this->execute("behat_completion::go_to_the_current_course_activity_completion_report"); + $this->execute("behat_general::should_exist", array($this->escape($xpath), "xpath_element")); } /** @@ -91,7 +86,7 @@ class behat_completion extends behat_base { $completionnode = get_string('pluginname', 'report_progress'); $reportsnode = get_string('courseadministration') . ' > ' . get_string('reports'); - return new Given('I navigate to "' . $completionnode . '" node in "' . $reportsnode . '"'); + $this->execute("behat_navigation::i_navigate_to_node_in", array($completionnode, $reportsnode)); } /** @@ -104,18 +99,21 @@ class behat_completion extends behat_base { $toggle = strtolower($completionstatus) == 'enabled' ? get_string('yes') : get_string('no'); - return array( - new Given('I follow "'.get_string('editsettings').'"'), - new Given('I set the field "'.get_string('enablecompletion', 'completion').'" to "'.$toggle.'"'), - new Given('I press "'.get_string('savechangesanddisplay').'"') - ); + // Go to course editing. + $this->execute("behat_general::click_link", get_string('editsettings')); + + // Enable completion. + $this->execute("behat_forms::i_set_the_field_to", + array(get_string('enablecompletion', 'completion'), $toggle)); + + // Save course settings. + $this->execute("behat_forms::press_button", get_string('savechangesanddisplay')); } /** * Checks if the activity with specified name is maked as complete. * * @Given /^the "(?P(?:[^"]|\\")*)" "(?P(?:[^"]|\\")*)" activity with "(manual|auto)" completion should be marked as complete$/ - * @return array */ public function activity_marked_as_complete($activityname, $activitytype, $completiontype) { if ($completiontype == "manual") { @@ -125,15 +123,17 @@ class behat_completion extends behat_base { } $csselementforactivitytype = "li.modtype_".strtolower($activitytype); - return new Given('"//img[contains(@alt, \''.$imgalttext.'\')]" "xpath_element" ' . - 'should exist in the "'.$csselementforactivitytype.'" "css_element"'); + $xpathtocheck = "//img[contains(@alt, '$imgalttext')]"; + $this->execute("behat_general::should_exist_in_the", + array($xpathtocheck, "xpath_element", $csselementforactivitytype, "css_element") + ); + } /** * Checks if the activity with specified name is maked as complete. * * @Given /^the "(?P(?:[^"]|\\")*)" "(?P(?:[^"]|\\")*)" activity with "(manual|auto)" completion should be marked as not complete$/ - * @return array */ public function activity_marked_as_not_complete($activityname, $activitytype, $completiontype) { if ($completiontype == "manual") { @@ -143,7 +143,10 @@ class behat_completion extends behat_base { } $csselementforactivitytype = "li.modtype_".strtolower($activitytype); - return new Given('"//img[contains(@alt, \''.$imgalttext.'\')]" "xpath_element" ' . - 'should exist in the "'.$csselementforactivitytype.'" "css_element"'); + $xpathtocheck = "//img[contains(@alt, '$imgalttext')]"; + $this->execute("behat_general::should_exist_in_the", + array($xpathtocheck, "xpath_element", $csselementforactivitytype, "css_element") + ); + } } diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index d16aa1a9433..733c1dcedc2 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode, +use Behat\Gherkin\Node\TableNode as TableNode, Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\DriverException as DriverException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; @@ -48,7 +47,8 @@ class behat_course extends behat_base { * @Given /^I turn editing mode on$/ */ public function i_turn_editing_mode_on() { - return new Given('I press "' . get_string('turneditingon') . '"'); + + $this->execute("behat_forms::press_button", get_string('turneditingon')); } /** @@ -56,7 +56,8 @@ class behat_course extends behat_base { * @Given /^I turn editing mode off$/ */ public function i_turn_editing_mode_off() { - return new Given('I press "' . get_string('turneditingoff') . '"'); + + $this->execute("behat_forms::press_button", get_string('turneditingoff')); } /** @@ -64,16 +65,21 @@ class behat_course extends behat_base { * * @Given /^I create a course with:$/ * @param TableNode $table The course data - * @return Given[] */ public function i_create_a_course_with(TableNode $table) { - $steps = array( - new Given('I go to the courses management page'), - new Given('I should see the "'.get_string('categories').'" management page'), - new Given('I click on category "'.get_string('miscellaneous').'" in the management interface'), - new Given('I should see the "'.get_string('categoriesandcoures').'" management page'), - new Given('I click on "'.get_string('createnewcourse').'" "link" in the "#course-listing" "css_element"') + // Go to course management page. + $this->i_go_to_the_courses_management_page(); + // Ensure you are on course management page. + $this->execute("behat_course::i_should_see_the_courses_management_page", get_string('categories')); + + // Select Miscellaneous category. + $this->i_click_on_category_in_the_management_interface(get_string('miscellaneous')); + $this->execute("behat_course::i_should_see_the_courses_management_page", get_string('categoriesandcoures')); + + // Click create new course. + $this->execute('behat_general::i_click_on_in_the', + array(get_string('createnewcourse'), "link", "#course-listing", "css_element") ); // If the course format is one of the fields we change how we @@ -101,29 +107,36 @@ class behat_course extends behat_base { // Adding a forced wait until editors are loaded as otherwise selenium sometimes tries clicks on the // format field when the editor is being rendered and the click misses the field coordinates. - $steps[] = new Given('I expand all fieldsets'); - $steps[] = new Given('I set the field "' . $formatfield . '" to "' . $formatvalue . '"'); - $steps[] = new Given('I set the following fields to these values:', $table); - } else { - $steps[] = new Given('I set the following fields to these values:', $table); + $this->execute("behat_forms::i_expand_all_fieldsets"); + + $this->execute("behat_forms::i_set_the_field_to", array($formatfield, $formatvalue)); } - $steps[] = new Given('I press "' . get_string('savechangesanddisplay') . '"'); + // Set form fields. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table); + + // Save course settings. + $this->execute("behat_forms::press_button", get_string('savechangesanddisplay')); - return $steps; } /** * Goes to the system courses/categories management page. * * @Given /^I go to the courses management page$/ - * @return Given[] */ public function i_go_to_the_courses_management_page() { - return array( - new Given('I am on homepage'), - new Given('I navigate to "' . get_string('coursemgmt', 'admin') . '" node in "' . get_string('administrationsite') . ' > ' . get_string('courses', 'admin') . '"') + + $parentnodes = get_string('administrationsite') . ' > ' . get_string('courses', 'admin'); + + // Go to home page. + $this->execute("behat_general::i_am_on_homepage"); + + // Navigate to course management page via navigation block. + $this->execute("behat_navigation::i_navigate_to_node_in", + array(get_string('coursemgmt', 'admin'), $parentnodes) ); + } /** @@ -133,16 +146,22 @@ class behat_course extends behat_base { * @param string $activity The activity name * @param int $section The section number * @param TableNode $data The activity field/value data - * @return Given[] */ public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) { - return array( - new Given('I add a "' . $this->escape($activity) . '" to section "' . $this->escape($section) . '"'), - new Given('I wait to be redirected'), - new Given('I set the following fields to these values:', $data), - new Given('I press "' . get_string('savechangesandreturntocourse') . '"') + // Add activity to section. + $this->execute("behat_course::i_add_to_section", + array($this->escape($activity), $this->escape($section)) ); + + // Wait to be redirected. + $this->execute('behat_general::wait_until_the_page_is_ready'); + + // Set form fields. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + + // Save course settings. + $this->execute("behat_forms::press_button", get_string('savechangesandreturntocourse')); } /** @@ -234,7 +253,6 @@ class behat_course extends behat_base { * * @Given /^I delete section "(?P\d+)"$/ * @param int $sectionnumber The section number - * @return Given[] */ public function i_delete_section($sectionnumber) { // Ensures the section exists. @@ -253,7 +271,11 @@ class behat_course extends behat_base { $this->i_open_section_edit_menu($sectionnumber); } - return new Given('I click on "' . $strdelete . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"'); + // Click on delete link. + $this->execute('behat_general::i_click_on_in_the', + array($strdelete, "link", $this->escape($xpath), "xpath_element") + ); + } /** @@ -261,7 +283,6 @@ class behat_course extends behat_base { * * @Given /^I turn section "(?P\d+)" highlighting on$/ * @param int $sectionnumber The section number - * @return Given[] */ public function i_turn_section_highlighting_on($sectionnumber) { @@ -273,7 +294,10 @@ class behat_course extends behat_base { $this->i_open_section_edit_menu($sectionnumber); } - return new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"'); + // Click on highlight topic link. + $this->execute('behat_general::i_click_on_in_the', + array(get_string('markthistopic'), "link", $this->escape($xpath), "xpath_element") + ); } /** @@ -281,7 +305,6 @@ class behat_course extends behat_base { * * @Given /^I turn section "(?P\d+)" highlighting off$/ * @param int $sectionnumber The section number - * @return Given[] */ public function i_turn_section_highlighting_off($sectionnumber) { @@ -293,7 +316,10 @@ class behat_course extends behat_base { $this->i_open_section_edit_menu($sectionnumber); } - return new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $this->escape($xpath) . '" "xpath_element"'); + // Click on un-highlight topic link. + $this->execute('behat_general::i_click_on_in_the', + array(get_string('markedthistopic'), "link", $this->escape($xpath), "xpath_element") + ); } /** @@ -358,7 +384,11 @@ class behat_course extends behat_base { $stredit = get_string('editsection'); } - return new Given('I click on "' . $stredit . '" "link" in the "#section-' . $sectionnumber . '" "css_element"'); + // Click on un-highlight topic link. + $this->execute('behat_general::i_click_on_in_the', + array($stredit, "link", "#section-" . $sectionnumber, "css_element") + ); + } /** @@ -367,15 +397,17 @@ class behat_course extends behat_base { * @When /^I edit the section "(?P\d+)" and I fill the form with:$/ * @param int $sectionnumber The section number * @param TableNode $data The activity field/value data - * @return Given[] */ public function i_edit_the_section_and_i_fill_the_form_with($sectionnumber, TableNode $data) { - return array( - new Given('I edit the section "' . $sectionnumber . '"'), - new Given('I set the following fields to these values:', $data), - new Given('I press "' . get_string('savechanges') . '"') - ); + // Edit given section. + $this->execute("behat_course::i_edit_the_section"); + + // Set form fields. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + + // Save section settings. + $this->execute("behat_forms::press_button", get_string('savechanges')); } /** @@ -632,7 +664,6 @@ class behat_course extends behat_base { * @Given /^I move "(?P(?:[^"]|\\")*)" activity to section "(?P\d+)"$/ * @param string $activityname The activity name * @param int $sectionnumber The number of section - * @return Given[] */ public function i_move_activity_to_section($activityname, $sectionnumber) { @@ -646,18 +677,21 @@ class behat_course extends behat_base { $destinationxpath = $sectionxpath . "/descendant::ul[contains(concat(' ', normalize-space(@class), ' '), ' yui3-dd-drop ')]"; - return array( - new Given('I drag "' . $this->escape($activitynode->getXpath()) . '" "xpath_element" ' . - 'and I drop it in "' . $this->escape($destinationxpath) . '" "xpath_element"'), + $this->execute("behat_general::i_drag_and_i_drop_it_in", + array($this->escape($activitynode->getXpath()), "xpath_element", + $this->escape($destinationxpath), "xpath_element") ); } else { // Following links with no-JS. // Moving to the fist spot of the section (before all other section's activities). - return array( - new Given('I click on "a.editing_move" "css_element" in the "' . $this->escape($activityname) . '" activity'), - new Given('I click on "li.movehere a" "css_element" in the "' . $this->escape($sectionxpath) . '" "xpath_element"'), + $this->execute('behat_course::i_click_on_in_the_activity', + array("a.editing_move", "css_element", $this->escape($activityname)) + ); + + $this->execute('behat_general::i_click_on_in_the', + array("li.movehere a", "css_element", $this->escape($sectionxpath), "xpath_element") ); } } @@ -669,7 +703,6 @@ class behat_course extends behat_base { * @throws DriverException Step not available when Javascript is disabled * @param string $activityname * @param string $newactivityname - * @return Given[] */ public function i_change_activity_name_to($activityname, $newactivityname) { @@ -677,12 +710,17 @@ class behat_course extends behat_base { throw new DriverException('Change activity name step is not available with Javascript disabled'); } - // Adding chr(10) to save changes. $activity = $this->escape($activityname); - return array( - new Given('I click on "' . get_string('edittitle') . '" "link" in the "' . $activity .'" activity'), - new Given('I set the field "title" to "' . $this->escape($newactivityname) . chr(10) . '"') + + $this->execute('behat_course::i_click_on_in_the_activity', + array(get_string('edittitle'), "link", $activity) ); + + // Adding chr(10) to save changes. + $this->execute('behat_forms::i_set_the_field_to', + array('title', $this->escape($newactivityname) . chr(10)) + ); + } /** @@ -691,7 +729,6 @@ class behat_course extends behat_base { * @Given /^I open "(?P(?:[^"]|\\")*)" actions menu$/ * @throws DriverException The step is not available when Javascript is disabled * @param string $activityname - * @return Given */ public function i_open_actions_menu($activityname) { @@ -706,7 +743,10 @@ class behat_course extends behat_base { return; } - return new Given('I click on "a[role=\'menuitem\']" "css_element" in the "' . $this->escape($activityname) . '" activity'); + $this->execute('behat_course::i_click_on_in_the_activity', + array("a[role='menuitem']", "css_element", $this->escape($activityname)) + ); + } /** @@ -715,7 +755,6 @@ class behat_course extends behat_base { * @Given /^I close "(?P(?:[^"]|\\")*)" actions menu$/ * @throws DriverException The step is not available when Javascript is disabled * @param string $activityname - * @return Given */ public function i_close_actions_menu($activityname) { @@ -730,7 +769,9 @@ class behat_course extends behat_base { return; } - return new Given('I click on "a[role=\'menuitem\']" "css_element" in the "' . $this->escape($activityname) . '" activity'); + $this->execute('behat_course::i_click_on_in_the_activity', + array("a[role='menuitem']", "css_element", $this->escape($activityname)) + ); } /** @@ -752,8 +793,6 @@ class behat_course extends behat_base { if (empty($classes['action-menu-shown'])) { throw new ExpectationException(sprintf("The action menu for '%s' is not open", $activityname), $this->getSession()); } - - return; } /** @@ -761,18 +800,18 @@ class behat_course extends behat_base { * * @Given /^I indent right "(?P(?:[^"]|\\")*)" activity$/ * @param string $activityname - * @return Given[] */ public function i_indent_right_activity($activityname) { - $steps = array(); $activity = $this->escape($activityname); if ($this->running_javascript()) { - $steps[] = new Given('I open "' . $activity . '" actions menu'); + $this->i_open_actions_menu($activity); } - $steps[] = new Given('I click on "' . get_string('moveright') . '" "link" in the "' . $activity . '" activity'); - return $steps; + $this->execute('behat_course::i_click_on_in_the_activity', + array(get_string('moveright'), "link", $this->escape($activity)) + ); + } /** @@ -780,18 +819,17 @@ class behat_course extends behat_base { * * @Given /^I indent left "(?P(?:[^"]|\\")*)" activity$/ * @param string $activityname - * @return Given[] */ public function i_indent_left_activity($activityname) { - $steps = array(); $activity = $this->escape($activityname); if ($this->running_javascript()) { - $steps[] = new Given('I open "' . $activity . '" actions menu'); + $this->i_open_actions_menu($activity); } - $steps[] = new Given('I click on "' . get_string('moveleft') . '" "link" in the "' . $activity . '" activity'); - return $steps; + $this->execute('behat_course::i_click_on_in_the_activity', + array(get_string('moveleft'), "link", $this->escape($activity)) + ); } @@ -800,23 +838,27 @@ class behat_course extends behat_base { * * @Given /^I delete "(?P(?:[^"]|\\")*)" activity$/ * @param string $activityname - * @return Given[] */ public function i_delete_activity($activityname) { $steps = array(); $activity = $this->escape($activityname); if ($this->running_javascript()) { - $steps[] = new Given('I open "' . $activity . '" actions menu'); + $this->i_open_actions_menu($activity); } - $steps[] = new Given('I click on "' . get_string('delete') . '" "link" in the "' . $activity . '" activity'); + + $this->execute('behat_course::i_click_on_in_the_activity', + array(get_string('delete'), "link", $this->escape($activity)) + ); // JS enabled. // Not using chain steps here because the exceptions catcher have problems detecting // JS modal windows and avoiding interacting them at the same time. if ($this->running_javascript()) { - $steps[] = new Given('I click on "' . get_string('yes') . '" "button" in the "Confirm" "dialogue"'); + $this->execute('behat_general::i_click_on_in_the', + array(get_string('yes'), "button", "Confirm", "dialogue") + ); } else { - $steps[] = new Given('I press "' . get_string('yes') . '"'); + $this->execute("behat_forms::press_button", get_string('yes')); } return $steps; @@ -827,16 +869,17 @@ class behat_course extends behat_base { * * @Given /^I duplicate "(?P(?:[^"]|\\")*)" activity$/ * @param string $activityname - * @return Given[] */ public function i_duplicate_activity($activityname) { $steps = array(); $activity = $this->escape($activityname); if ($this->running_javascript()) { - $steps[] = new Given('I open "' . $activity . '" actions menu'); + $this->i_open_actions_menu($activity); } - $steps[] = new Given('I click on "' . get_string('duplicate') . '" "link" in the "' . $activity . '" activity'); - return $steps; + $this->execute('behat_course::i_click_on_in_the_activity', + array(get_string('duplicate'), "link", $activity) + ); + } /** @@ -845,16 +888,13 @@ class behat_course extends behat_base { * @Given /^I duplicate "(?P(?:[^"]|\\")*)" activity editing the new copy with:$/ * @param string $activityname * @param TableNode $data - * @return Given[] */ public function i_duplicate_activity_editing_the_new_copy_with($activityname, TableNode $data) { - $steps = array(); - $activity = $this->escape($activityname); $activityliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($activityname); - $steps[] = new Given('I duplicate "' . $activity . '" activity'); + $this->execute("behat_course::i_duplicate_activity", $activity); // Determine the future new activity xpath from the former one. $duplicatedxpath = "//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')]" . @@ -868,24 +908,28 @@ class behat_course extends behat_base { "/ancestor::li[contains(concat(' ', normalize-space(@class), ' '), ' section ')]" . "/descendant::div[contains(concat(' ', @class, ' '), ' lightbox ')][contains(@style, 'display: none')]"; - $steps[] = new Given('I wait until the page is ready'); - $steps[] = new Given('I wait until "' . $this->escape($hiddenlightboxxpath) .'" "xpath_element" exists'); + $this->execute("behat_general::wait_until_exists", + array($this->escape($hiddenlightboxxpath), "xpath_element") + ); // Close the original activity actions menu. - $steps[] = new Given('I close "' . $activity . '" actions menu'); + $this->i_close_actions_menu($activity); // The next sibling of the former activity will be the duplicated one, so we click on it from it's xpath as, at // this point, it don't even exists in the DOM (the steps are executed when we return them). - $steps[] = new Given('I click on "' . $this->escape($duplicatedactionsmenuxpath) . '" "xpath_element"'); + $this->execute('behat_general::i_click_on', + array($this->escape($duplicatedactionsmenuxpath), "xpath_element") + ); } // We force the xpath as otherwise mink tries to interact with the former one. - $steps[] = new Given('I click on "' . get_string('editsettings') . '" "link" in the "' . - $this->escape($duplicatedxpath) . '" "xpath_element"'); + $this->execute('behat_general::i_click_on_in_the', + array(get_string('editsettings'), "link", $this->escape($duplicatedxpath), "xpath_element") + ); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + $this->execute("behat_forms::press_button", get_string('savechangesandreturntocourse')); - $steps[] = new Given('I set the following fields to these values:', $data); - $steps[] = new Given('I press "' . get_string('savechangesandreturntocourse') . '"'); - return $steps; } /** @@ -1280,14 +1324,16 @@ class behat_course extends behat_base { * * @Given /^I move category "(?P(?:[^"]|\\")*)" to top level in the management interface$/ * @param string $name - * @return Given[] */ public function i_move_category_to_top_level_in_the_management_interface($name) { $this->i_select_category_in_the_management_interface($name); - return array( - new Given('I set the field "menumovecategoriesto" to "' . coursecat::get(0)->get_formatted_name() . '"'), - new Given('I press "bulkmovecategories"'), + + $this->execute('behat_forms::i_set_the_field_to', + array('menumovecategoriesto', coursecat::get(0)->get_formatted_name()) ); + + // Save event. + $this->execute("behat_forms::press_button", "bulkmovecategories"); } /** @@ -1532,29 +1578,31 @@ class behat_course extends behat_base { * * @Given /^I should see the "(?P(?:[^"]|\\")*)" management page$/ * @param string $mode The mode to expected. One of 'Courses', 'Course categories' or 'Course categories and courses' - * @return Given[] */ public function i_should_see_the_courses_management_page($mode) { - $return = array( - new Given('I should see "Course and category management" in the "h2" "css_element"') + $this->execute("behat_general::assert_element_contains_text", + array("Course and category management", "h2", "css_element") ); + switch ($mode) { case "Courses": - $return[] = new Given('"#category-listing" "css_element" should not exist'); - $return[] = new Given('"#course-listing" "css_element" should exist'); + $this->execute("behat_general::should_not_exist", array("#category-listing", "css_element")); + $this->execute("behat_general::should_exist", array("#course-listing", "css_element")); break; + case "Course categories": - $return[] = new Given('"#category-listing" "css_element" should exist'); - $return[] = new Given('"#course-listing" "css_element" should exist'); + $this->execute("behat_general::should_exist", array("#category-listing", "css_element")); + $this->execute("behat_general::should_exist", array("#course-listing", "css_element")); break; + case "Courses categories and courses": default: - $return[] = new Given('"#category-listing" "css_element" should exist'); - $return[] = new Given('"#course-listing" "css_element" should exist'); + $this->execute("behat_general::should_exist", array("#category-listing", "css_element")); + $this->execute("behat_general::should_exist", array("#course-listing", "css_element")); break; } - $return[] = new Given('"#course-detail" "css_element" should not exist'); - return $return; + + $this->execute("behat_general::should_not_exist", array("#course-detail", "css_element")); } /** @@ -1562,13 +1610,30 @@ class behat_course extends behat_base { * * @Given /^I should see the "(?P(?:[^"]|\\")*)" management page with a course selected$/ * @param string $mode The mode to expected. One of 'Courses', 'Course categories' or 'Course categories and courses' - * @return Given[] */ public function i_should_see_the_courses_management_page_with_a_course_selected($mode) { - $return = $this->i_should_see_the_courses_management_page($mode); - array_pop($return); - $return[] = new Given('"#course-detail" "css_element" should exist'); - return $return; + $this->execute("behat_general::assert_element_contains_text", + array("Course and category management", "h2", "css_element")); + + switch ($mode) { + case "Courses": + $this->execute("behat_general::should_not_exist", array("#category-listing", "css_element")); + $this->execute("behat_general::should_exist", array("#course-listing", "css_element")); + break; + + case "Course categories": + $this->execute("behat_general::should_exist", array("#category-listing", "css_element")); + $this->execute("behat_general::should_exist", array("#course-listing", "css_element")); + break; + + case "Courses categories and courses": + default: + $this->execute("behat_general::should_exist", array("#category-listing", "css_element")); + $this->execute("behat_general::should_exist", array("#course-listing", "css_element")); + break; + } + + $this->execute("behat_general::should_exist", array("#course-detail", "css_element")); } /** diff --git a/enrol/tests/behat/behat_enrol.php b/enrol/tests/behat/behat_enrol.php index 034898ce449..5c273e7304e 100644 --- a/enrol/tests/behat/behat_enrol.php +++ b/enrol/tests/behat/behat_enrol.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; /** * Steps definitions for general enrolment actions. @@ -48,14 +47,23 @@ class behat_enrol extends behat_base { * @param TableNode $table */ public function i_add_enrolment_method_with($enrolmethod, TableNode $table) { - return array( - new Given('I expand "' . get_string('users', 'admin') . '" node'), - new Given('I follow "' . get_string('type_enrol_plural', 'plugin') . '"'), - new Given('I select "' . $this->escape($enrolmethod) . '" from the "' . - get_string('addinstance', 'enrol') . '" singleselect'), - new Given('I set the following fields to these values:', $table), - new Given('I press "' . get_string('addinstance', 'enrol') . '"'), + // Navigate to enrolment method page. + $parentnodes = get_string('courseadministration') . ' > ' . get_string('users', 'admin'); + $this->execute("behat_navigation::i_navigate_to_node_in", + array(get_string('type_enrol_plural', 'plugin'), $parentnodes) ); + + // Select enrolment method. + $this->execute('behat_forms::i_select_from_the_singleselect', + array($this->escape($enrolmethod), get_string('addinstance', 'enrol')) + ); + + // Set form fields. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table); + + // Save changes. + $this->execute("behat_forms::press_button", get_string('addinstance', 'enrol')); + } /** @@ -67,33 +75,34 @@ class behat_enrol extends behat_base { * @Given /^I enrol "(?P(?:[^"]|\\")*)" user as "(?P(?:[^"]|\\")*)"$/ * @param string $userfullname * @param string $rolename - * @return Given[] */ public function i_enrol_user_as($userfullname, $rolename) { - $steps = array( - new Given('I follow "' . get_string('enrolledusers', 'enrol') . '"'), - new Given('I press "' . get_string('enrolusers', 'enrol') . '"') + // Navigate to enrolment page. + $parentnodes = get_string('courseadministration') . ' > ' . get_string('users', 'admin'); + $this->execute("behat_navigation::i_navigate_to_node_in", + array(get_string('enrolledusers', 'enrol'), $parentnodes) ); + $this->execute("behat_forms::press_button", get_string('enrolusers', 'enrol')); + + $this->execute('behat_forms::i_set_the_field_to', array(get_string('assignroles', 'role'), $rolename)); + if ($this->running_javascript()) { // We have a div here, not a tr. $userliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname); $userrowxpath = "//div[contains(concat(' ',normalize-space(@class),' '),' user ')][contains(., $userliteral)]"; - $steps[] = new Given('I set the field "' . get_string('assignroles', 'role') . '" to "' . $rolename . '"'); - $steps[] = new Given('I click on "' . get_string('enrol', 'enrol') . '" "button" in the "' . $userrowxpath . '" "xpath_element"'); - $steps[] = new Given('I press "' . get_string('finishenrollingusers', 'enrol') . '"'); + $this->execute('behat_general::i_click_on_in_the', + array(get_string('enrol', 'enrol'), "button", $userrowxpath, "xpath_element") + ); + $this->execute("behat_forms::press_button", get_string('finishenrollingusers', 'enrol')); } else { - - $steps[] = new Given('I set the field "' . get_string('assignrole', 'role') . '" to "' . $rolename . '"'); - $steps[] = new Given('I set the field "addselect" to "' . $userfullname . '"'); - $steps[] = new Given('I press "add"'); + $this->execute('behat_forms::i_set_the_field_to', array("addselect", $userfullname)); + $this->execute("behat_forms::press_button", "add"); } - - return $steps; } } diff --git a/grade/grading/form/guide/tests/behat/behat_gradingform_guide.php b/grade/grading/form/guide/tests/behat/behat_gradingform_guide.php index e63fc5dd4d1..f368c14787e 100644 --- a/grade/grading/form/guide/tests/behat/behat_gradingform_guide.php +++ b/grade/grading/form/guide/tests/behat/behat_gradingform_guide.php @@ -26,9 +26,6 @@ require_once(__DIR__ . '/../../../../../../lib/behat/behat_base.php'); use Behat\Gherkin\Node\TableNode as TableNode, - Moodle\BehatExtension\Context\Step\Given as Given, - Moodle\BehatExtension\Context\Step\When as When, - Moodle\BehatExtension\Context\Step\Then as Then, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException, Behat\Mink\Exception\ExpectationException as ExpectationException; @@ -162,9 +159,6 @@ class behat_gradingform_guide extends behat_base { $stepusage = '"I grade by filling the rubric with:" step needs you to provide a table where each row is a criterion' . ' and each criterion has 3 different values: | Criterion name | Number of points | Remark text |'; - // To fill with the steps to execute. - $steps = array(); - // First element -> name, second -> points, third -> Remark. foreach ($criteria as $name => $criterion) { @@ -192,12 +186,11 @@ class behat_gradingform_guide extends behat_base { if ($criterionid) { $criterionroot = 'advancedgrading[criteria]' . '[' . $criterionid . ']'; - $steps[] = new Given('I set the field "' . $criterionroot . '[score]' . '" to "' . $points . '"'); - $steps[] = new Given('I set the field "' . $criterionroot . '[remark]' . '" to "' . $criterion[1] . '"'); + $this->execute('behat_forms::i_set_the_field_to', array($criterionroot . '[score]', $points)); + + $this->execute('behat_forms::i_set_the_field_to', array($criterionroot . '[remark]', $criterion[1])); } } - - return $steps; } /** diff --git a/grade/grading/form/rubric/tests/behat/behat_gradingform_rubric.php b/grade/grading/form/rubric/tests/behat/behat_gradingform_rubric.php index 778c7ff1b03..38cea083c16 100644 --- a/grade/grading/form/rubric/tests/behat/behat_gradingform_rubric.php +++ b/grade/grading/form/rubric/tests/behat/behat_gradingform_rubric.php @@ -28,7 +28,6 @@ require_once(__DIR__ . '/../../../../../../lib/behat/behat_base.php'); use Behat\Gherkin\Node\TableNode as TableNode, - Moodle\BehatExtension\Context\Step\Given as Given, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException, Behat\Mink\Exception\ExpectationException as ExpectationException; @@ -225,7 +224,6 @@ class behat_gradingform_rubric extends behat_base { * @param string $currentvalue * @param string $value * @param string $criterionname - * @return Given[] */ public function i_replace_rubric_level_with($currentvalue, $value, $criterionname) { @@ -275,7 +273,6 @@ class behat_gradingform_rubric extends behat_base { * * @throws ExpectationException * @param TableNode $rubric - * @return void */ public function i_grade_by_filling_the_rubric_with(TableNode $rubric) { @@ -284,9 +281,6 @@ class behat_gradingform_rubric extends behat_base { $stepusage = '"I grade by filling the rubric with:" step needs you to provide a table where each row is a criterion' . ' and each criterion has 3 different values: | Criterion name | Number of points | Remark text |'; - // To fill with the steps to execute. - $steps = array(); - // First element -> name, second -> points, third -> Remark. foreach ($criteria as $name => $criterion) { @@ -311,8 +305,9 @@ class behat_gradingform_rubric extends behat_base { // Using in_array() as there are only a few elements. if (!in_array('checked', explode(' ', $levelnode->getAttribute('class')))) { - $steps[] = new Given('I click on "' . $selectedlevelxpath . '" "xpath_element" in the "' . - $this->escape($name) . '" "table_row"'); + $this->execute('behat_general::i_click_on_in_the', + array($selectedlevelxpath, "xpath_element", $this->escape($name), "table_row") + ); } } else { @@ -321,7 +316,6 @@ class behat_gradingform_rubric extends behat_base { $radioxpath = $this->get_criterion_xpath($name) . $selectedlevelxpath . "/descendant::input[@type='radio']"; $radionode = $this->find('xpath', $radioxpath); - // TODO MDL-43738: Change setValue() to use the generic set_value() // which will delegate the process to the field type. $radionode->setValue($radionode->getAttribute('value')); } @@ -330,10 +324,8 @@ class behat_gradingform_rubric extends behat_base { // First we need to get the textarea name, then we can set the value. $textarea = $this->get_node_in_container('css_element', 'textarea', 'table_row', $name); - $steps[] = new Given('I set the field "' . $textarea->getAttribute('name') . '" to "' . $criterion[1] . '"'); + $this->execute('behat_forms::i_set_the_field_to', array($textarea->getAttribute('name'), $criterion[1])); } - - return $steps; } /** diff --git a/grade/grading/tests/behat/behat_grading.php b/grade/grading/tests/behat/behat_grading.php index f76117cb978..fdaff233ce1 100644 --- a/grade/grading/tests/behat/behat_grading.php +++ b/grade/grading/tests/behat/behat_grading.php @@ -27,9 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Behat\Gherkin\Node\TableNode as TableNode, - Moodle\BehatExtension\Context\Step\Given as Given, - Moodle\BehatExtension\Context\Step\When as When; +use Behat\Gherkin\Node\TableNode as TableNode; /** * Generic grading methods step definitions. @@ -46,13 +44,12 @@ class behat_grading extends behat_base { * * @Given /^I go to "(?P(?:[^"]|\\")*)" advanced grading page$/ * @param string $activityname - * @return Given[] */ public function i_go_to_advanced_grading_page($activityname) { - return array( - new Given('I follow "' . $this->escape($activityname) . '"'), - new Given('I follow "' . get_string('gradingmanagement', 'grading') . '"'), - ); + + $this->execute('behat_general::click_link', $this->escape($activityname)); + + $this->execute('behat_general::click_link', get_string('gradingmanagement', 'grading')); } /** @@ -60,7 +57,6 @@ class behat_grading extends behat_base { * * @Given /^I go to "(?P(?:[^"]|\\")*)" advanced grading definition page$/ * @param string $activityname - * @return Given[] */ public function i_go_to_advanced_grading_definition_page($activityname) { @@ -72,10 +68,9 @@ class behat_grading extends behat_base { $definitionxpath = "//a[@class='action']" . "[./descendant::*[contains(., $newactionliteral) or contains(., $editactionliteral)]]"; - return array( - new Given('I go to "' . $this->escape($activityname) . '" advanced grading page'), - new Given('I click on "' . $this->escape($definitionxpath) . '" "xpath_element"'), - ); + $this->execute('behat_grading::i_go_to_advanced_grading_page', $this->escape($activityname)); + + $this->execute("behat_general::i_click_on", array($this->escape($definitionxpath), "xpath_element")); } /** * Goes to the student's advanced grading page. @@ -83,25 +78,25 @@ class behat_grading extends behat_base { * @Given /^I go to "(?P(?:[^"]|\\")*)" "(?P(?:[^"]|\\")*)" activity advanced grading page$/ * @param string $userfullname The user full name including firstname and lastname. * @param string $activityname The activity name - * @return Given[] */ public function i_go_to_activity_advanced_grading_page($userfullname, $activityname) { // Step to access the user grade page from the grading page. $usergradetext = get_string('gradeuser', 'assign', $userfullname); - $gradeuserstep = new Given('I follow "' . $this->escape($usergradetext) . '"'); // Shortcut in case we already are in the grading page. $usergradetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($usergradetext); if ($this->getSession()->getPage()->find('named_partial', array('link', $usergradetextliteral))) { - return $gradeuserstep; + $this->execute('behat_general::click_link', $this->escape($usergradetext)); + + return true; } - return array( - new Given('I follow "' . $this->escape($activityname) . '"'), - new Given('I follow "' . $this->escape(get_string('viewgrading', 'assign')) . '"'), - $gradeuserstep - ); + $this->execute('behat_general::click_link', $this->escape($activityname)); + + $this->execute('behat_general::click_link', $this->escape(get_string('viewgrading', 'assign'))); + + $this->execute('behat_general::click_link', $this->escape($usergradetext)); } /** @@ -109,15 +104,14 @@ class behat_grading extends behat_base { * * @Given /^I publish "(?P(?:[^"]|\\")*)" grading form definition as a public template$/ * @param string $activityname - * @return Given[] */ public function i_publish_grading_form_definition_as_a_public_template($activityname) { - return array( - new Given('I go to "' . $this->escape($activityname) . '" advanced grading page'), - new Given('I click on "' . $this->escape(get_string("manageactionshare", "grading")) . '" "link"'), - new Given('I press "' . get_string('continue') . '"') - ); + $this->execute('behat_grading::i_go_to_advanced_grading_page', $this->escape($activityname)); + + $this->execute("behat_general::i_click_on", array($this->escape(get_string("manageactionshare", "grading")), "link")); + + $this->execute('behat_forms::press_button', get_string('continue')); } /** @@ -126,7 +120,6 @@ class behat_grading extends behat_base { * @Given /^I set "(?P(?:[^"]|\\")*)" activity to use "(?P(?:[^"]|\\")*)" grading form$/ * @param string $activityname * @param string $templatename - * @return Given[] */ public function i_set_activity_to_use_grading_form($activityname, $templatename) { @@ -141,28 +134,29 @@ class behat_grading extends behat_base { $usetemplatexpath = "/a[./descendant::div[text()=$literaltemplate]]|" . "/a[./descendant::div[text()=$literalownform]]"; - return array( - new Given('I go to "' . $this->escape($activityname) . '" advanced grading page'), - new Given('I follow "' . $this->escape(get_string('manageactionclone', 'grading')) . '"'), - new Given('I set the field "' . get_string('searchownforms', 'grading') . '" to "1"'), - new Given('I click on "' . get_string('search') . '" "button" in the "region-main" "region"'), - new Given('I click on "' . $this->escape($usetemplatexpath) . '" "xpath_element" ' . - 'in the "' . $this->escape($templatexpath) . '" "xpath_element"'), - new Given('I press "' . get_string('continue') . '"') + $this->execute('behat_grading::i_go_to_advanced_grading_page', $this->escape($activityname)); + + $this->execute('behat_general::click_link', $this->escape(get_string('manageactionclone', 'grading'))); + $this->execute('behat_forms::i_set_the_field_to', array(get_string('searchownforms', 'grading'), 1)); + $this->execute('behat_general::i_click_on_in_the', + array(get_string('search'), "button", "region-main", "region") + ); + $this->execute('behat_general::i_click_on_in_the', + array($this->escape($usetemplatexpath), "xpath_element", $this->escape($templatexpath), "xpath_element") ); + $this->execute('behat_forms::press_button', get_string('continue')); + } /** * Saves the current page advanced grading form. * * @When /^I save the advanced grading form$/ - * @return When[] */ public function i_save_the_advanced_grading_form() { - return array( - new When('I press "' . get_string('savechanges') . '"'), - new When('I press "' . get_string('continue') . '"') - ); + + $this->execute('behat_forms::press_button', get_string('savechanges')); + $this->execute('behat_forms::press_button', get_string('continue')); } /** @@ -170,12 +164,9 @@ class behat_grading extends behat_base { * * @Given /^I complete the advanced grading form with these values:$/ * @param TableNode $data - * @return Given[] */ public function i_complete_the_advanced_grading_form_with_these_values(TableNode $data) { - return array( - new Given('I set the following fields to these values:', $data), - new Given('I save the advanced grading form') - ); + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + $this->execute('behat_grading::i_save_the_advanced_grading_form'); } } diff --git a/grade/report/grader/tests/behat/behat_gradereport_grader.php b/grade/report/grader/tests/behat/behat_gradereport_grader.php index 3e2bcd1bef6..d1f63ffd87f 100644 --- a/grade/report/grader/tests/behat/behat_gradereport_grader.php +++ b/grade/report/grader/tests/behat/behat_gradereport_grader.php @@ -27,9 +27,7 @@ require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given, - Moodle\BehatExtension\Context\Step\Then, - Behat\Mink\Exception\ExpectationException as ExpectationException, +use Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** @@ -45,12 +43,11 @@ class behat_gradereport_grader extends behat_base { * @Given /^I click on student "([^"]*)" for grade item "([^"]*)"$/ * @param string $student * @param string $itemname - * @return Given */ public function i_click_on_student_and_grade_item($student, $itemname) { $xpath = $this->get_student_and_grade_cell_selector($student, $itemname); - return new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element")); } /** @@ -59,12 +56,11 @@ class behat_gradereport_grader extends behat_base { * @Given /^I click away from student "([^"]*)" and grade item "([^"]*)" value$/ * @param string $student * @param string $itemname - * @return Given */ public function i_click_away_from_student_and_grade_value($student, $itemname) { $xpath = $this->get_student_and_grade_value_selector($student, $itemname); - return new Given('I take focus off "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute('behat_general::i_take_focus_off_field', array($this->escape($xpath), 'xpath_element')); } /** @@ -73,12 +69,11 @@ class behat_gradereport_grader extends behat_base { * @Given /^I click away from student "([^"]*)" and grade item "([^"]*)" feedback$/ * @param string $student * @param string $itemname - * @return Given */ public function i_click_away_from_student_and_grade_feedback($student, $itemname) { $xpath = $this->get_student_and_grade_feedback_selector($student, $itemname); - return new Given('I take focus off "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute('behat_general::i_take_focus_off_field', array($this->escape($xpath), 'xpath_element')); } /** @@ -90,7 +85,6 @@ class behat_gradereport_grader extends behat_base { * @param string $student * @param string $itemname * @param string $value - * @return Then */ public function the_grade_should_match($student, $itemname, $value) { $xpath = $this->get_student_and_grade_value_selector($student, $itemname); @@ -131,12 +125,11 @@ class behat_gradereport_grader extends behat_base { * @Then /^I should see a grade field for "([^"]*)" and grade item "([^"]*)"$/ * @param string $student * @param string $itemname - * @return Then */ public function i_should_see_grade_field($student, $itemname) { $xpath = $this->get_student_and_grade_value_selector($student, $itemname); - return new Then('"' . $this->escape($xpath) . '" "xpath_element" should be visible'); + $this->execute('behat_general::should_be_visible', array($this->escape($xpath), 'xpath_element')); } /** @@ -145,12 +138,11 @@ class behat_gradereport_grader extends behat_base { * @Then /^I should see a feedback field for "([^"]*)" and grade item "([^"]*)"$/ * @param string $student * @param string $itemname - * @return Then */ public function i_should_see_feedback_field($student, $itemname) { $xpath = $this->get_student_and_grade_feedback_selector($student, $itemname); - return new Then('"' . $this->escape($xpath) . '" "xpath_element" should be visible'); + $this->execute('behat_general::should_be_visible', array($this->escape($xpath), 'xpath_element')); } /** @@ -159,12 +151,11 @@ class behat_gradereport_grader extends behat_base { * @Then /^I should not see a grade field for "([^"]*)" and grade item "([^"]*)"$/ * @param string $student * @param string $itemname - * @return Then */ public function i_should_not_see_grade_field($student, $itemname) { $xpath = $this->get_student_and_grade_value_selector($student, $itemname); - return new Then('"' . $this->escape($xpath) . '" "xpath_element" should not exist'); + $this->execute('behat_general::should_not_exist', array($this->escape($xpath), 'xpath_element')); } /** @@ -173,12 +164,11 @@ class behat_gradereport_grader extends behat_base { * @Then /^I should not see a feedback field for "([^"]*)" and grade item "([^"]*)"$/ * @param string $student * @param string $itemname - * @return Then */ public function i_should_not_see_feedback_field($student, $itemname) { $xpath = $this->get_student_and_grade_feedback_selector($student, $itemname); - return new Then('"' . $this->escape($xpath) . '" "xpath_element" should not exist'); + $this->execute('behat_general::should_not_exist', array($this->escape($xpath), 'xpath_element')); } /** @@ -269,5 +259,4 @@ class behat_gradereport_grader extends behat_base { return $cell . "//input[contains(@id, 'feedback_') or @name='ajaxfeedback']"; } - } diff --git a/grade/tests/behat/behat_grade.php b/grade/tests/behat/behat_grade.php index 76ef7f6a857..06723d91f5f 100644 --- a/grade/tests/behat/behat_grade.php +++ b/grade/tests/behat/behat_grade.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; class behat_grade extends behat_base { @@ -39,13 +38,12 @@ class behat_grade extends behat_base { * @param int $grade * @param string $userfullname the user's fullname as returned by fullname() * @param string $itemname - * @return Given */ public function i_give_the_grade($grade, $userfullname, $itemname) { $gradelabel = $userfullname . ' ' . $itemname; $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel); - return new Given('I set the field "' . $this->escape($fieldstr) . '" to "' . $grade . '"'); + $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $grade)); } /** @@ -56,13 +54,12 @@ class behat_grade extends behat_base { * @param string $feedback * @param string $userfullname the user's fullname as returned by fullname() * @param string $itemname - * @return Given */ public function i_give_the_feedback($feedback, $userfullname, $itemname) { $gradelabel = $userfullname . ' ' . $itemname; $fieldstr = get_string('useractivityfeedback', 'gradereport_grader', $gradelabel); - return new Given('I set the field "' . $this->escape($fieldstr) . '" to "' . $this->escape($feedback) . '"'); + $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $this->escape($feedback))); } /** @@ -73,27 +70,25 @@ class behat_grade extends behat_base { * @Given /^I set the following settings for grade item "(?P(?:[^"]|\\")*)":$/ * @param string $gradeitem * @param TableNode $data - * @return Given[] */ public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) { - $steps = array(); $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem); if ($this->running_javascript()) { $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]"; if ($this->getSession()->getPage()->findAll('xpath', $xpath)) { - $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element")); } } $savechanges = get_string('savechanges', 'grades'); $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('edit') . ' '); $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]"; - $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"'); - $steps[] = new Given('I set the following fields to these values:', $data); - $steps[] = new Given('I press "' . $this->escape($savechanges) . '"'); - return $steps; + + $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element")); + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + $this->execute('behat_forms::press_button', $this->escape($savechanges)); } /** @@ -104,17 +99,15 @@ class behat_grade extends behat_base { * @param string $calculation The calculation. * @param string $gradeitem The grade item name. * @param TableNode $TableNode The grade item name - idnumbers relation. - * @return Given[] */ public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) { - $steps = array(); $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem); if ($this->running_javascript()) { $xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]"; if ($this->getSession()->getPage()->findAll('xpath', $xpath)) { - $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element")); } } @@ -122,12 +115,7 @@ class behat_grade extends behat_base { $savechanges = get_string('savechanges', 'grades'); $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades')); $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]"; - $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"'); - - // After adding id numbers we should wait until the page is reloaded. - if ($this->running_javascript()) { - $steps[] = new Given('I wait until the page is ready'); - } + $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element")); // Mapping names to idnumbers. $datahash = $data->getRowsHash(); @@ -139,20 +127,13 @@ class behat_grade extends behat_base { " or " . "parent::li[@class='categoryitem' or @class='courseitem']/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" . "]"; - $steps[] = new Given('I set the field with xpath "' . $inputxpath . '" to "' . $idnumber . '"'); - } - - $steps[] = new Given('I press "' . get_string('addidnumbers', 'grades') . '"'); - - // After adding id numbers we should wait until the page is reloaded. - if ($this->running_javascript()) { - $steps[] = new Given('I wait until the page is ready'); + $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber)); } - $steps[] = new Given('I set the field "' . get_string('calculation', 'grades') . '" to "' . $calculation . '"'); - $steps[] = new Given('I press "' . $savechanges . '"'); + $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades')); + $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation)); + $this->execute('behat_forms::press_button', $savechanges); - return $steps; } /** @@ -163,11 +144,9 @@ class behat_grade extends behat_base { * @param string $calculation The calculation. * @param string $gradeitem The grade item name. * @param TableNode $data The grade item name - idnumbers relation. - * @return Given[] */ public function i_set_calculation_for_grade_category_with_idnumbers($calculation, $gradeitem, TableNode $data) { - $steps = array(); $gradecategorytotal = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem . ' total'); $gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem); @@ -175,7 +154,7 @@ class behat_grade extends behat_base { $xpath = "//tr[contains(.,$gradecategorytotal)]//*[contains(@class,'moodle-actionmenu')]" . "//a[contains(@class,'toggle-display')]"; if ($this->getSession()->getPage()->findAll('xpath', $xpath)) { - $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element")); } } @@ -183,10 +162,7 @@ class behat_grade extends behat_base { $savechanges = get_string('savechanges', 'grades'); $edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('editcalculation', 'grades')); $linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]"; - $steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"'); - - // After adding id numbers we should wait until the page is reloaded. - $steps[] = new Given('I wait until the page is ready'); + $this->execute("behat_general::i_click_on", array($this->escape($linkxpath), "xpath_element")); // Mapping names to idnumbers. $datahash = $data->getRowsHash(); @@ -199,18 +175,13 @@ class behat_grade extends behat_base { "parent::li[@class='categoryitem' | @class='courseitem']" . "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" . "]"; - $steps[] = new Given('I set the field with xpath "' . $inputxpath . '" to "' . $idnumber . '"'); + $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber)); } - $steps[] = new Given('I press "' . get_string('addidnumbers', 'grades') . '"'); - - // After adding id numbers we should wait until the page is reloaded. - $steps[] = new Given('I wait until the page is ready'); - - $steps[] = new Given('I set the field "' . get_string('calculation', 'grades') . '" to "' . $calculation . '"'); - $steps[] = new Given('I press "' . $savechanges . '"'); + $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades')); - return $steps; + $this->execute('behat_forms::i_set_the_field_to', array(get_string('calculation', 'grades'), $calculation)); + $this->execute('behat_forms::press_button', $savechanges); } /** @@ -220,7 +191,6 @@ class behat_grade extends behat_base { * * @Given /^I reset weights for grade category "(?P(?:[^"]|\\")*)"$/ * @param $gradeitem - * @return array */ public function i_reset_weights_for_grade_category($gradeitem) { @@ -230,13 +200,12 @@ class behat_grade extends behat_base { $gradeitemliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem); $xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]"; if ($this->getSession()->getPage()->findAll('xpath', $xpath)) { - $steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"'); + $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element")); } } $linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem)); - $steps[] = new Given('I click on "' . $this->escape($linktext) . '" "link"'); - return $steps; + $this->execute("behat_general::i_click_on", array($this->escape($linktext), "link")); } /** @@ -245,7 +214,6 @@ class behat_grade extends behat_base { * @Given /^gradebook calculations for the course "(?P(?:[^"]|\\")*)" are frozen at version "(?P(?:[^"]|\\")*)"$/ * @param string $coursename * @param string $version - * @return Given */ public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) { global $DB; diff --git a/group/tests/behat/behat_groups.php b/group/tests/behat/behat_groups.php index daede1d5b7e..ce33af789ce 100644 --- a/group/tests/behat/behat_groups.php +++ b/group/tests/behat/behat_groups.php @@ -27,7 +27,6 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Then; use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** @@ -96,11 +95,9 @@ class behat_groups extends behat_base { * @Given /^the group overview should include groups "(?P(?:[^"]|\\")*)" in grouping "(?P(?:[^"]|\\")*)"$/ * @param string $groups one or comma seperated list of groups. * @param string $grouping grouping in which all group should be present. - * @return Then[] */ public function the_groups_overview_should_include_groups_in_grouping($groups, $grouping) { - $steps = array(); $groups = array_map('trim', explode(',', $groups)); foreach ($groups as $groupname) { @@ -108,9 +105,7 @@ class behat_groups extends behat_base { $xpath = "//h3[normalize-space(.) = '{$grouping}']/following-sibling::table//tr//". "td[contains(concat(' ', normalize-space(@class), ' '), ' c0 ')][normalize-space(.) = '{$groupname}' ]"; - $steps[] = new Then('"'.$xpath.'" "xpath_element" should exist'); + $this->execute('behat_general::should_exist', array($xpath, 'xpath_element')); } - - return $steps; } } diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index 819dba9f3a3..673a98afff6 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -681,8 +681,10 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { } } else if (' . self::PAGE_READY_JS . ') { return ""; - } else { + } else if (typeof M.util !== "undefined") { return M.util.pending_js.join(":"); + } else { + return "incomplete" } }();'; $pending = $this->getSession()->evaluateScript($jscode); @@ -713,4 +715,119 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { throw new \Exception('Javascript code and/or AJAX requests are not ready after ' . self::EXTENDED_TIMEOUT . ' seconds. There is a Javascript error or the code is extremely slow.'); } + + /** + * Internal step definition to find exceptions, debugging() messages and PHP debug messages. + * + * Part of behat_hooks class as is part of the testing framework, is auto-executed + * after each step so no features will splicitly use it. + * + * @throws Exception Unknown type, depending on what we caught in the hook or basic \Exception. + * @see Moodle\BehatExtension\Tester\MoodleStepTester + */ + public function look_for_exceptions() { + // Wrap in try in case we were interacting with a closed window. + try { + + // Exceptions. + $exceptionsxpath = "//div[@data-rel='fatalerror']"; + // Debugging messages. + $debuggingxpath = "//div[@data-rel='debugging']"; + // PHP debug messages. + $phperrorxpath = "//div[@data-rel='phpdebugmessage']"; + // Any other backtrace. + $othersxpath = "(//*[contains(., ': call to ')])[1]"; + + $xpaths = array($exceptionsxpath, $debuggingxpath, $phperrorxpath, $othersxpath); + $joinedxpath = implode(' | ', $xpaths); + + // Joined xpath expression. Most of the time there will be no exceptions, so this pre-check + // is faster than to send the 4 xpath queries for each step. + if (!$this->getSession()->getDriver()->find($joinedxpath)) { + return; + } + + // Exceptions. + if ($errormsg = $this->getSession()->getPage()->find('xpath', $exceptionsxpath)) { + + // Getting the debugging info and the backtrace. + $errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.alert-error'); + // If errorinfoboxes is empty, try find notifytiny (original) class. + if (empty($errorinfoboxes)) { + $errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.notifytiny'); + } + $errorinfo = $this->get_debug_text($errorinfoboxes[0]->getHtml()) . "\n" . + $this->get_debug_text($errorinfoboxes[1]->getHtml()); + + $msg = "Moodle exception: " . $errormsg->getText() . "\n" . $errorinfo; + throw new \Exception(html_entity_decode($msg)); + } + + // Debugging messages. + if ($debuggingmessages = $this->getSession()->getPage()->findAll('xpath', $debuggingxpath)) { + $msgs = array(); + foreach ($debuggingmessages as $debuggingmessage) { + $msgs[] = $this->get_debug_text($debuggingmessage->getHtml()); + } + $msg = "debugging() message/s found:\n" . implode("\n", $msgs); + throw new \Exception(html_entity_decode($msg)); + } + + // PHP debug messages. + if ($phpmessages = $this->getSession()->getPage()->findAll('xpath', $phperrorxpath)) { + + $msgs = array(); + foreach ($phpmessages as $phpmessage) { + $msgs[] = $this->get_debug_text($phpmessage->getHtml()); + } + $msg = "PHP debug message/s found:\n" . implode("\n", $msgs); + throw new \Exception(html_entity_decode($msg)); + } + + // Any other backtrace. + // First looking through xpath as it is faster than get and parse the whole page contents, + // we get the contents and look for matches once we found something to suspect that there is a backtrace. + if ($this->getSession()->getDriver()->find($othersxpath)) { + $backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/'; + if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) { + $msgs = array(); + foreach ($backtraces[0] as $backtrace) { + $msgs[] = $backtrace . '()'; + } + $msg = "Other backtraces found:\n" . implode("\n", $msgs); + throw new \Exception(htmlentities($msg)); + } + } + + } catch (NoSuchWindow $e) { + // If we were interacting with a popup window it will not exists after closing it. + } + } + + /** + * Helper function to execute api in a given context. + * + * @param string $contextapi context in which api is defined. + * @param array $params list of params to pass. + * @throws Exception + */ + protected function execute($contextapi, $params = array()) { + if (!is_array($params)) { + $params = array($params); + } + + // Get required context and execute the api. + $contextapi = explode("::", $contextapi); + $context = behat_context_helper::get($contextapi[0]); + call_user_func_array(array($context, $contextapi[1]), $params); + + // NOTE: Wait for pending js and look for exception are not optional, as this might lead to unexpected results. + // Don't make them optional for performance reasons. + + // Wait for pending js. + $this->wait_for_pending_js(); + + // Look for exceptions. + $this->look_for_exceptions(); + } } diff --git a/lib/tests/behat/behat_deprecated.php b/lib/tests/behat/behat_deprecated.php index 1cc06725051..26774b398b5 100644 --- a/lib/tests/behat/behat_deprecated.php +++ b/lib/tests/behat/behat_deprecated.php @@ -28,8 +28,6 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException, - Moodle\BehatExtension\Context\Step\Given as Given, - Moodle\BehatExtension\Context\Step\Then as Then, Behat\Gherkin\Node\TableNode as TableNode; /** diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index d750a8f0ba1..90507b8de06 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -29,9 +29,6 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php'); use Behat\Gherkin\Node\TableNode as TableNode, - Moodle\BehatExtension\Context\Step\Given as Given, - Moodle\BehatExtension\Context\Step\When as When, - Moodle\BehatExtension\Context\Step\Then as Then, Behat\Gherkin\Node\PyStringNode as PyStringNode, Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; @@ -466,9 +463,8 @@ class behat_forms extends behat_base { * @Given /^I select "(?P(?:[^"]|\\")*)" from the "(?P(?:[^"]|\\")*)" singleselect$/ */ public function i_select_from_the_singleselect($option, $singleselect) { - $actions = array( - new Given('I set the field "' . $this->escape($singleselect) . '" to "' . $this->escape($option) . '"'), - ); + + $this->execute('behat_forms::i_set_the_field_to', array($this->escape($singleselect), $this->escape($option))); if (!$this->running_javascript()) { // Press button in the specified select container. @@ -480,11 +476,10 @@ class behat_forms extends behat_base { "or .//select[(./@name='" . $singleselect . "' or ./@id='". $singleselect . "')]" . ")]"; - $actions[] = new Given('I click on "' . get_string('go') . '" "button" in the "' . $containerxpath . - '" "xpath_element"'); + $this->execute('behat_general::i_click_on_in_the', + array(get_string('go'), "button", $containerxpath, "xpath_element") + ); } - - return $actions; } } diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 6ac017c59a6..800896d6569 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -32,8 +32,7 @@ use Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\DriverException as DriverException, WebDriver\Exception\NoSuchElement as NoSuchElement, WebDriver\Exception\StaleElementReference as StaleElementReference, - Behat\Gherkin\Node\TableNode as TableNode, - Moodle\BehatExtension\Context\Step\Given as Given; + Behat\Gherkin\Node\TableNode as TableNode; /** * Cross component steps definitions. @@ -258,8 +257,9 @@ class behat_general extends behat_base { */ public function wait_until_the_page_is_ready() { + // No need to wait if not running JS. if (!$this->running_javascript()) { - throw new DriverException('Waits are disabled in scenarios without Javascript support'); + return; } $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS); diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 348944d7eb6..f537cbeb04c 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -496,88 +496,12 @@ class behat_hooks extends behat_base { * @see Moodle\BehatExtension\Tester\MoodleStepTester */ public function i_look_for_exceptions() { - // If the step already failed in a hook throw the exception. if (!is_null(self::$currentstepexception)) { throw self::$currentstepexception; } - // Wrap in try in case we were interacting with a closed window. - try { - - // Exceptions. - $exceptionsxpath = "//div[@data-rel='fatalerror']"; - // Debugging messages. - $debuggingxpath = "//div[@data-rel='debugging']"; - // PHP debug messages. - $phperrorxpath = "//div[@data-rel='phpdebugmessage']"; - // Any other backtrace. - $othersxpath = "(//*[contains(., ': call to ')])[1]"; - - $xpaths = array($exceptionsxpath, $debuggingxpath, $phperrorxpath, $othersxpath); - $joinedxpath = implode(' | ', $xpaths); - - // Joined xpath expression. Most of the time there will be no exceptions, so this pre-check - // is faster than to send the 4 xpath queries for each step. - if (!$this->getSession()->getDriver()->find($joinedxpath)) { - return; - } - - // Exceptions. - if ($errormsg = $this->getSession()->getPage()->find('xpath', $exceptionsxpath)) { - - // Getting the debugging info and the backtrace. - $errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.alert-error'); - // If errorinfoboxes is empty, try find notifytiny (original) class. - if (empty($errorinfoboxes)) { - $errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.notifytiny'); - } - $errorinfo = $this->get_debug_text($errorinfoboxes[0]->getHtml()) . "\n" . - $this->get_debug_text($errorinfoboxes[1]->getHtml()); - - $msg = "Moodle exception: " . $errormsg->getText() . "\n" . $errorinfo; - throw new \Exception(html_entity_decode($msg)); - } - - // Debugging messages. - if ($debuggingmessages = $this->getSession()->getPage()->findAll('xpath', $debuggingxpath)) { - $msgs = array(); - foreach ($debuggingmessages as $debuggingmessage) { - $msgs[] = $this->get_debug_text($debuggingmessage->getHtml()); - } - $msg = "debugging() message/s found:\n" . implode("\n", $msgs); - throw new \Exception(html_entity_decode($msg)); - } - - // PHP debug messages. - if ($phpmessages = $this->getSession()->getPage()->findAll('xpath', $phperrorxpath)) { - - $msgs = array(); - foreach ($phpmessages as $phpmessage) { - $msgs[] = $this->get_debug_text($phpmessage->getHtml()); - } - $msg = "PHP debug message/s found:\n" . implode("\n", $msgs); - throw new \Exception(html_entity_decode($msg)); - } - - // Any other backtrace. - // First looking through xpath as it is faster than get and parse the whole page contents, - // we get the contents and look for matches once we found something to suspect that there is a backtrace. - if ($this->getSession()->getDriver()->find($othersxpath)) { - $backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/'; - if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) { - $msgs = array(); - foreach ($backtraces[0] as $backtrace) { - $msgs[] = $backtrace . '()'; - } - $msg = "Other backtraces found:\n" . implode("\n", $msgs); - throw new \Exception(htmlentities($msg)); - } - } - - } catch (NoSuchWindow $e) { - // If we were interacting with a popup window it will not exists after closing it. - } + $this->look_for_exceptions(); } /** diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 995a5104bcf..96e5ea376ea 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -27,8 +27,6 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given; -use Moodle\BehatExtension\Context\Step\When; use Behat\Mink\Exception\ExpectationException as ExpectationException; use Behat\Mink\Exception\DriverException as DriverException; @@ -147,23 +145,22 @@ class behat_navigation extends behat_base { * @Given /^I follow "(?P(?:[^"]|\\")*)" in the user menu$/ * * @param string $nodetext - * @return bool|void */ public function i_follow_in_the_user_menu($nodetext) { - $steps = array(); if ($this->running_javascript()) { // The user menu must be expanded when JS is enabled. $xpath = "//div[@class='usermenu']//a[contains(concat(' ', @class, ' '), ' toggle-display ')]"; - $steps[] = new When('I click on "'.$xpath.'" "xpath_element"'); + $this->execute("behat_general::i_click_on", array($this->escape($xpath), "xpath_element")); } // Now select the link. // The CSS path is always present, with or without JS. $csspath = ".usermenu [data-rel='menu-content']"; - $steps[] = new When('I click on "'.$nodetext.'" "link" in the "'.$csspath.'" "css_element"'); - return $steps; + $this->execute('behat_general::i_click_on_in_the', + array($nodetext, "link", $csspath, "css_element") + ); } /** @@ -182,7 +179,10 @@ class behat_navigation extends behat_base { if (!$this->running_javascript()) { if ($nodetext === get_string('administrationsite')) { // Administration menu is not loaded by default any more. Click the link to expand. - return new Given('I click on "'.$nodetext.'" "link" in the "'.get_string('administration').'" "block"'); + $this->execute('behat_general::i_click_on_in_the', + array($nodetext, "link", get_string('administration'), "block") + ); + return true; } return true; } @@ -413,6 +413,6 @@ class behat_navigation extends behat_base { return false; } - return new Given('I click on ".btn-navbar" "css_element"'); + $this->execute('behat_general::i_click_on', array(".btn-navbar", "css_element")); } } diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php index a9f05e44b76..67943bbf327 100644 --- a/lib/tests/behat/behat_permissions.php +++ b/lib/tests/behat/behat_permissions.php @@ -28,7 +28,6 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); use Behat\Mink\Exception\ExpectationException as ExpectationException, - Moodle\BehatExtension\Context\Step\Given as Given, Behat\Gherkin\Node\TableNode as TableNode; /** @@ -46,20 +45,25 @@ class behat_permissions extends behat_base { * @Given /^I set the following system permissions of "(?P(?:[^"]|\\")*)" role:$/ * @param string $rolename * @param TableNode $table - * @return void Executes other steps */ public function i_set_the_following_system_permissions_of_role($rolename, $table) { $parentnodes = get_string('administrationsite') . ' > ' . get_string('users', 'admin') . ' > ' . get_string('permissions', 'role'); - return array( - new Given('I am on homepage'), - new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'), - new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'), - new Given('I fill the capabilities form with the following permissions:', $table), - new Given('I press "' . get_string('savechanges') . '"') + + // Go to home page. + $this->execute("behat_general::i_am_on_homepage"); + + // Navigate to course management page via navigation block. + $this->execute("behat_navigation::i_navigate_to_node_in", + array(get_string('defineroles', 'role'), $parentnodes) ); + + $this->execute("behat_general::click_link", "Edit " . $this->escape($rolename) . " role"); + $this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table); + + $this->execute('behat_forms::press_button', get_string('savechanges')); } /** @@ -67,22 +71,23 @@ class behat_permissions extends behat_base { * @Given /^I override the system permissions of "(?P(?:[^"]|\\")*)" role with:$/ * @param string $rolename * @param TableNode $table - * @return void Executes other steps */ public function i_override_the_system_permissions_of_role_with($rolename, $table) { // We don't know the number of overrides so we have to get it to match the option contents. $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]'); - $result = array( - new Given('I set the field "' . get_string('advancedoverride', 'role') . - '" to "' . $this->escape($roleoption->getText()) . '"')); + $this->execute('behat_forms::i_set_the_field_to', + array(get_string('advancedoverride', 'role'), $this->escape($roleoption->getText())) + ); + if (!$this->running_javascript()) { - $result[] = new Given('I press "' . get_string('go') . '"'); + $this->execute("behat_forms::press_button", get_string('go')); } - $result[] = new Given('I fill the capabilities form with the following permissions:', $table); - $result[] = new Given('I press "' . get_string('savechanges') . '"'); - return $result; + + $this->execute("behat_permissions::i_fill_the_capabilities_form_with_the_following_permissions", $table); + + $this->execute('behat_forms::press_button', get_string('savechanges')); } /** @@ -192,13 +197,21 @@ class behat_permissions extends behat_base { $parentnodes = get_string('administrationsite') . ' > ' . get_string('users', 'admin') . ' > ' . get_string('permissions', 'role'); - return array( - new Given('I am on homepage'), - new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'), - new Given('I follow "Allow role assignments"'), - new Given('I fill in the allowed role assignments form for the "' . $rolename . '" role with:', $table), - new Given('I press "' . get_string('savechanges') . '"') + + // Go to home page. + $this->execute("behat_general::i_am_on_homepage"); + + // Navigate to course management page via navigation block. + $this->execute("behat_navigation::i_navigate_to_node_in", + array(get_string('defineroles', 'role'), $parentnodes) ); + + $this->execute("behat_general::click_link", "Allow role assignments"); + $this->execute("behat_permissions::i_fill_in_the_allowed_role_assignments_form_for_a_role_with", + array($rolename, $table) + ); + + $this->execute('behat_forms::press_button', get_string('savechanges')); } /** diff --git a/message/tests/behat/behat_message.php b/message/tests/behat/behat_message.php index 56c25735208..ecdecf085d6 100644 --- a/message/tests/behat/behat_message.php +++ b/message/tests/behat/behat_message.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; +use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** * Messaging system steps definitions. @@ -49,21 +48,27 @@ class behat_message extends behat_base { */ public function i_send_message_to_user($messagecontent, $userfullname) { - $steps = array(); - $steps[] = new Given('I am on homepage'); + // Visit home page and follow messages. + $this->execute("behat_general::i_am_on_homepage"); + if ($this->running_javascript()) { - $steps[] = new Given('I follow "' . get_string('messages', 'message') . '" in the user menu'); + $this->execute("behat_navigation::i_follow_in_the_user_menu", get_string('messages', 'message')); } else { - $steps[] = new Given('I follow "' . get_string('messages', 'message') . '"'); + $this->execute("behat_general::click_link", get_string('messages', 'message')); } - $steps[] = new Given('I set the field "' . get_string('searchcombined', 'message') . - '" to "' . $this->escape($userfullname) . '"'); - $steps[] = new Given('I press "' . get_string('searchcombined', 'message') . '"'); - $steps[] = new Given('I follow "' . $this->escape(get_string('sendmessageto', 'message', $userfullname)) . '"'); - $steps[] = new Given('I set the field "id_message" to "' . $this->escape($messagecontent) . '"'); - $steps[] = new Given('I press "' . get_string('sendmessage', 'message') . '"'); - return $steps; + $this->execute('behat_forms::i_set_the_field_to', + array(get_string('searchcombined', 'message'), $this->escape($userfullname)) + ); + $this->execute("behat_forms::press_button", get_string('searchcombined', 'message')); + + $this->execute("behat_general::click_link", $this->escape(get_string('sendmessageto', 'message', $userfullname))); + + $this->execute('behat_forms::i_set_the_field_to', + array("id_message", $this->escape($messagecontent)) + ); + + $this->execute("behat_forms::press_button", get_string('sendmessage', 'message')); } } diff --git a/mod/choice/tests/behat/behat_mod_choice.php b/mod/choice/tests/behat/behat_mod_choice.php index e370228a35e..a959c56fd80 100644 --- a/mod/choice/tests/behat/behat_mod_choice.php +++ b/mod/choice/tests/behat/behat_mod_choice.php @@ -27,8 +27,6 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given; - /** * Choice activity definitions. * @@ -49,12 +47,11 @@ class behat_mod_choice extends behat_base { */ 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 set the field "' . $this->escape($option) . '" to "1"'), - new Given('I press "' . get_string('savemychoice', 'choice') . '"') - ); + $this->execute("behat_general::click_link", $this->escape($choiceactivity)); + + $this->execute('behat_forms::i_set_the_field_to', array( $this->escape($option), 1)); + + $this->execute("behat_forms::press_button", get_string('savemychoice', 'choice')); } /** diff --git a/mod/data/tests/behat/behat_mod_data.php b/mod/data/tests/behat/behat_mod_data.php index 3fcc8d09e8d..bd7acff15d2 100644 --- a/mod/data/tests/behat/behat_mod_data.php +++ b/mod/data/tests/behat/behat_mod_data.php @@ -27,9 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Moodle\BehatExtension\Context\Step\When as When, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; /** * Database-related steps definitions. * @@ -48,27 +46,22 @@ class behat_mod_data extends behat_base { * @param string $fieldtype * @param string $activityname * @param TableNode $fielddata - * @return Given[] */ public function i_add_a_field_to_database_and_i_fill_the_form_with($fieldtype, $activityname, TableNode $fielddata) { - $steps = array( - new Given('I follow "' . $this->escape($activityname) . '"'), - new Given('I follow "' . get_string('fields', 'mod_data') . '"'), - new Given('I set the field "newtype" to "' . $this->escape($fieldtype) . '"') - ); + $this->execute("behat_general::click_link", $this->escape($activityname)); + $this->execute("behat_general::click_link", get_string('fields', 'mod_data')); + + $this->execute('behat_forms::i_set_the_field_to', array('newtype', $this->escape($fieldtype))); if (!$this->running_javascript()) { - $steps[] = new Given('I click on "' . get_string('go') . '" "button" in the ".fieldadd" "css_element"'); + $this->execute('behat_general::i_click_on_in_the', + array(get_string('go'), "button", ".fieldadd", "css_element") + ); } - array_push( - $steps, - new Given('I set the following fields to these values:', $fielddata), - new Given('I press "' . get_string('add') . '"') - ); - - return $steps; + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $fielddata); + $this->execute('behat_forms::press_button', get_string('add')); } /** @@ -78,14 +71,12 @@ class behat_mod_data extends behat_base { * * @param string $activityname * @param TableNode $entrydata - * @return When[] */ public function i_add_an_entry_to_database_with($activityname, TableNode $entrydata) { - return array( - new When('I follow "' . $this->escape($activityname) . '"'), - new When('I follow "' . get_string('add', 'mod_data') . '"'), - new When('I set the following fields to these values:', $entrydata), - ); + $this->execute("behat_general::click_link", $this->escape($activityname)); + $this->execute("behat_general::click_link", get_string('add', 'mod_data')); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $entrydata); } } diff --git a/mod/feedback/tests/behat/behat_mod_feedback.php b/mod/feedback/tests/behat/behat_mod_feedback.php index af1c0f1a4ef..eaf20877aab 100644 --- a/mod/feedback/tests/behat/behat_mod_feedback.php +++ b/mod/feedback/tests/behat/behat_mod_feedback.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Behat\Behat\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode, +use Behat\Gherkin\Node\TableNode as TableNode, Behat\Mink\Exception\ExpectationException as ExpectationException; /** @@ -49,10 +48,11 @@ class behat_mod_feedback extends behat_base { * @param TableNode $questiondata with data for filling the add question form */ public function i_add_question_to_the_feedback_with($questiontype, TableNode $questiondata) { - $rv = array(); + $questiontype = $this->escape($questiontype); $additem = $this->escape(get_string('add_item', 'feedback')); - $rv[] = new Given("I select \"{$questiontype}\" from the \"{$additem}\" singleselect"); + + $this->execute('behat_forms::i_select_from_the_singleselect', array($questiontype, $additem)); $rows = $questiondata->getRows(); $modifiedrows = array(); @@ -64,12 +64,10 @@ class behat_mod_feedback extends behat_base { } $newdata = new TableNode($modifiedrows); - $rv[] = new Given('I set the following fields to these values:', $newdata); + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $newdata); $saveitem = $this->escape(get_string('save_item', 'feedback')); - $rv[] = new Given("I press \"{$saveitem}\""); - - return $rv; + $this->execute("behat_forms::press_button", $saveitem); } /** @@ -84,14 +82,20 @@ class behat_mod_feedback extends behat_base { $coursename = $this->escape($coursename); $feedbackname = $this->escape($feedbackname); $completeform = $this->escape(get_string('complete_the_form', 'feedback')); - return [ - new Given("I log in as \"$username\""), - new Given("I follow \"$coursename\""), - new Given("I follow \"$feedbackname\""), - new Given("I follow \"$completeform\""), - new Given("I set the following fields to these values:", $answers), - new Given("I press \"Submit your answers\""), - new Given("I log out") - ]; + + // Log in as user. + $this->execute('behat_auth::i_log_in_as', $username); + + // Navigate to feedback complete form. + $this->execute('behat_general::click_link', $coursename); + $this->execute('behat_general::click_link', $feedbackname); + $this->execute('behat_general::click_link', $completeform); + + // Fill form and submit. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $answers); + $this->execute("behat_forms::press_button", 'Submit your answers'); + + // Log out. + $this->execute('behat_auth::i_log_out'); } } diff --git a/mod/forum/tests/behat/behat_mod_forum.php b/mod/forum/tests/behat/behat_mod_forum.php index f132e1f48a4..3a8cd701f23 100644 --- a/mod/forum/tests/behat/behat_mod_forum.php +++ b/mod/forum/tests/behat/behat_mod_forum.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; /** * Forum-related steps definitions. * @@ -47,7 +46,7 @@ class behat_mod_forum extends behat_base { * @param TableNode $table */ public function i_add_a_new_topic_to_forum_with($forumname, TableNode $table) { - return $this->add_new_discussion($forumname, $table, get_string('addanewtopic', 'forum')); + $this->add_new_discussion($forumname, $table, get_string('addanewtopic', 'forum')); } /** @@ -58,7 +57,7 @@ class behat_mod_forum extends behat_base { * @param TableNode $table */ public function i_add_a_forum_discussion_to_forum_with($forumname, TableNode $table) { - return $this->add_new_discussion($forumname, $table, get_string('addanewdiscussion', 'forum')); + $this->add_new_discussion($forumname, $table, get_string('addanewdiscussion', 'forum')); } /** @@ -71,15 +70,16 @@ class behat_mod_forum extends behat_base { */ public function i_reply_post_from_forum_with($postsubject, $forumname, TableNode $table) { - return array( - new Given('I follow "' . $this->escape($forumname) . '"'), - new Given('I follow "' . $this->escape($postsubject) . '"'), - new Given('I follow "' . get_string('reply', 'forum') . '"'), - new Given('I set the following fields to these values:', $table), - new Given('I press "' . get_string('posttoforum', 'forum') . '"'), - new Given('I wait to be redirected') - ); + // Navigate to forum. + $this->execute('behat_general::click_link', $this->escape($forumname)); + $this->execute('behat_general::click_link', $this->escape($postsubject)); + $this->execute('behat_general::click_link', get_string('reply', 'forum')); + // Fill form and post. + $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table); + + $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum')); + $this->execute('behat_general::i_wait_to_be_redirected'); } /** @@ -91,19 +91,17 @@ class behat_mod_forum extends behat_base { * @param string $forumname * @param TableNode $table * @param string $buttonstr - * @return Given[] */ protected function add_new_discussion($forumname, TableNode $table, $buttonstr) { - // Escaping $forumname as it has been stripped automatically by the transformer. - return array( - new Given('I follow "' . $this->escape($forumname) . '"'), - new Given('I press "' . $buttonstr . '"'), - new Given('I set the following fields to these values:', $table), - new Given('I press "' . get_string('posttoforum', 'forum') . '"'), - new Given('I wait to be redirected') - ); + // Navigate to forum. + $this->execute('behat_general::click_link', $this->escape($forumname)); + $this->execute('behat_forms::press_button', $buttonstr); + // Fill form and post. + $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table); + $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum')); + $this->execute('behat_general::i_wait_to_be_redirected'); } } diff --git a/mod/glossary/tests/behat/behat_mod_glossary.php b/mod/glossary/tests/behat/behat_mod_glossary.php index 51c8eae0d02..0f4b21fa58e 100644 --- a/mod/glossary/tests/behat/behat_mod_glossary.php +++ b/mod/glossary/tests/behat/behat_mod_glossary.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; /** * Glossary-related steps definitions. @@ -47,11 +46,11 @@ class behat_mod_glossary extends behat_base { * @param TableNode $data */ public function i_add_a_glossary_entry_with_the_following_data(TableNode $data) { - return array( - new Given('I press "' . get_string('addentry', 'mod_glossary') . '"'), - new Given('I set the following fields to these values:', $data), - new Given('I press "' . get_string('savechanges') . '"') - ); + $this->execute("behat_forms::press_button", get_string('addentry', 'mod_glossary')); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + + $this->execute("behat_forms::press_button", get_string('savechanges')); } /** @@ -62,13 +61,15 @@ class behat_mod_glossary extends behat_base { */ public function i_add_a_glossary_entries_category_named($categoryname) { - return array( - new Given('I follow "' . get_string('categoryview', 'mod_glossary') . '"'), - new Given('I press "' . get_string('editcategories', 'mod_glossary') . '"'), - new Given('I press "' . get_string('add').' '.get_string('category', 'glossary') . '"'), - new Given('I set the field "name" to "' . $this->escape($categoryname) . '"'), - new Given('I press "' . get_string('savechanges') . '"'), - new Given('I press "' . get_string('back', 'mod_glossary') . '"') - ); + $this->execute("behat_general::click_link", get_string('categoryview', 'mod_glossary')); + + $this->execute("behat_forms::press_button", get_string('editcategories', 'mod_glossary')); + + $this->execute("behat_forms::press_button", get_string('add').' '.get_string('category', 'glossary')); + + $this->execute('behat_forms::i_set_the_field_to', array('name', $this->escape($categoryname))); + + $this->execute("behat_forms::press_button", get_string('savechanges')); + $this->execute("behat_forms::press_button", get_string('back', 'mod_glossary')); } } diff --git a/mod/quiz/tests/behat/behat_mod_quiz.php b/mod/quiz/tests/behat/behat_mod_quiz.php index 4f4e7f80895..715a73a0416 100644 --- a/mod/quiz/tests/behat/behat_mod_quiz.php +++ b/mod/quiz/tests/behat/behat_mod_quiz.php @@ -28,8 +28,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); require_once(__DIR__ . '/../../../../question/tests/behat/behat_question_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; use Behat\Mink\Exception\ExpectationException as ExpectationException; @@ -249,12 +248,15 @@ class behat_mod_quiz extends behat_question_base { $addaquestion = $this->escape(get_string('addaquestion', 'quiz')); $menuxpath = "//div[contains(@class, ' page-add-actions ')][last()]//a[contains(@class, ' textmenu')]"; $itemxpath = "//div[contains(@class, ' page-add-actions ')][last()]//a[contains(@class, ' addquestion ')]"; - return array_merge(array( - new Given("I follow \"$quizname\""), - new Given("I navigate to \"$editquiz\" node in \"$quizadmin\""), - new Given("I click on \"$menuxpath\" \"xpath_element\""), - new Given("I click on \"$itemxpath\" \"xpath_element\""), - ), $this->finish_adding_question($questiontype, $questiondata)); + + $this->execute('behat_general::click_link', $quizname); + + $this->execute("behat_navigation::i_navigate_to_node_in", array($editquiz, $quizadmin)); + + $this->execute("behat_general::i_click_on", array($menuxpath, "xpath_element")); + $this->execute("behat_general::i_click_on", array($itemxpath, "xpath_element")); + + $this->finish_adding_question($questiontype, $questiondata); } /** @@ -265,12 +267,13 @@ class behat_mod_quiz extends behat_question_base { * @param string $newmark the mark to set */ public function i_set_the_max_mark_for_quiz_question($questionname, $newmark) { - return array( - new Given('I follow "' . $this->escape(get_string('editmaxmark', 'quiz')) . '"'), - new Given('I wait until "li input[name=maxmark]" "css_element" exists'), - new Given('I should see "' . $this->escape(get_string('edittitleinstructions')) . '"'), - new Given('I set the field "maxmark" to "' . $this->escape($newmark) . chr(10) . '"'), - ); + $this->execute('behat_general::click_link', $this->escape(get_string('editmaxmark', 'quiz'))); + + $this->execute('behat_general::wait_until_exists', array("li input[name=maxmark]", "css_element")); + + $this->execute('behat_general::assert_page_contains_text', $this->escape(get_string('edittitleinstructions'))); + + $this->execute('behat_forms::i_set_the_field_to', array('maxmark', $this->escape($newmark) . chr(10))); } /** @@ -291,20 +294,21 @@ class behat_mod_quiz extends behat_question_base { } else { throw new ExpectationException("The I open the add to quiz menu step must specify either 'Page N' or 'last'."); } - $menu = $this->find('xpath', $xpath)->click(); + $this->find('xpath', $xpath)->click(); } /** * Click on a given link in the moodle-actionmenu that is currently open. * @Given /^I follow "(?P(?:[^"]|\\")*)" in the open menu$/ * @param string $linkstring the text (or id, etc.) of the link to click. - * @return array of steps. */ public function i_follow_in_the_open_menu($linkstring) { $openmenuxpath = "//div[contains(@class, 'moodle-actionmenu') and contains(@class, 'show')]"; - return array( - new Given('I click on "' . $linkstring . '" "link" in the "' . $openmenuxpath . '" "xpath_element"'), + + $this->execute('behat_general::i_click_on_in_the', + array($linkstring, "link", $openmenuxpath, "xpath_element") ); + } /** @@ -312,15 +316,13 @@ class behat_mod_quiz extends behat_question_base { * @Given /^I should see "(?P(?:[^"]|\\")*)" on quiz page "(?P\d+)"$/ * @param string $questionname the name of the question we are looking for. * @param number $pagenumber the page it should be found on. - * @return array of steps. */ public function i_should_see_on_quiz_page($questionname, $pagenumber) { $xpath = "//li[contains(., '" . $this->escape($questionname) . "')][./preceding-sibling::li[contains(@class, 'pagenumber')][1][contains(., 'Page " . $pagenumber . "')]]"; - return array( - new Given('"' . $xpath . '" "xpath_element" should exist'), - ); + + $this->execute('behat_general::should_exist', array($xpath, 'xpath_element')); } /** @@ -328,15 +330,13 @@ class behat_mod_quiz extends behat_question_base { * @Given /^I should not see "(?P(?:[^"]|\\")*)" on quiz page "(?P\d+)"$/ * @param string $questionname the name of the question we are looking for. * @param number $pagenumber the page it should be found on. - * @return array of steps. */ public function i_should_not_see_on_quiz_page($questionname, $pagenumber) { $xpath = "//li[contains(., '" . $this->escape($questionname) . "')][./preceding-sibling::li[contains(@class, 'pagenumber')][1][contains(., 'Page " . $pagenumber . "')]]"; - return array( - new Given('"' . $xpath . '" "xpath_element" should not exist'), - ); + + $this->execute('behat_general::should_not_exist', array($xpath, 'xpath_element')); } /** @@ -345,15 +345,13 @@ class behat_mod_quiz extends behat_question_base { * @Given /^I should see "(?P(?:[^"]|\\")*)" before "(?P(?:[^"]|\\")*)" on the edit quiz page$/ * @param string $firstquestionname the name of the question that should come first in order. * @param string $secondquestionname the name of the question that should come immediately after it in order. - * @return array of steps. */ public function i_should_see_before_on_the_edit_quiz_page($firstquestionname, $secondquestionname) { $xpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($firstquestionname) . "')]/following-sibling::li[contains(@class, ' slot ')][1]" . "[contains(., '" . $this->escape($secondquestionname) . "')]"; - return array( - new Given('"' . $xpath . '" "xpath_element" should exist'), - ); + + $this->execute('behat_general::should_exist', array($xpath, 'xpath_element')); } /** @@ -361,14 +359,12 @@ class behat_mod_quiz extends behat_question_base { * @Given /^"(?P(?:[^"]|\\")*)" should have number "(?P(?:[^"]|\\")*)" on the edit quiz page$/ * @param string $questionname the name of the question we are looking for. * @param number $number the number (or 'i') that should be displayed beside that question. - * @return array of steps. */ public function should_have_number_on_the_edit_quiz_page($questionname, $number) { $xpath = "//li[contains(@class, 'slot') and contains(., '" . $this->escape($questionname) . "')]//span[contains(@class, 'slotnumber') and normalize-space(text()) = '" . $this->escape($number) . "']"; - return array( - new Given('"' . $xpath . '" "xpath_element" should exist'), - ); + + $this->execute('behat_general::should_exist', array($xpath, 'xpath_element')); } /** @@ -387,13 +383,11 @@ class behat_mod_quiz extends behat_question_base { * @When /^I click on the "(Add|Remove)" page break icon after question "(?P(?:[^"]|\\")*)"$/ * @param string $addorremoves 'Add' or 'Remove'. * @param string $questionname the name of the question before the icon to click. - * @return array of steps. */ public function i_click_on_the_page_break_icon_after_question($addorremoves, $questionname) { $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname); - return array( - new Given('I click on "' . $xpath . '" "xpath_element"'), - ); + + $this->execute("behat_general::i_click_on", array($xpath, "xpath_element")); } /** @@ -405,9 +399,8 @@ class behat_mod_quiz extends behat_question_base { */ public function the_page_break_icon_after_question_should_exist($addorremoves, $questionname) { $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname); - return array( - new Given('"' . $xpath . '" "xpath_element" should exist'), - ); + + $this->execute('behat_general::should_exist', array($xpath, 'xpath_element')); } /** @@ -419,9 +412,8 @@ class behat_mod_quiz extends behat_question_base { */ public function the_page_break_icon_after_question_should_not_exist($addorremoves, $questionname) { $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname); - return array( - new Given('"' . $xpath . '" "xpath_element" should not exist'), - ); + + $this->execute('behat_general::should_not_exist', array($xpath, 'xpath_element')); } /** @@ -434,9 +426,8 @@ class behat_mod_quiz extends behat_question_base { */ public function the_page_break_link_after_question_should_contain($addorremoves, $questionname, $paramdata) { $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname); - return array( - new Given('I click on "' . $xpath . '" "xpath_element"'), - ); + + $this->execute("behat_general::i_click_on", array($xpath, "xpath_element")); } /** @@ -480,7 +471,7 @@ class behat_mod_quiz extends behat_question_base { /** * Return the xpath for shuffle checkbox in section heading - * @param strung $heading + * @param string $heading * @return string */ protected function get_xpath_for_shuffle_checkbox($heading) { @@ -495,15 +486,13 @@ class behat_mod_quiz extends behat_question_base { * @param string $questionname the name of the question we are looking for. * @param string $target the target place to move to. One of the links in the pop-up like * "After Page 1" or "After Question N". - * @return array of steps. */ public function i_move_question_after_item_by_clicking_the_move_icon($questionname, $target) { $iconxpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($questionname) . "')]//span[contains(@class, 'editing_move')]"; - return array( - new Given('I click on "' . $iconxpath . '" "xpath_element"'), - new Given('I click on "' . $this->escape($target) . '" "text"'), - ); + + $this->execute("behat_general::i_click_on", array($iconxpath, "xpath_element")); + $this->execute("behat_general::i_click_on", array($this->escape($target), "text")); } /** @@ -511,16 +500,15 @@ class behat_mod_quiz extends behat_question_base { * @When /^I move "(?P(?:[^"]|\\")*)" to "(?P(?:[^"]|\\")*)" in the quiz by dragging$/ * @param string $questionname the name of the question we are looking for. * @param string $target the target place to move to. Ether a question name, or "Page N" - * @return array of steps. */ public function i_move_question_after_item_by_dragging($questionname, $target) { $iconxpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($questionname) . "')]//span[contains(@class, 'editing_move')]//img"; $destinationxpath = "//li[contains(@class, ' slot ') or contains(@class, 'pagenumber ')]" . "[contains(., '" . $this->escape($target) . "')]"; - return array( - new Given('I drag "' . $iconxpath . '" "xpath_element" ' . - 'and I drop it in "' . $destinationxpath . '" "xpath_element"'), + + $this->execute('behat_general::i_drag_and_i_drop_it_in', + array($iconxpath, 'xpath_element', $destinationxpath, 'xpath_element') ); } @@ -535,9 +523,11 @@ class behat_mod_quiz extends behat_question_base { $slotxpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($questionname) . "')]"; $deletexpath = "//a[contains(@class, 'editing_delete')]"; - return array( - new Given('I click on "' . $slotxpath . $deletexpath . '" "xpath_element"'), - new Given('I click on "Yes" "button" in the "Confirm" "dialogue"'), + + $this->execute("behat_general::i_click_on", array($slotxpath . $deletexpath, "xpath_element")); + + $this->execute('behat_general::i_click_on_in_the', + array('Yes', "button", "Confirm", "dialogue") ); } @@ -549,11 +539,11 @@ class behat_mod_quiz extends behat_question_base { * @param string $sectionheading the new heading to set. */ public function i_set_the_section_heading_for($sectionname, $sectionheading) { - return array( - new Given('I follow "' . $this->escape("Edit heading '{$sectionname}'") . '"'), - new Given('I should see "' . $this->escape(get_string('edittitleinstructions')) . '"'), - new Given('I set the field "section" to "' . $this->escape($sectionheading) . chr(10) . '"'), - ); + $this->execute('behat_general::click_link', $this->escape("Edit heading '{$sectionname}'")); + + $this->execute('behat_general::assert_page_contains_text', $this->escape(get_string('edittitleinstructions'))); + + $this->execute('behat_forms::i_set_the_field_to', array('section', $this->escape($sectionheading) . chr(10))); } /** diff --git a/mod/workshop/tests/behat/behat_mod_workshop.php b/mod/workshop/tests/behat/behat_mod_workshop.php index 3c1c7b4bae5..4a6718523d5 100644 --- a/mod/workshop/tests/behat/behat_mod_workshop.php +++ b/mod/workshop/tests/behat/behat_mod_workshop.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Gherkin\Node\TableNode as TableNode; /** * Steps definitions related to mod_workshop. @@ -54,11 +53,12 @@ class behat_mod_workshop extends behat_base { $xpath = "//*[@class='userplan']/descendant::div[./span[contains(.,$phaseliteral)]]/". "descendant-or-self::a[./img[@alt=$switchphase]]"; $continue = $this->escape(get_string('continue')); - return array( - new Given("I follow \"$workshopname\""), - new Given("I click on \"$xpath\" \"xpath_element\""), - new Given("I press \"$continue\""), - ); + + $this->execute('behat_general::click_link', $workshopname); + + $this->execute("behat_general::i_click_on", array($xpath, "xpath_element")); + + $this->execute("behat_forms::press_button", $continue); } /** @@ -72,12 +72,14 @@ class behat_mod_workshop extends behat_base { $workshopname = $this->escape($workshopname); $savechanges = $this->escape(get_string('savechanges')); $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' ownsubmission ')]/descendant::input[@type='submit']"; - return array( - new Given("I follow \"$workshopname\""), - new Given("I click on \"$xpath\" \"xpath_element\""), - new Given("I set the following fields to these values:", $table), - new Given("I press \"$savechanges\""), - ); + + $this->execute('behat_general::click_link', $workshopname); + + $this->execute("behat_general::i_click_on", array($xpath, "xpath_element")); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table); + + $this->execute("behat_forms::press_button", $savechanges); } /** @@ -91,12 +93,14 @@ class behat_mod_workshop extends behat_base { $workshopname = $this->escape($workshopname); $editassessmentform = $this->escape(get_string('editassessmentform', 'workshop')); $saveandclose = $this->escape(get_string('saveandclose', 'workshop')); - return array( - new Given("I follow \"$workshopname\""), - new Given("I follow \"$editassessmentform\""), - new Given("I set the following fields to these values:", $table), - new Given("I press \"$saveandclose\""), - ); + + $this->execute('behat_general::click_link', $workshopname); + + $this->execute('behat_general::click_link', $editassessmentform); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table); + + $this->execute("behat_forms::press_button", $saveandclose); } /** @@ -114,12 +118,16 @@ class behat_mod_workshop extends behat_base { "and contains(.,$submissionliteral)]"; $assess = $this->escape(get_string('assess', 'workshop')); $saveandclose = $this->escape(get_string('saveandclose', 'workshop')); - return array( - new Given("I follow \"$workshopname\""), - new Given("I click on \"$assess\" \"button\" in the \"$xpath\" \"xpath_element\""), - new Given("I set the following fields to these values:", $table), - new Given("I press \"$saveandclose\""), + + $this->execute('behat_general::click_link', $workshopname); + + $this->execute('behat_general::i_click_on_in_the', + array($assess, "button", $xpath, "xpath_element") ); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table); + + $this->execute("behat_forms::press_button", $saveandclose); } /** diff --git a/question/tests/behat/behat_question.php b/question/tests/behat/behat_question.php index 2c5e60c3250..b5bf6f3c8e6 100644 --- a/question/tests/behat/behat_question.php +++ b/question/tests/behat/behat_question.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/behat_question_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode, +use Behat\Gherkin\Node\TableNode as TableNode, Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; @@ -48,14 +47,17 @@ class behat_question extends behat_question_base { * @Given /^I add a "(?P(?:[^"]|\\")*)" question filling the form with:$/ * @param string $questiontypename The question type name * @param TableNode $questiondata The data to fill the question type form. - * @return Given[] the steps. */ public function i_add_a_question_filling_the_form_with($questiontypename, TableNode $questiondata) { - return array_merge(array( - new Given('I follow "' . get_string('questionbank', 'question') . '"'), - new Given('I press "' . get_string('createnewquestion', 'question') . '"'), - ), $this->finish_adding_question($questiontypename, $questiondata)); + // Go to question bank. + $this->execute("behat_general::click_link", get_string('questionbank', 'question')); + + // Click on create question. + $this->execute('behat_forms::press_button', get_string('createnewquestion', 'question')); + + // Add question. + $this->finish_adding_question($questiontypename, $questiondata); } /** diff --git a/question/tests/behat/behat_question_base.php b/question/tests/behat/behat_question_base.php index b45b6aeb4b7..55ab4526f6b 100644 --- a/question/tests/behat/behat_question_base.php +++ b/question/tests/behat/behat_question_base.php @@ -27,8 +27,7 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); -use Moodle\BehatExtension\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode, +use Behat\Gherkin\Node\TableNode as TableNode, Behat\Mink\Exception\ExpectationException as ExpectationException, Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; @@ -48,15 +47,13 @@ class behat_question_base extends behat_base { * * @param string $questiontypename The question type name * @param TableNode $questiondata The data to fill the question type form - * @return Given[] the steps. */ protected function finish_adding_question($questiontypename, TableNode $questiondata) { - return array( - new Given('I set the field "' . $this->escape($questiontypename) . '" to "1"'), - new Given('I click on ".submitbutton" "css_element"'), - new Given('I set the following fields to these values:', $questiondata), - new Given('I press "id_submitbutton"') - ); + $this->execute('behat_forms::i_set_the_field_to', array($this->escape($questiontypename), 1)); + $this->execute("behat_general::i_click_on", array('.submitbutton', "css_element")); + + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $questiondata); + $this->execute("behat_forms::press_button", 'id_submitbutton'); } } -- 2.43.0