From ca29a8f8656b74881e089c99205bd8ed0f98cfb9 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 13 Feb 2014 14:27:44 +0800 Subject: [PATCH] MDL-44440 navigation: Added behat step to navigate from navigation/Admin block This step will help navigate to different parts of moodle using navigation or adminstration block. --- lib/tests/behat/behat_navigation.php | 124 +++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 7807f3746b7..8de69a411af 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -111,4 +111,128 @@ class behat_navigation extends behat_base { $node = $this->find('xpath', $xpath, $exception); $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(?:[^"]|\\")*)" 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); + $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); + $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 exxeption if no node found. + if (!$node) { + throw new ExpectationException('Navigation node "' . $nodetext . ' not found under "' . $parentnodes . + '" block', $this->getSession()); + } + $this->ensure_node_is_visible($node); + $node->click(); + } }