我需要使用 Selenium 测试动态 Web 元素,该元素会在一段时间后更改其值(取决于后端)。因此,我构建了一个 boolean 方法,如果 Web 元素具有我需要的值,则返回 true;如果从未检索到该值,则返回 false。我想以一定的时间间隔检查值的变化(因此,页面刷新之间的 Thread.sleep )。我的代码总是返回 true,我做错了什么?
public boolean checkStatus() throws InterruptedException {
for(int i=0; i<2;) {
if (!serviceStatus.serviceElement().equals("LIVE")) {
Thread.sleep(5000);
theBrowser.navigate().refresh();
i++;
}else{
return true;
}
}
return false;
}
该方法用于以下断言的主要测试:
Ensure.that(checkStatus()).isTrue();
最佳答案
我假设 serviceElement() 是 Webelement,您必须使用 getText() 方法获取元素的文本,并在这种情况下与您想要的字符串(“LIVE”)进行比较。您似乎正在将 Webelement 与串在这里。如果我正确理解了要求,下面应该可以工作。
public boolean checkStatus() throws InterruptedException {
boolean isMatching = false;
for(int i=0; i<2;) {
if (!serviceStatus.serviceElement().getText().equals("LIVE")) {
Thread.sleep(5000);
theBrowser.navigate().refresh();
i++;
isMatching = false;
break;
}else{
isMatching = true;
}
}
return isMatching;
}
关于java - 检查 Web 元素值的 boolean 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61524109/