Skip to content

Commit

Permalink
MDL-44440 behat: Refactoring features to use I navigate to
Browse files Browse the repository at this point in the history
Replacing multiple "I expand node" steps by the
new "I navigate to". Also fixing few I navigate to
when dealing with non-JS.
  • Loading branch information
David Monllao committed Mar 7, 2014
1 parent 370e88d commit a542553
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 124 deletions.
6 changes: 1 addition & 5 deletions admin/tests/behat/upload_users.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ Feature: Upload users
| Section 1 | math102 | S1 |
| Section 3 | math102 | S3 |
And I log in as "admin"
And I collapse "Front page settings" node
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
And I follow "Upload users"
And I navigate to "Upload users" node in "Site administration,Users,Accounts"
When I upload "lib/tests/fixtures/upload_users.csv" file to "File" filemanager
And I press "Upload users"
Then I should see "Upload users preview"
Expand Down
4 changes: 1 addition & 3 deletions admin/tool/behat/tests/behat/list_steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ Feature: List the system steps definitions
Background:
Given I am on homepage
And I log in as "admin"
And I expand "Site administration" node
And I expand "Development" node
And I follow "Acceptance testing"
And I navigate to "Acceptance testing" node in "Site administration,Development"

@javascript
Scenario: Accessing the list
Expand Down
4 changes: 1 addition & 3 deletions admin/tool/uploadcourse/tests/behat/create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ Feature: An admin can create courses using a CSV file
| fullname | shortname | category |
| First course | C1 | 0 |
And I log in as "admin"
And I expand "Site administration" node
And I expand "Courses" node
And I follow "Upload courses"
And I navigate to "Upload courses" node in "Site administration,Courses"

@javascript
Scenario: Creation of unexisting courses
Expand Down
4 changes: 1 addition & 3 deletions admin/tool/uploadcourse/tests/behat/update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ Feature: An admin can update courses using a CSV file
| fullname | shortname | category |
| Some random name | C1 | 0 |
And I log in as "admin"
And I expand "Site administration" node
And I expand "Courses" node
And I follow "Upload courses"
And I navigate to "Upload courses" node in "Site administration,Courses"

@javascript
Scenario: Updating a course fullname
Expand Down
8 changes: 2 additions & 6 deletions badges/tests/behat/award_badge.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ Feature: Award badges
@javascript
Scenario: Award profile badge
Given I log in as "admin"
And I expand "Site administration" node
And I expand "Badges" node
And I follow "Add a new badge"
And I navigate to "Add a new badge" node in "Site administration,Badges"
And I set the following fields to these values:
| Name | Profile Badge |
| Description | Test badge description |
Expand Down Expand Up @@ -44,9 +42,7 @@ Feature: Award badges
| teacher | teacher | 1 | teacher1@asd.com |
| student | student | 1 | student1@asd.com |
And I log in as "admin"
And I expand "Site administration" node
And I expand "Badges" node
And I follow "Add a new badge"
And I navigate to "Add a new badge" node in "Site administration,Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ Feature: Block activity modules

Background:
Given I log in as "admin"
And I expand "Site administration" node
And I expand "Plugins" node
And I expand "Activity modules" node
And I follow "Manage activities"
And I navigate to "Manage activities" node in "Site administration,Plugins,Activity modules"
And I click on "//a[@title=\"Show\"]" "xpath_element" in the "Feedback" "table_row"

Scenario: Add activities block on the frontpage
Expand Down
5 changes: 1 addition & 4 deletions blocks/tests/behat/manage_blocks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ Feature: Block appearances
| user | course | role |
| teacher1 | C1 | editingteacher |
And I log in as "admin"
And I expand "Site administration" node
And I expand "Appearance" node
And I expand "Themes" node
And I follow "Theme selector"
And I navigate to "Theme selector" node in "Site administration,Appearance,Themes"
And I click on "Change theme" "button" in the "Default" "table_row"
And I click on "Use theme" "button" in the "Afterburner" "table_row"
And I press "Continue"
Expand Down
6 changes: 1 addition & 5 deletions cohort/tests/behat/add_cohort.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ Feature: Add cohorts of users
| user3 | Third | User | third@user.com |
| user4 | Forth | User | forth@user.com |
And I log in as "admin"
And I collapse "Front page settings" node
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
And I follow "Cohorts"
And I navigate to "Cohorts" node in "Site administration,Users,Accounts"
And I press "Add"
And I set the following fields to these values:
| Name | Test cohort name |
Expand Down
9 changes: 4 additions & 5 deletions cohort/tests/behat/behat_cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ public function i_add_user_to_cohort_members($user, $cohortidnumber) {

// With JS enabled we should expand a few tree nodes.
if ($this->running_javascript()) {
$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 collapse "' . get_string('frontpagesettings', 'admin') . '" node'),
new Given('I expand "' . get_string('administrationsite') . '" node'),
new Given('I expand "' . get_string('users', 'admin') . '" node'),
new Given('I expand "' . get_string('accounts', 'admin') . '" node'),
new Given('I follow "' . get_string('cohorts', 'cohort') . '"')
new Given('I navigate to "' . get_string('cohorts', 'cohort') . '" node in "' . $parentnodes . '"')
),
$steps
);
Expand Down
6 changes: 1 addition & 5 deletions cohort/tests/behat/upload_cohort_users.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ Feature: Upload users to a cohort
And I follow "Course 2"
And I add "Cohort sync" enrolment method with:
| Cohort | Cohort 2 |
And I collapse "Course administration" node
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
When I follow "Upload users"
When I navigate to "Upload users" node in "Site administration,Users,Accounts"
And I upload "lib/tests/fixtures/upload_users_cohorts.csv" file to "File" filemanager
And I press "Upload users"
And I press "Upload users"
Expand Down
163 changes: 93 additions & 70 deletions lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,87 +112,37 @@ public function i_collapse_node($nodetext) {
$node->click();
}

/**
* Helper function to get top navigation node in tree.
*
* @param string $nodetext name of top navigation node in tree.
* @param NodeElement $blocknode Block node in which to find node.
* @return NodeElement
* @throws ExpectationException if note not found.
*/
protected function get_top_navigation_node($nodetext) {
// Avoid problems with quotes.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
$exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());

// First find in navigation block.
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
"/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
"/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/span[normalize-space(.)=" . $nodetextliteral ."]]" .
"|" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
"/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
"/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/span[normalize-space(.)=" . $nodetextliteral ."]]" .
"|" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
"/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
"/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/span[normalize-space(.)=" . $nodetextliteral ."]]";
$node = $this->find('xpath', $xpath, $exception);

return $node;
}

/**
* Helper function to get sub-navigation node.
*
* @param string $nodetext node to find.
* @param NodeElement $parentnode parent navigation node.
* @return NodeElement.
* @throws ExpectationException if note not found.
*/
protected function get_navigation_node($nodetext, $parentnode = null) {
// Avoid problems with quotes.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);

$xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/child::span[normalize-space(.)=" . $nodetextliteral ."]]";
$node = $parentnode->find('xpath', $xpath);
if (!$node) {
$xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/child::a[normalize-space(.)=" . $nodetextliteral ."]]";
$node = $parentnode->find('xpath', $xpath);
}

if (!$node) {
throw new ExpectationException('Sub-navigation node "' . $nodetext . ' not found under "' . $parentnode->getText() .
'" block', $this->getSession());
}
return $node;
}

/**
* Click link in navigation tree that matches the text in parentnode/s (comma seperated if more then one)
*
* @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" node in "(?P<parentnodes_string>(?:[^"]|\\")*)"$/
*
* @throws ExpectationException
* @param string $nodetext navigation node to click.
* @param string $parentnodes comma seperated list of parent nodes.
* @param strin $blockname name of the block.
* @return void
*/
public function i_navigate_to_node_in($nodetext, $parentnodes) {

// Site admin is different and needs special treatment.
$siteadminstr = get_string('administrationsite');

// Create array of all parentnodes.
$parentnodes = explode(',', $parentnodes);
$countparentnode = count($parentnodes);

// If JS is disabled and Site administration is not expanded we
// should follow it, so all the lower-level nodes are available.
if (!$this->running_javascript()) {
if ($parentnodes[0] === $siteadminstr) {
// We don't know if there if Site admin is already expanded so
// don't wait, it is non-JS and we already waited for the DOM.
if ($siteadminlink = $this->getSession()->getPage()->find('named', array('link', "'" . $siteadminstr . "'"))) {
$siteadminlink->click();
}
}
}

// Expand first node, and get it.
$node = $this->get_top_navigation_node($parentnodes[0]);

Expand All @@ -204,6 +154,11 @@ public function i_navigate_to_node_in($nodetext, $parentnodes) {
if ($this->running_javascript()) {
$this->ensure_node_is_visible($nodetoexpand);
$nodetoexpand->click();

// Site administration node needs to be expanded.
if ($parentnodes[0] === $siteadminstr) {
$this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
}
}
}

Expand Down Expand Up @@ -234,8 +189,8 @@ public function i_navigate_to_node_in($nodetext, $parentnodes) {

// Throw exception if no node found.
if (!$node) {
throw new ExpectationException('Navigation node "' . $nodetext . ' not found under "' . $parentnodes .
'" block', $this->getSession());
throw new ExpectationException('Navigation node "' . $nodetext . '" not found under "' .
$parentnodes . '"', $this->getSession());
}

if ($this->running_javascript()) {
Expand All @@ -244,4 +199,72 @@ public function i_navigate_to_node_in($nodetext, $parentnodes) {

$node->click();
}

/**
* Helper function to get top navigation node in tree.
*
* @throws ExpectationException if note not found.
* @param string $nodetext name of top navigation node in tree.
* @return NodeElement
*/
protected function get_top_navigation_node($nodetext) {

// Avoid problems with quotes.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
$exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());

// First find in navigation block.
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
"/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
"/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/span[normalize-space(.)=" . $nodetextliteral ."]]" .
"|" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
"/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
"/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/span[normalize-space(.)=" . $nodetextliteral ."]]" .
"|" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
"/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
"/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/span[normalize-space(.)=" . $nodetextliteral ."]]";
$node = $this->find('xpath', $xpath, $exception);

return $node;
}

/**
* Helper function to get sub-navigation node.
*
* @throws ExpectationException if note not found.
* @param string $nodetext node to find.
* @param NodeElement $parentnode parent navigation node.
* @return NodeElement.
*/
protected function get_navigation_node($nodetext, $parentnode = null) {

// Avoid problems with quotes.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);

$xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/child::span[normalize-space(.)=" . $nodetextliteral ."]]";
$node = $parentnode->find('xpath', $xpath);
if (!$node) {
$xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
"[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
"/child::a[normalize-space(.)=" . $nodetextliteral ."]]";
$node = $parentnode->find('xpath', $xpath);
}

if (!$node) {
throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' .
$parentnode->getText() . '"', $this->getSession());
}
return $node;
}
}
9 changes: 4 additions & 5 deletions lib/tests/behat/behat_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ class behat_permissions extends behat_base {
*/
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 collapse "' . get_string('frontpagesettings', 'admin') . '" node'),
new Given('I expand "' . get_string('administrationsite') . '" node'),
new Given('I expand "' . get_string('users', 'admin') . '" node'),
new Given('I expand "' . get_string('permissions', 'role') . '" node'),
new Given('I follow "' . get_string('defineroles', 'role') . '"'),
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') . '"')
Expand Down
4 changes: 2 additions & 2 deletions repository/upload/tests/behat/upload_file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Feature: Upload files
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And I log in as "admin"
And I navigate to "My private files" node in "My profile"
When I navigate to "My private files" node in "My profile"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
Then I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the "div.fp-content" "css_element"
When I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager as:
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager as:
| Save as | empty_copy.txt |
Then I should see "2" elements in "Files" filemanager
And I should see "empty.txt"
Expand Down
5 changes: 1 addition & 4 deletions user/tests/behat/edituserpassword.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ Feature: Enable/disable password field based on authentication selected.
Scenario: Verify the password field is enabled/disabled based on authentication selected, in user edit advanced page.
Given I log in as "admin"
And I follow "My home"
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
When I follow "Add a new user"
When I navigate to "Add a new user" node in "Site administration,Users,Accounts"
Then the "newpassword" "field" should be enabled
And I set the field "auth" to "Web services authentication"
And the "newpassword" "field" should be disabled
Expand Down

0 comments on commit a542553

Please sign in to comment.