在 PHPUnit 的 Selenium 扩展中使用 glob 来识别元素
我正在使用 Selenium 扩展在 PHPUnit 中编写一个 Selenium 测试用例。在测试中,我断言页面上存在某个链接。
例如,假设我想断言页面中存在以下链接:
<a href="http://www.stackoverflow.com">click here to enter stackoverflow</a>
为了断言该链接存在,我需要识别它。通常我们使用锚文本来识别链接:assertElementPresent("link=click here to enter stackoverflow")
。
我不想使用整个锚文本来识别链接,而只是使用锚文本包含的子字符串“stackoverflow”。我可以使用 glob 和星号运算符来执行此操作: assertElementPresent("link=glob:*stackoverflow*")
这在 selenium IDE 上完美运行,但是在 PHPUnit 中编写等效代码时,断言失败。下面是 PHPUnit 中的等效代码行:
$this->assertTrue(isElementPresent("link=glob:*stackoverflow*"));
上面的断言失败,因为元素 link=glob:*stackoverflow*
未被识别。应该识别该元素,因为页面上存在一个链接,其锚文本包含字符串“stackoverflow”。
我的问题比我给出的简单示例更笼统:How can I use glob to recognize elements in the Selenium extension to PHPUnit?
I'm writing a Selenium test case in PHPUnit using the Selenium extension. In the test, I assert that a certain link is present on the page.
For example, say I want to assert that the following link exists in the page:
<a href="http://www.stackoverflow.com">click here to enter stackoverflow</a>
In order to assert that the link is present I need to identify it. Usually we identify links using their anchor text: assertElementPresent("link=click here to enter stackoverflow")
.
I don't want to identify the link using its whole anchor text, but just the sub-string "stackoverflow" which the anchor text contains. I can use glob and the star operator in order to do so: assertElementPresent("link=glob:*stackoverflow*")
This works perfectly on selenium IDE, but when coding the equivalent in PHPUnit the assertion fails. Here is the equivalent line of code in PHPUnit:
$this->assertTrue(isElementPresent("link=glob:*stackoverflow*"));
The above assertion fails because the element link=glob:*stackoverflow*
is not identified. The element should be identified because there exists a link on the page which its anchor text contains the string "stackoverflow".
My question is more general than the simple example I've given: How can I use glob to identify elements in the Selenium extension to PHPUnit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们在所有 Selenium 测试用例中都使用 XPath 表达式,但我没有参与编写它们,也没有研究其他选项。如果你找不到让
glob
工作的方法,你可以随时依靠这个:We use XPath expressions in all of our Selenium test cases, but I wasn't involved in writing them and didn't investigate other options. If you cannot find a way to get
glob
to work, you can always fall back on this: