我有一个简单的C#方法,在继续我的Selenium WebDriver测试之前等待所有jQuery AJAX请求完成。
我想要的是确切地知道当我达到最大等待时间时哪些请求待处理。有没有办法做这样的事情?我在网页和jQuery文档中搜索但找不到任何内容。
public void WaitForAjax(IWebDriver driver)
{
const int ajaxSleepInMilliseconds = 250;
int counter = 0;
const int maxSleeps = 40;
while (counter < maxSleeps)
{
var javaScriptExecutor = driver as IJavaScriptExecutor;
var ajaxIsComplete = javaScriptExecutor != null &&
(bool) javaScriptExecutor.ExecuteScript("return jQuery.active == 0");
if (ajaxIsComplete)
{
break;
}
Thread.Sleep(ajaxSleepInMilliseconds);
counter += 1;
}
if (counter >= maxSleeps)
{
var currentAjaxCalls = ""; // ???
throw new WebDriverException("Maximum number of WaitForAjax Thread.Sleeps were hit. Waited for AJAX " +
"for: " + ((maxSleeps * ajaxSleepInMilliseconds) / 60) + " seconds; the calls in progress were: " +
currentAjaxCalls);
}
}
答案 0 :(得分:0)