For each element with behavior or code

I want to test a website with a dynamic structure. I want to skip all the menu items and run the same series of tests on every page. We are talking about over 100 pages that change reguraly.

I would like to do this using either behavior or code.

Does anyone have an idea on how to do this?

+4
source share
2 answers

When using Behat with Mink, you can capture menu items with findAll () and then iterate over it:

/** * @When /^I run my test series for all menu items$/ */ public function iRunMyTestSeriesForAllMenuItems() { $result = TRUE; $this->getSession()->visit('http://www.example.com/'); $links = $this->getSession()->getPage()->findAll('css', '#menu ul li a'); foreach ($links as $link) { $url = $link->getAttribute('href'); if (FALSE === $this->yourTestHere($url)) { $result = FALSE; } } return $result; } 
0
source

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.

 /** * @Then /^I should access all pages of site map "([^"]*)"$/ */ public function iShouldAccessAllPagesOfSiteMap($selector) { $page = $this->getSession()->getPage(); $locator = sprintf('#%s a', $selector); $elements = $page->findAll('css', $locator); $steps = array(); foreach ($elements as $element) { /** @var \Behat\Mink\Element\NodeElement $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; } /** * @When /^I print out page "([^"]*)"$/ */ 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

+1
source

Source: https://habr.com/ru/post/1480278/


All Articles