java - TestNG Java 硒。Thread.sleep() 的替代方法是什么
问题描述
根据我看到的一些文章,使用 Thread.sleep() 似乎是不受欢迎的。我在我的测试类中经常使用它,需要等待加载。我曾尝试使用此方法告诉我加载何时完成,但这没有帮助。这不是一个可靠的方法。:
try {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded")
|| ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
} catch (Exception e) {
System.err.println(thisClass + " Exception caught: " + e.getMessage());
}
我可以使用哪些替代方法?
该解决方案有效:
调用函数:
String className = "gwt-Button form-control btn back-button ripple-container"; String textToFind = "back-button-icon"; String htmlElement = "button"; boolean isReady = Common.FluentWait(driver, 60, className, textToFind, htmlElement);
那个功能:
public static boolean FluentWait(WebDriver driver, int timeOut, String className, String textToFind, String htmlElement) { // Waiting timeOut seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Common.myPrint(thisClass + " FluentWait. "); Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeOut, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { Common.myPrint(thisClass + " run returnWebElement. "); return returnWebElement(className, textToFind, htmlElement, driver); } }); if (foo != null) { return true; } return false;
}
以及我在其中调用的函数:
public static WebElement returnWebElement(String className, String textToFind, String htmlElement,
WebDriver driver) {
List<WebElement> elements = Common.findElementsUsingHtmlXpathClass(driver, htmlElement, className);
Common.myPrint(thisClass + " elements count: " + elements.size());
String text = "";
for (WebElement element : elements) {
// select an element
if (element.isDisplayed()) {
text = element.getAttribute("innerHTML");
if (text != "") {
text = text.trim();
if (text.contains(textToFind)) {
Common.myPrint(thisClass + " innerHTML: " + text);
Common.myPrint(thisClass + " returning element found. ");
return element;
}
}
}
}
Common.myPrint(thisClass + " element not found. ");
return null;
}
解决方案
您可以使用 FluentWait
每个 FluentWait 实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时的 NoSuchElementExceptions。
/*
code snippet will Wait 30 seconds for
an element to be present on the page and check for its
presence once every 5 seconds.
*/
Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
另一个等待也可用
推荐阅读
- javascript - javascript - 将对象数组中的 2 个对象合并为一个对象
- python - 裁剪图像后转换 Numpy 数组
- angular - Atlassian Bamboo 无法发布工件
- reactjs - 使用状态钩子触发一系列事件
- javascript - Electron - ipcRenderer 在 main.js 中不起作用
- mysql - 如何将 mysql 距离查询转换为 Laravel Eloquent 查询?- (带有纬度、经度和半径参数)
- python - 为什么python认为“自我”是一个论点
- scala - 是否可以在 Scala 3 中使用类似于打字稿映射类型的另一种类型来包装成员类型?
- python - 使用 QuantLib Python 使用 Heston 模型对亚洲期权定价
- java - 尝试更新多张图片。如果我更新单个图像但更新两个或多个图像,则下面的代码有效