MDL-44440 behat: Refactoring features to use I navigate to

Replacing multiple "I expand node" steps by the
new "I navigate to". Also fixing few I navigate to
when dealing with non-JS.
This commit is contained in:
David Monllao 2014-03-04 17:40:56 +08:00
parent 370e88d4fe
commit a542553d42
14 changed files with 116 additions and 126 deletions

View File

@ -14,11 +14,7 @@ Feature: Upload users
| Section 1 | math102 | S1 | | Section 1 | math102 | S1 |
| Section 3 | math102 | S3 | | Section 3 | math102 | S3 |
And I log in as "admin" And I log in as "admin"
And I collapse "Front page settings" node And I navigate to "Upload users" node in "Site administration,Users,Accounts"
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
And I follow "Upload users"
When I upload "lib/tests/fixtures/upload_users.csv" file to "File" filemanager When I upload "lib/tests/fixtures/upload_users.csv" file to "File" filemanager
And I press "Upload users" And I press "Upload users"
Then I should see "Upload users preview" Then I should see "Upload users preview"

View File

@ -7,9 +7,7 @@ Feature: List the system steps definitions
Background: Background:
Given I am on homepage Given I am on homepage
And I log in as "admin" And I log in as "admin"
And I expand "Site administration" node And I navigate to "Acceptance testing" node in "Site administration,Development"
And I expand "Development" node
And I follow "Acceptance testing"
@javascript @javascript
Scenario: Accessing the list Scenario: Accessing the list

View File

@ -9,9 +9,7 @@ Feature: An admin can create courses using a CSV file
| fullname | shortname | category | | fullname | shortname | category |
| First course | C1 | 0 | | First course | C1 | 0 |
And I log in as "admin" And I log in as "admin"
And I expand "Site administration" node And I navigate to "Upload courses" node in "Site administration,Courses"
And I expand "Courses" node
And I follow "Upload courses"
@javascript @javascript
Scenario: Creation of unexisting courses Scenario: Creation of unexisting courses

View File

@ -9,9 +9,7 @@ Feature: An admin can update courses using a CSV file
| fullname | shortname | category | | fullname | shortname | category |
| Some random name | C1 | 0 | | Some random name | C1 | 0 |
And I log in as "admin" And I log in as "admin"
And I expand "Site administration" node And I navigate to "Upload courses" node in "Site administration,Courses"
And I expand "Courses" node
And I follow "Upload courses"
@javascript @javascript
Scenario: Updating a course fullname Scenario: Updating a course fullname

View File

@ -7,9 +7,7 @@ Feature: Award badges
@javascript @javascript
Scenario: Award profile badge Scenario: Award profile badge
Given I log in as "admin" Given I log in as "admin"
And I expand "Site administration" node And I navigate to "Add a new badge" node in "Site administration,Badges"
And I expand "Badges" node
And I follow "Add a new badge"
And I set the following fields to these values: And I set the following fields to these values:
| Name | Profile Badge | | Name | Profile Badge |
| Description | Test badge description | | Description | Test badge description |
@ -44,9 +42,7 @@ Feature: Award badges
| teacher | teacher | 1 | teacher1@asd.com | | teacher | teacher | 1 | teacher1@asd.com |
| student | student | 1 | student1@asd.com | | student | student | 1 | student1@asd.com |
And I log in as "admin" And I log in as "admin"
And I expand "Site administration" node And I navigate to "Add a new badge" node in "Site administration,Badges"
And I expand "Badges" node
And I follow "Add a new badge"
And I set the following fields to these values: And I set the following fields to these values:
| Name | Site Badge | | Name | Site Badge |
| Description | Site badge description | | Description | Site badge description |

View File

@ -6,10 +6,7 @@ Feature: Block activity modules
Background: Background:
Given I log in as "admin" Given I log in as "admin"
And I expand "Site administration" node And I navigate to "Manage activities" node in "Site administration,Plugins,Activity modules"
And I expand "Plugins" node
And I expand "Activity modules" node
And I follow "Manage activities"
And I click on "//a[@title=\"Show\"]" "xpath_element" in the "Feedback" "table_row" And I click on "//a[@title=\"Show\"]" "xpath_element" in the "Feedback" "table_row"
Scenario: Add activities block on the frontpage Scenario: Add activities block on the frontpage

View File

@ -15,10 +15,7 @@ Feature: Block appearances
| user | course | role | | user | course | role |
| teacher1 | C1 | editingteacher | | teacher1 | C1 | editingteacher |
And I log in as "admin" And I log in as "admin"
And I expand "Site administration" node And I navigate to "Theme selector" node in "Site administration,Appearance,Themes"
And I expand "Appearance" node
And I expand "Themes" node
And I follow "Theme selector"
And I click on "Change theme" "button" in the "Default" "table_row" 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 click on "Use theme" "button" in the "Afterburner" "table_row"
And I press "Continue" And I press "Continue"

View File

@ -12,11 +12,7 @@ Feature: Add cohorts of users
| user3 | Third | User | third@user.com | | user3 | Third | User | third@user.com |
| user4 | Forth | User | forth@user.com | | user4 | Forth | User | forth@user.com |
And I log in as "admin" And I log in as "admin"
And I collapse "Front page settings" node And I navigate to "Cohorts" node in "Site administration,Users,Accounts"
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
And I follow "Cohorts"
And I press "Add" And I press "Add"
And I set the following fields to these values: And I set the following fields to these values:
| Name | Test cohort name | | Name | Test cohort name |

View File

@ -60,14 +60,13 @@ class behat_cohort extends behat_base {
// With JS enabled we should expand a few tree nodes. // With JS enabled we should expand a few tree nodes.
if ($this->running_javascript()) { if ($this->running_javascript()) {
$parentnodes = get_string('administrationsite') . ',' .
get_string('users', 'admin') . ',' .
get_string('accounts', 'admin');
$steps = array_merge( $steps = array_merge(
array( array(
new Given('I am on homepage'), new Given('I am on homepage'),
new Given('I collapse "' . get_string('frontpagesettings', 'admin') . '" node'), new Given('I navigate to "' . get_string('cohorts', 'cohort') . '" node in "' . $parentnodes . '"')
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') . '"')
), ),
$steps $steps
); );

View File

@ -22,11 +22,7 @@ Feature: Upload users to a cohort
And I follow "Course 2" And I follow "Course 2"
And I add "Cohort sync" enrolment method with: And I add "Cohort sync" enrolment method with:
| Cohort | Cohort 2 | | Cohort | Cohort 2 |
And I collapse "Course administration" node When I navigate to "Upload users" node in "Site administration,Users,Accounts"
And I expand "Site administration" node
And I expand "Users" node
And I expand "Accounts" node
When I follow "Upload users"
And I upload "lib/tests/fixtures/upload_users_cohorts.csv" file to "File" filemanager And I upload "lib/tests/fixtures/upload_users_cohorts.csv" file to "File" filemanager
And I press "Upload users" And I press "Upload users"
And I press "Upload users" And I press "Upload users"

View File

@ -112,15 +112,103 @@ class behat_navigation extends behat_base {
$node->click(); $node->click();
} }
/**
* 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.
* @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 parent, sub-parent nodes in navigation if js enabled.
if ($node->hasClass('collapsed') || ($node->hasAttribute('data-loaded') && $node->getAttribute('data-loaded') == 0)) {
$xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]/span";
$nodetoexpand = $node->find('xpath', $xpath);
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);
}
}
}
// If sub-parent nodes then get to the last one.
if ($countparentnode > 1) {
for ($i = 1; $i < $countparentnode; $i++) {
$node = $this->get_navigation_node($parentnodes[$i], $node);
// Keep expanding all sub-parents if js enabled.
if ($this->running_javascript()) {
$xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]";
if ($node->hasClass('collapsed')) {
$nodetoexpand = $node->find('xpath', $xpath);
if ($this->running_javascript()) {
$this->ensure_node_is_visible($nodetoexpand);
$nodetoexpand->click();
}
}
}
}
}
// Finally, click on requested node under navigation.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
$xpath = "/ul/li/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]" .
"/a[normalize-space(.)=" . $nodetextliteral . "]";
$node = $node->find('xpath', $xpath);
// Throw exception if no node found.
if (!$node) {
throw new ExpectationException('Navigation node "' . $nodetext . '" not found under "' .
$parentnodes . '"', $this->getSession());
}
if ($this->running_javascript()) {
$this->ensure_node_is_visible($node);
}
$node->click();
}
/** /**
* Helper function to get top navigation node in tree. * 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. * @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) { protected function get_top_navigation_node($nodetext) {
// Avoid problems with quotes. // Avoid problems with quotes.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext); $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
$exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession()); $exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());
@ -152,12 +240,13 @@ class behat_navigation extends behat_base {
/** /**
* Helper function to get sub-navigation node. * Helper function to get sub-navigation node.
* *
* @throws ExpectationException if note not found.
* @param string $nodetext node to find. * @param string $nodetext node to find.
* @param NodeElement $parentnode parent navigation node. * @param NodeElement $parentnode parent navigation node.
* @return NodeElement. * @return NodeElement.
* @throws ExpectationException if note not found.
*/ */
protected function get_navigation_node($nodetext, $parentnode = null) { protected function get_navigation_node($nodetext, $parentnode = null) {
// Avoid problems with quotes. // Avoid problems with quotes.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext); $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
@ -173,75 +262,9 @@ class behat_navigation extends behat_base {
} }
if (!$node) { if (!$node) {
throw new ExpectationException('Sub-navigation node "' . $nodetext . ' not found under "' . $parentnode->getText() . throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' .
'" block', $this->getSession()); $parentnode->getText() . '"', $this->getSession());
} }
return $node; 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.
*/
public function i_navigate_to_node_in($nodetext, $parentnodes) {
// Create array of all parentnodes.
$parentnodes = explode(',', $parentnodes);
$countparentnode = count($parentnodes);
// Expand first node, and get it.
$node = $this->get_top_navigation_node($parentnodes[0]);
// Expand parent, sub-parent nodes in navigation if js enabled.
if ($node->hasClass('collapsed') || ($node->hasAttribute('data-loaded') && $node->getAttribute('data-loaded') == 0)) {
$xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]/span";
$nodetoexpand = $node->find('xpath', $xpath);
if ($this->running_javascript()) {
$this->ensure_node_is_visible($nodetoexpand);
$nodetoexpand->click();
}
}
// If sub-parent nodes then get to the last one.
if ($countparentnode > 1) {
for ($i = 1; $i < $countparentnode; $i++) {
$node = $this->get_navigation_node($parentnodes[$i], $node);
// Keep expanding all sub-parents if js enabled.
if ($this->running_javascript()) {
$xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]";
if ($node->hasClass('collapsed')) {
$nodetoexpand = $node->find('xpath', $xpath);
if ($this->running_javascript()) {
$this->ensure_node_is_visible($nodetoexpand);
$nodetoexpand->click();
}
}
}
}
}
// Finally, click on requested node under navigation.
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
$xpath = "/ul/li/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]" .
"/a[normalize-space(.)=" . $nodetextliteral . "]";
$node = $node->find('xpath', $xpath);
// Throw exception if no node found.
if (!$node) {
throw new ExpectationException('Navigation node "' . $nodetext . ' not found under "' . $parentnodes .
'" block', $this->getSession());
}
if ($this->running_javascript()) {
$this->ensure_node_is_visible($node);
}
$node->click();
}
} }

View File

@ -50,13 +50,12 @@ class behat_permissions extends behat_base {
*/ */
public function i_set_the_following_system_permissions_of_role($rolename, $table) { 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( return array(
new Given('I am on homepage'), new Given('I am on homepage'),
new Given('I collapse "' . get_string('frontpagesettings', 'admin') . '" node'), new Given('I navigate to "' . get_string('defineroles', 'role') . '" node in "' . $parentnodes . '"'),
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 follow "Edit ' . $this->escape($rolename) . ' role"'), new Given('I follow "Edit ' . $this->escape($rolename) . ' role"'),
new Given('I fill the capabilities form with the following permissions:', $table), new Given('I fill the capabilities form with the following permissions:', $table),
new Given('I press "' . get_string('savechanges') . '"') new Given('I press "' . get_string('savechanges') . '"')

View File

@ -10,11 +10,11 @@ Feature: Upload files
| fullname | shortname | category | | fullname | shortname | category |
| Course 1 | C1 | 0 | | Course 1 | C1 | 0 |
And I log in as "admin" 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 And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
Then I should see "1" elements in "Files" filemanager Then I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the "div.fp-content" "css_element" 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 | | Save as | empty_copy.txt |
Then I should see "2" elements in "Files" filemanager Then I should see "2" elements in "Files" filemanager
And I should see "empty.txt" And I should see "empty.txt"

View File

@ -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. Scenario: Verify the password field is enabled/disabled based on authentication selected, in user edit advanced page.
Given I log in as "admin" Given I log in as "admin"
And I follow "My home" And I follow "My home"
And I expand "Site administration" node When I navigate to "Add a new user" node in "Site administration,Users,Accounts"
And I expand "Users" node
And I expand "Accounts" node
When I follow "Add a new user"
Then the "newpassword" "field" should be enabled Then the "newpassword" "field" should be enabled
And I set the field "auth" to "Web services authentication" And I set the field "auth" to "Web services authentication"
And the "newpassword" "field" should be disabled And the "newpassword" "field" should be disabled