使用带有 Java 的 Selenium WebDriver 断言 WebElement 不存在 [英] Assert that a WebElement is not present using Selenium WebDriver with java

查看:29
本文介绍了使用带有 Java 的 Selenium WebDriver 断言 WebElement 不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我编写的测试中,如果我想断言页面上存在 WebElement,我可以做一个简单的:

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 这就是我想出自己的修复方法的方式,我想知道是否还有更好的方法.

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)");
  }
}

推荐答案

不确定您指的是哪个版本的 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

  • assertNotSomethingSelected
  • assertTextNotPresent

等等.

这篇关于使用带有 Java 的 Selenium WebDriver 断言 WebElement 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆