使用 Selenium WebDriver 和 java 断言 WebElement 不存在
在我编写的测试中,如果我想断言页面上存在 WebElement,我可以做一个简单的操作:
driver.findElement(By.linkText("Test Search"));
如果存在,则通过,如果不存在,则失败。但现在我想断言链接不存在。我不清楚如何执行此操作,因为上面的代码不返回布尔值。
编辑这就是我自己想出的解决办法,我想知道是否还有更好的方法。
public static void assertLinkNotPresent (WebDriver driver, String text) throws Exception {
List<WebElement> bob = driver.findElements(By.linkText(text));
if (bob.isEmpty() == false) {
throw new Exception (text + " (Link is present)");
}
}
In tests that I write, if I want to assert a WebElement is present on the page, I can do a simple:
driver.findElement(By.linkText("Test Search"));
This will pass if it exists and it will bomb out if it does not exist. But now I want to assert that a link does not exist. I am unclear how to do this since the code above does not return a boolean.
EDIT This is how I came up with my own fix, I'm wondering if there's a better way out there still.
public static void assertLinkNotPresent (WebDriver driver, String text) throws Exception {
List<WebElement> bob = driver.findElements(By.linkText(text));
if (bob.isEmpty() == false) {
throw new Exception (text + " (Link is present)");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
这样做更容易:
It's easier to do this:
我认为你可以捕获
org.openqa.selenium.NoSuchElementException
如果没有这样的元素,它将由driver.findElement
抛出:I think that you can just catch
org.openqa.selenium.NoSuchElementException
that will be thrown bydriver.findElement
if there's no such element:不确定您指的是哪个版本的 selenium,但是 selenium * 中的一些命令现在可以执行此操作:
http://release.seleniumhq.org/selenium-core/0.8.0 /reference.html
..
Not Sure which version of selenium you are referring to, however some commands in selenium * can now do this:
http://release.seleniumhq.org/selenium-core/0.8.0/reference.html
Etc..
有一个名为
ExpectedConditions
的类:There is an Class called
ExpectedConditions
:试试这个 -
Try this -
使用 Selenium Webdriver 会是这样的:
With Selenium Webdriver would be something like this:
看起来
findElements()
只有在找到至少一个元素时才会快速返回。否则,它会在返回零元素之前等待隐式等待超时 - 就像findElement()
一样。为了保持良好的测试速度,此示例在等待元素消失时暂时缩短了隐式等待:
尽管仍在寻找一种完全避免超时的方法......
It looks like
findElements()
only returns quickly if it finds at least one element. Otherwise it waits for the implicit wait timeout, before returning zero elements - just likefindElement()
.To keep the speed of the test good, this example temporarily shortens the implicit wait, while waiting for the element to disappear:
Still looking for a way to avoid the timeout completely though...
您可以利用 Arquillian Graphene 框架来实现此目的。因此,您的情况的例子可能是
Is 还为您提供了一堆很好的 API,用于使用 Ajax、流畅的等待、页面对象、片段等。它确实大大简化了基于 Selenium 的测试开发。
You can utlilize Arquillian Graphene framework for this. So example for your case could be
Is also provides you bunch of nice API's for working with Ajax, fluent waits, page objects, fragments and so on. It definitely eases a Selenium based test development a lot.
对于node.js,我发现以下是等待元素不再存在的有效方法:
For node.js I've found the following to be effective way to wait for an element to no longer be present:
不是问题的答案,但可能是基本任务的想法:
当您的站点逻辑不应显示某个元素时,您可以插入一个您检查的不可见的“标志”元素。
Not an answer to the very question but perhaps an idea for the underlying task:
When your site logic should not show a certain element, you could insert an invisible "flag" element that you check for.
请查找下面使用 Selenium“until.stalenessOf”和 Jasmine 断言的示例。
当元素不再附加到 DOM 时,它返回 true。
供参考: Selenium:Until Doc
Please find below example using Selenium "until.stalenessOf" and Jasmine assertion.
It returns true when element is no longer attached to the DOM.
For ref.: Selenium:Until Doc
我发现最好的方法 - 并且在 Allure 报告中显示为失败 - 是尝试捕获findelement并在catch块中将assertTrue设置为false,如下所示:
The way that I have found best - and also to show in Allure report as fail - is to try-catch the findelement and in the catch block, set the assertTrue to false, like this:
这对我来说是最好的方法
This is the best approach for me
对于 JavaScript(支持 TypeScript)实现,我想出了一些虽然不是很漂亮但有效的方法:
PS:我正在使用 "selenium-webdriver": "^4.1.1"
For a JavaScript (with TypeScript support) implementation I came up with something, not very pretty, that works:
P.S: I am using "selenium-webdriver": "^4.1.1"
findElement 将检查 html 源,即使该元素未显示也会返回 true。要检查元素是否显示,请使用 -
findElement will check the html source and will return true even if the element is not displayed. To check whether an element is displayed or not use -
适用于appium 1.6.0及以上版本
For appium 1.6.0 and above