2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Navigation steps definitions.
22 * @copyright 2012 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../behat/behat_base.php');
30 use Behat\Behat\Context\Step\Given as Given,
31 Behat\Mink\Exception\ExpectationException as ExpectationException;
34 * Steps definitions to navigate through the navigation tree nodes.
38 * @copyright 2012 David Monllaó
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class behat_navigation extends behat_base {
44 * Expands the selected node of the navigation tree that matches the text.
45 * @Given /^I expand "(?P<nodetext_string>(?:[^"]|\\")*)" node$/
47 * @throws ExpectationException
48 * @param string $nodetext
50 public function i_expand_node($nodetext) {
52 // This step is useless with Javascript disabled as Moodle auto expands
53 // all of tree's nodes; adding this because of scenarios that shares the
54 // same steps with and without Javascript enabled.
55 if (!$this->running_javascript()) {
56 if ($nodetext === get_string('administrationsite')) {
57 // Administration menu is not loaded by default any more. Click the link to expand.
58 return new Given('I click on "'.$nodetext.'" "link" in the "'.get_string('administration').'" "block"');
63 // Avoid problems with quotes.
64 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
66 $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
67 "/child::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" .
68 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
69 "/child::span[normalize-space(.)=$nodetextliteral]" .
71 "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
72 "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
73 "/descendant::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" .
74 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
75 "/child::span[normalize-space(.)=$nodetextliteral]";
77 $exception = new ExpectationException('The "' . $nodetext . '" node can not be expanded', $this->getSession());
78 $node = $this->find('xpath', $xpath, $exception);
79 $this->ensure_node_is_visible($node);
84 * Collapses the selected node of the navigation tree that matches the text.
86 * @Given /^I collapse "(?P<nodetext_string>(?:[^"]|\\")*)" node$/
87 * @throws ExpectationException
88 * @param string $nodetext
90 public function i_collapse_node($nodetext) {
92 // No collapsible nodes with non-JS browsers.
93 if (!$this->running_javascript()) {
97 // Avoid problems with quotes.
98 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
100 $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
101 "/child::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
102 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
103 "/child::span[normalize-space(.)=$nodetextliteral]" .
105 "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
106 "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
107 "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
108 "/child::span[normalize-space(.)=$nodetextliteral]";
110 $exception = new ExpectationException('The "' . $nodetext . '" node can not be collapsed', $this->getSession());
111 $node = $this->find('xpath', $xpath, $exception);
116 * Click link in navigation tree that matches the text in parentnode/s (seperated using greater-than character if more than one)
118 * @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" node in "(?P<parentnodes_string>(?:[^"]|\\")*)"$/
120 * @throws ExpectationException
121 * @param string $nodetext navigation node to click.
122 * @param string $parentnodes comma seperated list of parent nodes.
125 public function i_navigate_to_node_in($nodetext, $parentnodes) {
127 // Site admin is different and needs special treatment.
128 $siteadminstr = get_string('administrationsite');
130 // Create array of all parentnodes.
131 $parentnodes = array_map('trim', explode('>', $parentnodes));
132 $countparentnode = count($parentnodes);
134 // If JS is disabled and Site administration is not expanded we
135 // should follow it, so all the lower-level nodes are available.
136 if (!$this->running_javascript()) {
137 if ($parentnodes[0] === $siteadminstr) {
138 // We don't know if there if Site admin is already expanded so
139 // don't wait, it is non-JS and we already waited for the DOM.
140 if ($siteadminlink = $this->getSession()->getPage()->find('named', array('link', "'" . $siteadminstr . "'"))) {
141 $siteadminlink->click();
146 // Expand first node, and get it.
147 $node = $this->get_top_navigation_node($parentnodes[0]);
149 // Expand parent, sub-parent nodes in navigation if js enabled.
150 if ($node->hasClass('collapsed') || ($node->hasAttribute('data-loaded') && $node->getAttribute('data-loaded') == 0)) {
151 $xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]/span";
152 $nodetoexpand = $node->find('xpath', $xpath);
154 if ($this->running_javascript()) {
155 $this->ensure_node_is_visible($nodetoexpand);
156 $nodetoexpand->click();
158 // Site administration node needs to be expanded.
159 if ($parentnodes[0] === $siteadminstr) {
160 $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
165 // If sub-parent nodes then get to the last one.
166 if ($countparentnode > 1) {
167 for ($i = 1; $i < $countparentnode; $i++) {
168 $node = $this->get_navigation_node($parentnodes[$i], $node);
170 // Keep expanding all sub-parents if js enabled.
171 if ($this->running_javascript()) {
172 $xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]";
173 if ($node->hasClass('collapsed')) {
174 $nodetoexpand = $node->find('xpath', $xpath);
175 if ($this->running_javascript()) {
176 $this->ensure_node_is_visible($nodetoexpand);
177 $nodetoexpand->click();
184 // Finally, click on requested node under navigation.
185 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
186 $xpath = "/ul/li/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]" .
187 "/a[normalize-space(.)=" . $nodetextliteral . "]";
188 $node = $node->find('xpath', $xpath);
190 // Throw exception if no node found.
192 throw new ExpectationException('Navigation node "' . $nodetext . '" not found under "' .
193 $parentnodes . '"', $this->getSession());
196 if ($this->running_javascript()) {
197 $this->ensure_node_is_visible($node);
204 * Helper function to get top navigation node in tree.
206 * @throws ExpectationException if note not found.
207 * @param string $nodetext name of top navigation node in tree.
208 * @return NodeElement
210 protected function get_top_navigation_node($nodetext) {
212 // Avoid problems with quotes.
213 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
214 $exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());
216 // First find in navigation block.
217 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
218 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
219 "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
220 "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
221 "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
222 "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
224 "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
225 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
226 "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
227 "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
228 "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
229 "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
231 "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
232 "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
233 "/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
234 "/span[normalize-space(.)=" . $nodetextliteral ."]]";
235 $node = $this->find('xpath', $xpath, $exception);
241 * Helper function to get sub-navigation node.
243 * @throws ExpectationException if note not found.
244 * @param string $nodetext node to find.
245 * @param NodeElement $parentnode parent navigation node.
246 * @return NodeElement.
248 protected function get_navigation_node($nodetext, $parentnode = null) {
250 // Avoid problems with quotes.
251 $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
253 $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
254 "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
255 "/child::span[normalize-space(.)=" . $nodetextliteral ."]]";
256 $node = $parentnode->find('xpath', $xpath);
258 $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
259 "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
260 "/child::a[normalize-space(.)=" . $nodetextliteral ."]]";
261 $node = $parentnode->find('xpath', $xpath);
265 throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' .
266 $parentnode->getText() . '"', $this->getSession());