如何在wait.until上不失败(预期条件)

问题描述 投票:1回答:2

我有一个可能根据URL显示也可能不显示的表。现在,我的测试将遍历所有给定的URL。我必须单击搜索按钮,然后才能验证表的存在。即使表清晰可见,我的表最初还是失败了。我添加了以下代码来解决该问题:

WebDriverWait wait = new WebDriverWait(data.Driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("abc")));
Boolean tableVisibility = data.Driver.findElements(By.id("12345abcd")).size() > 0;
if (tableVisibility == true) {
    result.logInfo("table displaying successfully");
}
Boolean isElmPresent =  data.Driver.findElements(By.id("labelMessage")).size() > 0;
if (isPresent == true) {
    this.updateDBTbl(abc,xyz);
}

此语句帮助我解决了表加载的问题。但是现在我遇到了找不到表的问题。实际上,当找不到表时,我们得到一个新标签,上面提到“我们需要联系系统服务台”,我需要在数据库中报告该标签。但是在使用wait.until语句的情况下,如果30秒后仍未看到该表,则会出错并停止执行。因此,标签永远不会得到验证。我现在已经注释掉了[[wait.until statement,而是添加了Thread.Sleep来解决该问题,但我根本不喜欢硬等待。因此,我想知道是否有更好的方法来解决此问题。

java selenium-webdriver webdriverwait
2个回答
0
投票
尝试一下:

try{ wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("abc"))); Boolean tableVisibility = data.Driver.findElements(By.id("12345abcd")).size() > 0; if (tableVisibility==true) { result.logInfo("table displaying successfully"); } Boolean isElmPresent= data.Driver.findElements(By.id("labelMessage")).size() > 0; if (isPresent == true) { this.updateDBTbl(abc,xyz); } }catch(ElementNotVisibleException e){ e.printStackTrace(); }

这将解决您的问题。

-1
投票
您的逻辑流程需要稍微调整。您需要:

    等待表格出现
    1. 如果存在,则记录成功
  1. 如果不存在,则记录失败和日志标签消息
  • // not sure what this does wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("abc"))); // is table present? try { wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("12345abcd"))); result.logInfo("table displaying successfully"); } catch (TimeoutException e) { // table is not present result.logInfo("table NOT displaying successfully"); // report contact system desk message if (data.Driver.findElements(By.id("labelMessage")).size() > 0) { this.updateDBTbl(abc,xyz); } }

  • © www.soinside.com 2019 - 2024. All rights reserved.