java - 即使测试用例失败也执行 Selenium 脚本

标签 java selenium scripting selenium-webdriver testcase

实际上,我正在尝试使用 TestNG 在 Eclipse 中运行 Web 应用程序的测试用例。但我在运行 Selenium 脚本时遇到了一些问题。即使某些测试用例失败,我只想继续执行。但我不知道该怎么做。

friend 们,我对这个话题很陌生。请帮我..!!!无论如何,提前致谢。

最佳答案

在 TestNG 中使用 alwaysRun = true 注释并不能完全解决您的问题。

为了让 Selenium 在偶尔出现 Exception 的情况下也能够继续运行,您需要使用 FluentWait 类定义一个 Wait 对象,如下所示:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring( NoSuchElementException.class, ElementNotFoundException.class );
// using a customized expected condition
WebElement foo1 = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply( WebDriver driver ) {
       // do something here if you want
       return driver.findElement( By.id("foo") );
     }
   });
// using a built-in expected condition
WebElement foo2 = wait.until( ExpectedConditions.visibilityOfElementLocated(
     By.id("foo") );

这使您能够在每次调用 .findElement 时忽略异常,直到达到某个预先配置的超时为止。

关于java - 即使测试用例失败也执行 Selenium 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18744775/

相关文章:

java - 我正在尝试使用 Java 从 Oracle SQL 生成 Excel 格式的数据,但我找不到方法

java - Spring Cache 中的@Cacheable 在缓存外的redis 中存储值。我如何将它放入redis的缓存中?

java - 在 Eclipse 中的单独 JVM 中运行每个 JUnit 测试?

Selenium WebDriver - 从隐藏选择中获取选项

ruby - 如何在 goto 之前设置 cookie?

c# - 如何使用 Selenium c# 将剪贴板中的 url 粘贴到 Chrome 中的地址栏?

linux - PostgreSQL 无密码提示 : . pgpass 被忽略

java - 如何检测并删除数组中两个以上的重复项?

python - 在添加新数据之前删除上个月

powershell - 比较 Powershell 中的两个列表