Skip to content

Commit

Permalink
MDL-35590 behat: Fixed navigation steps to support aria attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja authored and lameze committed Jan 22, 2016
1 parent f889544 commit 10ac8ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
18 changes: 3 additions & 15 deletions completion/tests/behat/behat_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,10 @@ public function user_has_not_completed_activity($userfullname, $activityname) {
* @Given /^I go to the current course activity completion report$/
*/
public function go_to_the_current_course_activity_completion_report() {
$completionnode = get_string('pluginname', 'report_progress');
$reportsnode = get_string('courseadministration') . ' > ' . get_string('reports');

$steps = array();

// Expand reports node if we can't see the link.
try {
$this->find('xpath', "//div[@id='settingsnav']" .
"/descendant::li" .
"/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" .
"/descendant::p[contains(., '" . get_string('pluginname', 'report_progress') . "')]");
} catch (ElementNotFoundException $e) {
$steps[] = new Given('I expand "' . get_string('reports') . '" node');
}

$steps[] = new Given('I follow "' . get_string('pluginname', 'report_progress') . '"');

return $steps;
return new Given('I navigate to "' . $completionnode . '" node in "' . $reportsnode . '"');
}

/**
Expand Down
45 changes: 29 additions & 16 deletions lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ protected function get_node_text_node($text, $branch = false, $collapsed = null,
$nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
$hasblocktree = "[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]";
$hasbranch = "[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]";
$hascollapsed = "li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ') or @data-expandable='1']";
$notcollapsed = "li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]";
$hascollapsed = "li[@aria-expanded='false']";
$notcollapsed = "li[@aria-expanded='true']";
$match = "[normalize-space(.)={$nodetextliteral}]";

// Avoid problems with quotes.
Expand All @@ -79,12 +79,15 @@ protected function get_node_text_node($text, $branch = false, $collapsed = null,
$iscollapsed = 'li';
}

// First check root nodes.
// First check root nodes, it can be a span or link.
$xpath = "//ul{$hasblocktree}/$hascollapsed/p{$isbranch}/span{$match}|";
$xpath .= "//ul{$hasblocktree}/$hascollapsed/p{$isbranch}/a{$match}|";

// Next search for the node containing the text within a link.
$xpath .= "//ul{$hasblocktree}//{$notcollapsed}/ul/{$iscollapsed}/p{$isbranch}/a{$match}|";
$xpath .= "//ul{$hasblocktree}//ul/{$iscollapsed}/p{$isbranch}/a{$match}|";

// Finally search for the node containing the text within a span.
$xpath .= "//ul{$hasblocktree}//{$notcollapsed}/ul/{$iscollapsed}/p{$isbranch}/span{$match}";
$xpath .= "//ul{$hasblocktree}//ul/{$iscollapsed}/p{$isbranch}/span{$match}";

$node = $this->find('xpath', $xpath, $exception);
$this->ensure_node_is_visible($node);
Expand All @@ -108,10 +111,11 @@ public function navigation_node_should_be_expandable($nodetext) {

$node = $this->get_node_text_node($nodetext, true);
$node = $node->getParent();
if ($node->hasAttribute('data-expandable') && $node->getAttribute('data-expandable')) {
return true;
if ($node->hasClass('emptybranch')) {
throw new ExpectationException('The "' . $nodetext . '" node is not expandable', $this->getSession());
}
throw new ExpectationException('The "' . $nodetext . '" node is not expandable', $this->getSession());

return true;
}

/**
Expand All @@ -131,10 +135,11 @@ public function navigation_node_should_not_be_expandable($nodetext) {

$node = $this->get_node_text_node($nodetext);
$node = $node->getParent();
if ($node->hasAttribute('data-expandable') && $node->getAttribute('data-expandable')) {
throw new ExpectationException('The "' . $nodetext . '" node is expandable', $this->getSession());

if ($node->hasClass('emptybranch') || $node->hasClass('tree_item')) {
return true;
}
return true;
throw new ExpectationException('The "' . $nodetext . '" node is expandable', $this->getSession());
}

/**
Expand Down Expand Up @@ -257,17 +262,19 @@ public function i_navigate_to_node_in($nodetext, $parentnodes) {
}

// Keep expanding all sub-parents if js enabled.
if ($this->running_javascript() && $node->hasClass('collapsed')) {
if ($this->running_javascript() && $node->hasAttribute('aria-expanded') &&
($node->getAttribute('aria-expanded') == "false")) {

$xpath = "/p[contains(concat(' ', normalize-space(@class), ' '), ' tree_item ')]";
$nodetoexpand = $node->find('xpath', $xpath);

$this->ensure_node_is_visible($nodetoexpand);
$nodetoexpand->click();

// Wait for node to load, if not loaded before.
if ($nodetoexpand->hasAttribute('data-loaded') && $nodetoexpand->getAttribute('data-loaded') == 0) {
if ($nodetoexpand->hasAttribute('data-loaded') && $nodetoexpand->getAttribute('data-loaded') == "false") {
$jscondition = '(document.evaluate("' . $nodetoexpand->getXpath() . '", document, null, '.
'XPathResult.ANY_TYPE, null).iterateNext().getAttribute(\'data-loaded\') == 1)';
'XPathResult.ANY_TYPE, null).iterateNext().getAttribute(\'data-loaded\') == "true")';

$this->getSession()->wait(self::EXTENDED_TIMEOUT * 1000, $jscondition);
}
Expand Down Expand Up @@ -320,8 +327,14 @@ protected function get_top_navigation_node($nodetext) {
"//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);
"/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 ')]" .
"/a[normalize-space(.)=" . $nodetextliteral ."]]";

$node = $this->find('xpath', $xpath, $exception);

return $node;
}
Expand Down

0 comments on commit 10ac8ba

Please sign in to comment.