I had a similar use case when I wanted to visit all the pages of this site map, making sure there were no dead links. I had an approach to dynamically generate an array of steps , which is then returned and processed by Behat. I had to add an artificial step “I print the page” to make sure that I see on the console which page is currently being tested.
public function iShouldAccessAllPagesOfSiteMap($selector) { $page = $this->getSession()->getPage(); $locator = sprintf('#%s a', $selector); $elements = $page->findAll('css', $locator); $steps = array(); foreach ($elements as $element) { $steps[] = new Behat\Behat\Context\Step\When(sprintf('I print out page "%s"', $element->getAttribute('href'))); $steps[] = new Behat\Behat\Context\Step\When(sprintf('I go to "%s"', $element->getAttribute('href'))); $steps[] = new Behat\Behat\Context\Step\Then('the response status code should be 200'); } return $steps; } public function iPrintOutThePage($page) { $string = sprintf('Visiting page ' . $page); echo "\033[36m -> " . strtr($string, array("\n" => "\n| ")) . "\033[0m\n"; }
Then my script looks like this:
Scenario: my website has no "dead" pages Given I am on "/examples/site-map/" Then I should access all pages of site map "c118"
The whole Gist is here https://gist.github.com/fudriot/6028678
source share