在Codeception测试中循环浏览页面上的链接 [英] Loop through links on a page in Codeception Tests
问题描述
我正在使用Codeception和PHPBrowser Webdriver编写一些功能测试。
I'm writing some functionality tests using Codeception and the PHPBrowser webdriver.
Codeception使用CSS或XPath中的特定引用来检查页面上的元素。
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);
}
然后测试可以使用CSS伪选择器引用链接:
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');
}
我找不到任何方法可以使用PHPbrowser或编码接收方法,我不确定是否将其抓取的页面显示为要遍历的对象。
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.
推荐答案
您是否尝试过使用grabMultiple方法?
http://codeception.com/docs/modules/PhpBrowser#grabMultiple
Have you tried using grabMultiple method? http://codeception.com/docs/modules/PhpBrowser#grabMultiple
它返回一个单一属性值的列表,但是如果所有项目都具有id或href属性,就足够了。
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.
将getMultiple方法复制到Helper\Functional类,并根据需要对其进行重命名和修改。
http://codeception.com/docs/06-ReusingTestCode#Modules和助手
Copy getMultiple method to Helper\Functional class, rename and modify it as you wish. http://codeception.com/docs/06-ReusingTestCode#Modules-and-Helpers
这篇关于在Codeception测试中循环浏览页面上的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!