我有一个可能根据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来解决该问题,但我根本不喜欢硬等待。因此,我想知道是否有更好的方法来解决此问题。
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();
}
这将解决您的问题。
// 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);
}
}