我正在检查我的selenium脚本是否完成ajax调用,我首先检查文档是否准备就绪然后检查是否定义了jQuery然后检查活动状态
public class WaitForAjaxToLoad {
public static void waitForAjaxToLoad(WebDriver driver) {
final JavascriptExecutor js= (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver, 15);
ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
if(js.executeScript("return document.readyState").equals("complete"))
{
if((boolean) js.executeScript("return window.jQuery != undefined")) {
Boolean status= (Boolean) js.executeScript("return jQuery.active==0");
System.out.println("Jquery is defined active status:"+status+"for thread:"+Thread.currentThread().getId());
if (status) {
return true;
} else {
return false;
}
}
else {
System.out.println("jQuery is undefined for thread "+Thread.currentThread().getId());
return false;
}
}
else {
System.out.println("Document is not ready for thread "+Thread.currentThread().getId());
return false;
}
}
};
wait.until(e);
}
}
当我执行我的脚本时,文档已准备好但jQuery未定义。 当测试方法并行执行时会发生这种情况。 一旦文档准备就绪,为什么我的jQuery未定义?
答案 0 :(得分:0)
为Java调整this answer,您的测试可以大大简化 - 在我看来,没有丢失任何有价值的内容:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(new Function<WebDriver, WebElement>() {
public Boolean apply(WebDriver driver) {
return js.executeScript("return jQuery.active == 0");
}
});