gpt4 book ai didi

java - WebDriver等待,不同的等待条件

转载 作者:行者123 更新时间:2023-12-01 13:18:12 25 4
gpt4 key购买 nike

我正在将 WebDriver 与 Java 绑定(bind)一起使用。我正在使用通用方法进行元素等待。其中之一称为 waitByPageTitle。

这是我对此方法的定义:

public void waitByPageTitle(WebDriver driver, String pageTitle) {
WebDriverWait wait = new WebDriverWait(driver, DEFAULT_IMPLICIT_WAIT);
try {
wait.until(ExpectedConditions.titleContains(pageTitle));
} catch (TimeoutException e) {
...
}
}

在我的页面对象中,当方法需要等待页面标题时,我将参数传递给该方法。但在某些情况下,页面标题可能会根据不同的事件而有所不同。如何更改通用 waitByPageTitle 方法,以便它接受多个参数,并且可以等待其中任何一个(无论它看到第一个参数)吗?

谢谢。

最佳答案

您可以使用FluentWait和 Java varargs

 // This method will accept any number of titles
public void waitUntilTextChanges(WebDriver driver, String... titles) {

new FluentWait<WebDriver>(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(10, TimeUnit.MILLISECONDS)
.until(new Predicate<WebDriver>() {

public boolean apply(WebDriver d) {
boolean titleMatched = false;
// Get current window title
String windowTitle = driver.getTitle();
for(String title : titles){
// Iterate through all input titles and compare with window title
titleMatched = windowTitle.equalsIgnoreCase(title);
// If match found, exit
if(titleMatched){
break;
}
}
return titleMatched;
}
});
}

关于java - WebDriver等待,不同的等待条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com