From a542553d425c03de8736bedbe1c68e0af0650fcf Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 4 Mar 2014 17:40:56 +0800 Subject: [PATCH] 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. --- admin/tests/behat/upload_users.feature | 6 +- .../tool/behat/tests/behat/list_steps.feature | 4 +- .../uploadcourse/tests/behat/create.feature | 4 +- .../uploadcourse/tests/behat/update.feature | 4 +- badges/tests/behat/award_badge.feature | 8 +- .../behat/block_activity_modules.feature | 5 +- blocks/tests/behat/manage_blocks.feature | 5 +- cohort/tests/behat/add_cohort.feature | 6 +- cohort/tests/behat/behat_cohort.php | 9 +- .../tests/behat/upload_cohort_users.feature | 6 +- lib/tests/behat/behat_navigation.php | 167 ++++++++++-------- lib/tests/behat/behat_permissions.php | 9 +- .../upload/tests/behat/upload_file.feature | 4 +- user/tests/behat/edituserpassword.feature | 5 +- 14 files changed, 116 insertions(+), 126 deletions(-) diff --git a/admin/tests/behat/upload_users.feature b/admin/tests/behat/upload_users.feature index d67085b329c..6c0cf64e303 100644 --- a/admin/tests/behat/upload_users.feature +++ b/admin/tests/behat/upload_users.feature @@ -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" diff --git a/admin/tool/behat/tests/behat/list_steps.feature b/admin/tool/behat/tests/behat/list_steps.feature index 8061f4ed88c..2301c78701d 100644 --- a/admin/tool/behat/tests/behat/list_steps.feature +++ b/admin/tool/behat/tests/behat/list_steps.feature @@ -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 diff --git a/admin/tool/uploadcourse/tests/behat/create.feature b/admin/tool/uploadcourse/tests/behat/create.feature index 60688091aad..4692a9916fd 100644 --- a/admin/tool/uploadcourse/tests/behat/create.feature +++ b/admin/tool/uploadcourse/tests/behat/create.feature @@ -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 diff --git a/admin/tool/uploadcourse/tests/behat/update.feature b/admin/tool/uploadcourse/tests/behat/update.feature index 4f115892406..bf16f5d81f6 100644 --- a/admin/tool/uploadcourse/tests/behat/update.feature +++ b/admin/tool/uploadcourse/tests/behat/update.feature @@ -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 diff --git a/badges/tests/behat/award_badge.feature b/badges/tests/behat/award_badge.feature index b6ac009ab00..01c05fa3086 100644 --- a/badges/tests/behat/award_badge.feature +++ b/badges/tests/behat/award_badge.feature @@ -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 | @@ -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 | diff --git a/blocks/activity_modules/tests/behat/block_activity_modules.feature b/blocks/activity_modules/tests/behat/block_activity_modules.feature index 50df25fb6a6..6199d69859f 100644 --- a/blocks/activity_modules/tests/behat/block_activity_modules.feature +++ b/blocks/activity_modules/tests/behat/block_activity_modules.feature @@ -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 diff --git a/blocks/tests/behat/manage_blocks.feature b/blocks/tests/behat/manage_blocks.feature index 13e23be1bed..542911f5128 100644 --- a/blocks/tests/behat/manage_blocks.feature +++ b/blocks/tests/behat/manage_blocks.feature @@ -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" diff --git a/cohort/tests/behat/add_cohort.feature b/cohort/tests/behat/add_cohort.feature index b490ef5504f..5391230829e 100644 --- a/cohort/tests/behat/add_cohort.feature +++ b/cohort/tests/behat/add_cohort.feature @@ -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 | diff --git a/cohort/tests/behat/behat_cohort.php b/cohort/tests/behat/behat_cohort.php index adeb88b3b77..f41f57d5195 100644 --- a/cohort/tests/behat/behat_cohort.php +++ b/cohort/tests/behat/behat_cohort.php @@ -60,14 +60,13 @@ class behat_cohort extends behat_base { // 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 ); diff --git a/cohort/tests/behat/upload_cohort_users.feature b/cohort/tests/behat/upload_cohort_users.feature index 22133290607..2d896b26125 100644 --- a/cohort/tests/behat/upload_cohort_users.feature +++ b/cohort/tests/behat/upload_cohort_users.feature @@ -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" diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 8600d0056bf..06c071436ea 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -112,15 +112,103 @@ class behat_navigation extends behat_base { $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(?:[^"]|\\")*)" node in "(?P(?:[^"]|\\")*)"$/ + * + * @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. * - * @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. + * @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()); @@ -152,12 +240,13 @@ class behat_navigation extends behat_base { /** * 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. - * @throws ExpectationException if note not found. */ protected function get_navigation_node($nodetext, $parentnode = null) { + // Avoid problems with quotes. $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext); @@ -173,75 +262,9 @@ class behat_navigation extends behat_base { } if (!$node) { - throw new ExpectationException('Sub-navigation node "' . $nodetext . ' not found under "' . $parentnode->getText() . - '" block', $this->getSession()); + throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' . + $parentnode->getText() . '"', $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(?:[^"]|\\")*)" node in "(?P(?:[^"]|\\")*)"$/ - * - * @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(); - } } diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php index 1e6fd98b266..c17b6ed93d4 100644 --- a/lib/tests/behat/behat_permissions.php +++ b/lib/tests/behat/behat_permissions.php @@ -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') . '"') diff --git a/repository/upload/tests/behat/upload_file.feature b/repository/upload/tests/behat/upload_file.feature index baf94253c93..3fb3d680e99 100644 --- a/repository/upload/tests/behat/upload_file.feature +++ b/repository/upload/tests/behat/upload_file.feature @@ -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" diff --git a/user/tests/behat/edituserpassword.feature b/user/tests/behat/edituserpassword.feature index 8bff06662f0..0a3a0fe2815 100644 --- a/user/tests/behat/edituserpassword.feature +++ b/user/tests/behat/edituserpassword.feature @@ -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