Skip to content

Commit

Permalink
MDL-59470 behat: Add a element comtains x occurrences step
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocolate-lightning committed Aug 8, 2019
1 parent c48fbb6 commit 71fd7b1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/tests/behat/behat_general.php
Original file line number Diff line number Diff line change
Expand Up @@ -1799,4 +1799,41 @@ public function i_click_on_skipping_visibility_check($element, $selectortype) {
$node = $this->get_selected_node($selectortype, $element);
$this->js_trigger_click($node);
}

/**
* Checks, that the specified element contains the specified text a certain amount of times.
* When running Javascript tests it also considers that texts may be hidden.
*
* @Then /^I should see "(?P<elementscount_number>\d+)" occurrences of "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
* @throws ElementNotFoundException
* @throws ExpectationException
* @param int $elementscount How many occurrences of the element we look for.
* @param string $text
* @param string $element Element we look in.
* @param string $selectortype The type of element where we are looking in.
*/
public function i_should_see_occurrences_of_in_element($elementscount, $text, $element, $selectortype) {

// Getting the container where the text should be found.
$container = $this->get_selected_node($selectortype, $element);

// Looking for all the matching nodes without any other descendant matching the
// same xpath (we are using contains(., ....).
$xpathliteral = behat_context_helper::escape($text);
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";

$nodes = $this->find_all('xpath', $xpath, false, $container);

if ($this->running_javascript()) {
$nodes = array_filter($nodes, function($node) {
return $node->isVisible();
});
}

if ($elementscount != count($nodes)) {
throw new ExpectationException('Found '.count($nodes).' elements in column. Expected '.$elementscount,
$this->getSession());
}
}
}

0 comments on commit 71fd7b1

Please sign in to comment.