111
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 "' . $parentnodes . '"', $this->getSession());
195
if ($this->running_javascript()) {
196
$this->ensure_node_is_visible($node);
203
* Helper function to get top navigation node in tree.
205
* @throws ExpectationException if note not found.
206
* @param string $nodetext name of top navigation node in tree.
207
* @return NodeElement
209
protected function get_top_navigation_node($nodetext) {
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());
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 ."]]" .
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 ."]]" .
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);
240
* Helper function to get sub-navigation node.
242
* @throws ExpectationException if note not found.
243
* @param string $nodetext node to find.
244
* @param NodeElement $parentnode parent navigation node.
245
* @return NodeElement.
247
protected function get_navigation_node($nodetext, $parentnode = null) {
249
// Avoid problems with quotes.
250
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext);
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);
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);
264
throw new ExpectationException('Sub-navigation node "' . $nodetext . '" not found under "' . $parentnode->getText() . '"', $this->getSession());