Java WebDriver等待,不同的等待条件
我正在使用带有Java绑定的WebDriver。我正在使用通用方法进行元素等待。其中一个叫做waitByPageTitle 以下是我对该方法的定义:Java WebDriver等待,不同的等待条件,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在使用带有Java绑定的WebDriver。我正在使用通用方法进行元素等待。其中一个叫做waitByPageTitle 以下是我对该方法的定义: public void waitByPageTitle(WebDriver driver, String pageTitle) { WebDriverWait wait = new WebDriverWait(driver, DEFAULT_IMPLICIT_WAIT); try { wait
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方法,使其能够接受多个参数,并且可以由其中任何一个参数等待,哪一个参数是它最先看到的
谢谢。您可以使用和Java
//此方法将接受任意数量的标题
public void waitUntilTextChanges(WebDriver驱动程序、字符串…标题){
新FluentWait(驱动程序)
.带超时(60,时间单位。秒)
.pollingEvery(10,时间单位为毫秒)
.until(新谓词(){
公共布尔应用(WebDriver d){
布尔标题匹配=假;
//获取当前窗口标题
字符串windowTitle=driver.getTitle();
for(字符串标题:标题){
//迭代所有输入标题并与窗口标题进行比较
titleMatched=windowTitle.equalsIgnoreCase(标题);
//如果找到匹配项,则退出
如果(标题匹配){
打破
}
}
返回标题匹配;
}
});
}
通用方法保持不变,在调用之前,您必须构建要验证的标题字符串,您可以确保要验证的标题字符串与窗口标题不同,然后您可以根据事件拥有更多选项,并构建最终标题字符串,该字符串将传递给waitForPageTitle()
method从字符串切换到字符串数组,然后使用wait.until(bool)。因为它是bool,所以您应该能够为stringArray中的每个字符串执行
// 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;
}
});
}