等待ajax请求完成-selenium webdriver
我被困在一个点上,我希望我的脚本等待ajax请求完成 我试过: 1.在慢速网络模式下运行应用程序,单击创建按钮并选中JQuery.active返回1。只有ajax调用发生 问题是整个页面并没有刷新,只是页面上的一些数据每次都会更新 我使用显式等待,直到加载程序/进度条消失,但我被要求特别等待ajax完成,因为UI可能很快就会改变 我尝试实施:等待ajax请求完成-selenium webdriver,ajax,selenium-webdriver,Ajax,Selenium Webdriver,我被困在一个点上,我希望我的脚本等待ajax请求完成 我试过: 1.在慢速网络模式下运行应用程序,单击创建按钮并选中JQuery.active返回1。只有ajax调用发生 问题是整个页面并没有刷新,只是页面上的一些数据每次都会更新 我使用显式等待,直到加载程序/进度条消失,但我被要求特别等待ajax完成,因为UI可能很快就会改变 我尝试实施: public boolean waitForJSandJQueryToLoad() { WebDriverWait wait = new Web
public boolean waitForJSandJQueryToLoad() {
WebDriverWait wait = new WebDriverWait(driver, 30);
// wait for jQuery to load
ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
return ((Long)((JavascriptExecutor)getDriver()).executeScript("return jQuery.active") == 0);
}
catch (Exception e) {
// no jQuery present
return true;
}
}
};
// wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)getDriver()).executeScript("return document.readyState")
.toString().equals("complete");
}
};
return wait.until(jQueryLoad) && wait.until(jsLoad);
}
public boolean waitForJSandJQueryToLoad(){
WebDriverWait wait=新的WebDriverWait(驱动程序,30);
//等待jQuery加载
ExpectedCondition jQueryLoad=新的ExpectedCondition(){
@凌驾
公共布尔应用(WebDriver驱动程序){
试一试{
return((Long)((JavascriptExecutor)getDriver()).executeScript(“return jQuery.active”)==0;
}
捕获(例外e){
//不存在jQuery
返回true;
}
}
};
//等待Javascript加载
ExpectedCondition jsLoad=新的ExpectedCondition(){
@凌驾
公共布尔应用(WebDriver驱动程序){
return((JavascriptExecutor)getDriver()).executeScript(“return document.readyState”)
.toString()等于(“完成”);
}
};
返回wait.until(jQueryLoad)和&wait.until(jsLoad);
}
但它不起作用。我的测试失败了,因为断言是在ajax完成和数据更新之前调用的
有人能给我一个有效的解决方案:
1.检查AJAX是否正在运行。
2.等待ajax完成。
3.等待数据更新/刷新,而不是整个页面
谢谢 下面是我用来等待Ajax完成的代码:
public static void waitForAjaxToFinish() {
WebDriverWait wait = new WebDriverWait(driver, 5000);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver wdriver) {
return ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0").equals(true);
}
});
}
希望这有帮助。以下是我用来等待Ajax完成的代码:
public static void waitForAjaxToFinish() {
WebDriverWait wait = new WebDriverWait(driver, 5000);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver wdriver) {
return ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0").equals(true);
}
});
}
希望这能有所帮助。您提到的用例,例如检查AJAX是否正在运行,等待AJAX完成并等待数据更新/刷新,而不是整个页面都是琐碎的。您的实际用例应该是明确的,并且必须与页面上的WebElements交互相关。你能用你确切的用例、相关的HTML和代码测试更新这个问题吗?这里是确切的用例:你提到的用例,例如检查AJAX是否正在运行,等到AJAX完成,等到数据更新/刷新,而不是整个页面都是琐碎的。您的实际用例应该是明确的,并且必须与页面上的WebElements交互相关。您能用您的确切用例、相关HTML和代码试用更新问题吗?以下是确切的用例: