~ubuntu-branches/ubuntu/utopic/moodle/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/tests/behat/behat_navigation.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20140512161038-2039l24hvvlan3hs
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
111
111
        $node = $this->find('xpath', $xpath, $exception);
112
112
        $node->click();
113
113
    }
 
114
 
 
115
    /**
 
116
     * Click link in navigation tree that matches the text in parentnode/s (seperated using greater-than character if more than one)
 
117
     *
 
118
     * @Given /^I navigate to "(?P<nodetext_string>(?:[^"]|\\")*)" node in "(?P<parentnodes_string>(?:[^"]|\\")*)"$/
 
119
     *
 
120
     * @throws ExpectationException
 
121
     * @param string $nodetext navigation node to click.
 
122
     * @param string $parentnodes comma seperated list of parent nodes.
 
123
     * @return void
 
124
     */
 
125
    public function i_navigate_to_node_in($nodetext, $parentnodes) {
 
126
 
 
127
        // Site admin is different and needs special treatment.
 
128
        $siteadminstr = get_string('administrationsite');
 
129
 
 
130
        // Create array of all parentnodes.
 
131
        $parentnodes = array_map('trim', explode('>', $parentnodes));
 
132
        $countparentnode = count($parentnodes);
 
133
 
 
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();
 
142
                }
 
143
            }
 
144
        }
 
145
 
 
146
        // Expand first node, and get it.
 
147
        $node = $this->get_top_navigation_node($parentnodes[0]);
 
148
 
 
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);
 
153
 
 
154
            if ($this->running_javascript()) {
 
155
                $this->ensure_node_is_visible($nodetoexpand);
 
156
                $nodetoexpand->click();
 
157
 
 
158
                // Site administration node needs to be expanded.
 
159
                if ($parentnodes[0] === $siteadminstr) {
 
160
                    $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
 
161
                }
 
162
            }
 
163
        }
 
164
 
 
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);
 
169
 
 
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();
 
178
                        }
 
179
                    }
 
180
                }
 
181
            }
 
182
        }
 
183
 
 
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);
 
189
 
 
190
        // Throw exception if no node found.
 
191
        if (!$node) {
 
192
            throw new ExpectationException('Navigation node "' . $nodetext . '" not found under "' . $parentnodes . '"', $this->getSession());
 
193
        }
 
194
 
 
195
        if ($this->running_javascript()) {
 
196
            $this->ensure_node_is_visible($node);
 
197
        }
 
198
 
 
199
        $node->click();
 
200
    }
 
201
 
 
202
    /**
 
203
     * Helper function to get top navigation node in tree.
 
204
     *
 
205
     * @throws ExpectationException if note not found.
 
206
     * @param string $nodetext name of top navigation node in tree.
 
207
     * @return NodeElement
 
208
     */
 
209
    protected function get_top_navigation_node($nodetext) {
 
210
 
 
211
        // Avoid problems with quotes.
 
212
        $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
 
213
        $exception = new ExpectationException('Top navigation node "' . $nodetext . ' not found in "', $this->getSession());
 
214
 
 
215
        // First find in navigation block.
 
216
        $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]" .
 
217
            "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
 
218
            "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
 
219
            "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
 
220
            "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
 
221
            "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
 
222
            "|" .
 
223
            "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
 
224
            "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
 
225
            "/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
 
226
            "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
 
227
            "[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
 
228
            "/span[normalize-space(.)=" . $nodetextliteral ."]]" .
 
229
            "|" .
 
230
            "//div[contains(concat(' ', normalize-space(@class), ' '), ' content ')]/div" .
 
231
            "/ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" .
 
232
            "/li[p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
 
233
            "/span[normalize-space(.)=" . $nodetextliteral ."]]";
 
234
            $node = $this->find('xpath', $xpath, $exception);
 
235
 
 
236
        return $node;
 
237
    }
 
238
 
 
239
    /**
 
240
     * Helper function to get sub-navigation node.
 
241
     *
 
242
     * @throws ExpectationException if note not found.
 
243
     * @param string $nodetext node to find.
 
244
     * @param NodeElement $parentnode parent navigation node.
 
245
     * @return NodeElement.
 
246
     */
 
247
    protected function get_navigation_node($nodetext, $parentnode = null) {
 
248
 
 
249
        // Avoid problems with quotes.
 
250
        $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
 
251
 
 
252
        $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
 
253
            "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
 
254
            "/child::span[normalize-space(.)=" . $nodetextliteral ."]]";
 
255
        $node = $parentnode->find('xpath', $xpath);
 
256
        if (!$node) {
 
257
            $xpath = "/ul/li[contains(concat(' ', normalize-space(@class), ' '), ' contains_branch ')]" .
 
258
                "[child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" .
 
259
                "/child::a[normalize-space(.)=" . $nodetextliteral ."]]";
 
260
            $node = $parentnode->find('xpath', $xpath);
 
261
        }
 
262
 
 
263
        if (!$node) {
 
264
            throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' . $parentnode->getText() . '"', $this->getSession());
 
265
        }
 
266
        return $node;
 
267
    }
114
268
}