Skip to content

Commit

Permalink
MDL-53381 behat: Replaced chained steps with api.
Browse files Browse the repository at this point in the history
Behat 3.x doesn't support chained steps and hence
this is first step to get rid of chained steps in core.
  • Loading branch information
Rajesh Taneja committed Apr 6, 2016
1 parent cffc012 commit eb9ca84
Show file tree
Hide file tree
Showing 30 changed files with 737 additions and 700 deletions.
32 changes: 12 additions & 20 deletions auth/tests/behat/behat_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -51,12 +48,11 @@ public function i_log_in_as($username) {
$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'));
}

/**
Expand All @@ -65,22 +61,18 @@ public function i_log_in_as($username) {
* @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'));
}
}
30 changes: 16 additions & 14 deletions blocks/tests/behat/behat_blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

require_once(__DIR__ . '/../../../lib/behat/behat_base.php');

use Moodle\BehatExtension\Context\Step\Given as Given;

/**
* Blocks management steps definitions.
*
Expand All @@ -46,30 +44,31 @@ 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;
}

/**
* Docks a block. Editing mode should be previously enabled.
*
* @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")
);
}

/**
Expand All @@ -78,7 +77,6 @@ public function i_dock_block($blockname) {
* @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) {

Expand All @@ -93,7 +91,9 @@ public function i_open_the_blocks_action_menu($blockname) {
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")
);
}

/**
Expand All @@ -106,9 +106,11 @@ public function i_open_the_blocks_action_menu($blockname) {
*/
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")
);
}
}
41 changes: 20 additions & 21 deletions calendar/tests/behat/behat_calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -44,45 +43,45 @@ 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);
}

/**
* Create event.
*
* @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);

}

/**
* Hover over a specific day in the calendar.
*
* @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')));
Expand All @@ -94,16 +93,16 @@ public function i_hover_over_day_of_this_month_in_calendar($day) {
$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.
Expand Down
31 changes: 15 additions & 16 deletions cohort/tests/behat/behat_cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -48,29 +46,30 @@ 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')) {

// With JS enabled we should expand a few tree nodes.
$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'));

}
}
49 changes: 26 additions & 23 deletions completion/tests/behat/behat_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -55,9 +53,9 @@ public function user_has_completed_activity($userfullname, $activityname) {
$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")
);
}

Expand All @@ -74,12 +72,9 @@ public function user_has_not_completed_activity($userfullname, $activityname) {
$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"));
}

/**
Expand All @@ -91,7 +86,7 @@ public function go_to_the_current_course_activity_completion_report() {
$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));
}

/**
Expand All @@ -104,18 +99,21 @@ public function completion_is_toggled_in_course($completionstatus) {

$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") {
Expand All @@ -125,15 +123,17 @@ public function activity_marked_as_complete($activityname, $activitytype, $compl
}
$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") {
Expand All @@ -143,7 +143,10 @@ public function activity_marked_as_not_complete($activityname, $activitytype, $c
}
$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")
);

}
}
Loading

0 comments on commit eb9ca84

Please sign in to comment.