问题
I'm writing some functionality tests using Codeception and the PHPBrowser webdriver.
Codeception uses specific references in CSS or XPath to check elements on a page.
But I want to be able to loop through all the links in my menu, click on it and run a test to see if the link is working.
ie. if my menu looks like this:
<ul id='nav'>
<li><a>Link1</a></li>
<li><a>Link2</a></li>
<li><a>Link3</a></li>
<li><a>Link4</a></li>
<li><a>Link5</a></li>
</ul>
I want to be able to loop through the links and pass its index:
$links = somefunctiontogetalllinks();
for ($x = 0; $x <= count($links); $x++) {
codeceptionTest($x);
}
And then the test can refer to the link using CSS pseudo selectors:
public function codeceptionTest($index) {
$i->click('#nav > li:nth-child(' . $index . ') > a');
$I->see('Page Content');
}
I can;t find any way to get a collection of elements using PHPbrowser or Codeception methods, and I'm unsure whether either presents the page it scrapes as an object to traverse.
回答1:
Have you tried using grabMultiple method? http://codeception.com/docs/modules/PhpBrowser#grabMultiple
https://github.com/Codeception/Codeception/blob/faa5fb331eb15760bc88f284183f367df5a26a39/src/Codeception/Lib/InnerBrowser.php#L948
It returns a list of values of single attribute, but it should be good enough if all your items have id or href attribute.
If you actually want to get nodes, implement your own method.
Copy getMultiple method to Helper\Functional class, rename and modify it as you wish. http://codeception.com/docs/06-ReusingTestCode#Modules-and-Helpers
来源:https://stackoverflow.com/questions/32431362/loop-through-links-on-a-page-in-codeception-tests