Search code examples
phptestingbehatmink

Behat MinkExtension traversing, counting css-selectors


i need to test, how many css-selectors appear on webseite. I geht only huge array with nodes, which match on my condition. When i count rows within array, i get "five", but it schould be "three"

My code:

/**
 * @Then /^I should see more then one css slider-selector "([^"]*)"$/
 * @Then /^I should see more then one CSS slider-selector "([^"]*)"$/
 */
public function iShouldSeeMoreThenOneCssSliderSelector($css_selector) {
    $nodes = $this->getSession()->getPage()->findAll("css", $css_selector);

    $counter = 0;
    foreach($nodes as $row){
        if(!empty($row)) {$counter +=1;}
    }
    if($counter<2){
        throw new \Exception(sprintf("The page '%s' does not contain enough pictures for slider '%s'", $this->getSession()->getCurrentUrl(), $css_selector));
    }
}

But when i look on html-source, i have only three matches. This code tells, that i have 5 machtes. I guess, i don´t test correctly. Has anybody idea?


Solution

  • Please check the same selector manually and debug the scenario to see if something else happens like page is not loaded, it is not navigating to the right page or something else happens that affects the result.

    For counting the elements found you can use count() method instead of foreach loop.

    $counter = count($nodes);