Commit eb9ca848 authored by Rajesh Taneja's avatar Rajesh Taneja
Browse files

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.
parent cffc0126
......@@ -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'));
}
}
......@@ -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_name_string>(?:[^"]|\\")*)" 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<block_name_string>(?:[^"]|\\")*)" 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")
);
}
}
......@@ -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<dayofmonth>\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.
......
......@@ -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'));
}
}
......@@ -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<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" 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<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" 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")
);
}
}
This diff is collapsed.
......@@ -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_fullname_string>(?:[^"]|\\")*)" user as "(?P<rolename_string>(?:[^"]|\\")*)"$/
* @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;
}
}
......@@ -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;
}
/**
......
......@@ -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;
}
/**
......
......@@ -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<activity_name_string>(?:[^"]|\\")*)" 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 {