diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index e59bb584a81..d01213b4fec 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -944,4 +944,22 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { } return $DB->get_record('user', ['id' => $userid]); } + + /** + * Trigger click on node via javascript instead of actually clicking on it via pointer. + * + * This function resolves the issue of nested elements with click listeners or links - in these cases clicking via + * the pointer may accidentally cause a click on the wrong element. + * Example of issue: clicking to expand navigation nodes when the config value linkadmincategories is enabled. + * @param NodeElement $node + */ + protected function js_trigger_click($node) { + if (!$this->running_javascript()) { + $node->click(); + } + $this->ensure_node_is_visible($node); // Ensures hidden elements can't be clicked. + $xpath = $node->getXpath(); + $script = "Syn.click({{ELEMENT}})"; + $this->getSession()->getDriver()->triggerSynScript($xpath, $script); + } } diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index f49fdfbd750..9460ebd4896 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -290,21 +290,7 @@ class behat_navigation extends behat_base { if ($pnode && $this->running_javascript() && $pnode->hasAttribute('aria-expanded') && ($pnode->getAttribute('aria-expanded') == "false")) { - $this->ensure_node_is_visible($pnode); - - // If node is a link then some driver click in the middle of the node, which click on link and - // page gets redirected. To ensure expansion works in all cases, check if the node to expand is a - // link and if yes then click on link and wait for it to navigate to next page with node expanded. - $nodetoexpandliteral = behat_context_helper::escape($parentnodes[$i]); - $nodetoexpandxpathlink = $pnodexpath . "/a[normalize-space(.)=" . $nodetoexpandliteral . "]"; - - if ($nodetoexpandlink = $node->find('xpath', $nodetoexpandxpathlink)) { - $behatgeneralcontext = behat_context_helper::get('behat_general'); - $nodetoexpandlink->click(); - $behatgeneralcontext->wait_until_the_page_is_ready(); - } else { - $pnode->click(); - } + $this->js_trigger_click($pnode); // Wait for node to load, if not loaded before. if ($pnode->hasAttribute('data-loaded') && $pnode->getAttribute('data-loaded') == "false") {