Behat Test more than number of elements
There is a Mink step to check for the number of elements
//MinkDictionary.php
/**
* Checks, that (?P<num>\d+) CSS elements exist on the page
*
* @Then /^(?:|I )should see (?P<num>\d+) "(?P<element>[^"]*)" elements?$/
*/
public function assertNumElements($num, $element)
{
$this->assertSession()->elementsCount('css', $element, intval($num));
}
But I needed one just to make sure there are some elements on the page, more than 1 for example on a reports page. I guess I could have checked for just 1 eg “#behat ol li” but I think this can come in handy for our reporting tests.
//FeatureContext.php
/**
* Check that there are more than or = to a number of elements on a page
*
* @Then /^I should see more "([^"]*)" or more "([^"]*)" elements$/
*/
public function iShouldSeeMoreOrMoreElements($num, $element)
{
$container = $this->getSession()->getPage();
$nodes = $container->findAll('css', $element);
if (intval($num) > count($nodes)) {
$message = sprintf('%d elements less than %s "%s" found on the page, but should be %d.', count($nodes), $selectorType, $selector, $count);
throw new ExpectationException($message, $this->session);
}
}
comments powered by Disqus