From c4509f8084961e03d3de18f5478fc22f182509ac Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 13 May 2013 15:13:00 +0800 Subject: [PATCH] MDL-39638 behat: New step to collapse nodes based on the expand nodes Also updating steps with a collapse behaviour. --- admin/tests/behat/upload_users.feature | 2 +- cohort/tests/behat/add_cohort.feature | 2 +- cohort/tests/behat/behat_cohort.php | 2 +- .../tests/behat/upload_cohort_users.feature | 2 +- lib/tests/behat/behat_navigation.php | 40 +++++++++++++++++-- lib/tests/behat/behat_permissions.php | 2 +- 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/admin/tests/behat/upload_users.feature b/admin/tests/behat/upload_users.feature index 9ff0ab6f7ba..ae9d83beed0 100644 --- a/admin/tests/behat/upload_users.feature +++ b/admin/tests/behat/upload_users.feature @@ -14,7 +14,7 @@ Feature: Upload users | Section 1 | math102 | S1 | | Section 3 | math102 | S3 | And I log in as "admin" - And I expand "Front page settings" node + And I collapse "Front page settings" node And I expand "Site administration" node And I expand "Users" node And I expand "Accounts" node diff --git a/cohort/tests/behat/add_cohort.feature b/cohort/tests/behat/add_cohort.feature index 82dd518a9e0..2c6ba7b2f5b 100644 --- a/cohort/tests/behat/add_cohort.feature +++ b/cohort/tests/behat/add_cohort.feature @@ -12,7 +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 expand "Front page settings" node + And I collapse "Front page settings" node And I expand "Site administration" node And I expand "Users" node And I expand "Accounts" node diff --git a/cohort/tests/behat/behat_cohort.php b/cohort/tests/behat/behat_cohort.php index 884bb02a5e8..68ff6073fcd 100644 --- a/cohort/tests/behat/behat_cohort.php +++ b/cohort/tests/behat/behat_cohort.php @@ -65,7 +65,7 @@ class behat_cohort extends behat_base { $steps = array_merge( array( new Given('I am on homepage'), - new Given('I expand "Front page settings" node'), + new Given('I collapse "Front page settings" node'), new Given('I expand "Site administration" node'), new Given('I expand "Users" node'), new Given('I expand "Accounts" node'), diff --git a/cohort/tests/behat/upload_cohort_users.feature b/cohort/tests/behat/upload_cohort_users.feature index e62a13fe938..c40095f16db 100644 --- a/cohort/tests/behat/upload_cohort_users.feature +++ b/cohort/tests/behat/upload_cohort_users.feature @@ -22,7 +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 expand "Course administration" node + And I collapse "Course administration" node And I expand "Site administration" node And I expand "Users" node And I expand "Accounts" node diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 9146eb9593f..f676146ed06 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -27,6 +27,9 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); +use Behat\Behat\Context\Step\Given as Given, + Behat\Mink\Exception\ExpectationException as ExpectationException; + /** * Steps definitions to navigate through the navigation tree nodes. * @@ -41,7 +44,7 @@ class behat_navigation extends behat_base { * Expands the selected node of the navigation tree that matches the text. * @Given /^I expand "(?P(?:[^"]|\\")*)" node$/ * - * @throws ElementNotFoundException Thrown by behat_base::find + * @throws ExpectationException * @param string $nodetext */ public function i_expand_node($nodetext) { @@ -54,18 +57,47 @@ class behat_navigation extends behat_base { } $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . - "/child::li" . + "/child::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" . "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch')]" . "/child::span[contains(concat(' ', normalize-space(.), ' '), '" . $nodetext . "')]" . "|" . "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed'))]" . - "/descendant::li" . + "/descendant::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed')]" . "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch')]" . "/child::span[contains(concat(' ', normalize-space(.), ' '), '" . $nodetext . "')]"; - $node = $this->find('xpath', $xpath); + $exception = new ExpectationException('The "' . $nodetext . '" node can not be expanded', $this->getSession()); + $node = $this->find('xpath', $xpath, $exception); $node->click(); } + /** + * Collapses the selected node of the navigation tree that matches the text. + * + * @Given /^I collapse "(?P(?:[^"]|\\")*)" node$/ + * @throws ExpectationException + * @param string $nodetext + */ + public function i_collapse_node($nodetext) { + + // No collapsible nodes with non-JS browsers. + if (!$this->running_javascript()) { + return false; + } + + $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . + "/child::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" . + "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch')]" . + "/child::span[contains(concat(' ', normalize-space(.), ' '), '" . $nodetext . "')]" . + "|" . + "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . + "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed'))]" . + "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch')]" . + "/child::span[contains(concat(' ', normalize-space(.), ' '), '" . $nodetext . "')]"; + + $exception = new ExpectationException('The "' . $nodetext . '" node can not be collapsed', $this->getSession()); + $node = $this->find('xpath', $xpath, $exception); + $node->click(); + } } diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php index 15981ff0e0f..84c8f5aa961 100644 --- a/lib/tests/behat/behat_permissions.php +++ b/lib/tests/behat/behat_permissions.php @@ -52,7 +52,7 @@ class behat_permissions extends behat_base { return array( new Given('I am on homepage'), - new Given('I expand "Front page settings" node'), + new Given('I collapse "Front page settings" node'), new Given('I expand "Site administration" node'), new Given('I expand "Users" node'), new Given('I expand "Permissions" node'),