等待使用Selenium2WebDriver完成Ajax调用

本文关键字:Ajax 调用 完成 Selenium2WebDriver 等待 | 更新日期: 2023-09-27 17:58:04

我正在使用Selenium 2 WebDriver来测试一个使用AJAX的UI。

有没有一种方法可以让驱动程序等待Ajax请求完成。

基本上我有这个:

d.FindElement(By.XPath("//div[8]/div[3]/div/button")).Click();
// This click trigger an ajax request which will fill the below ID with content.
// So I need to make it wait for a bit.
Assert.IsNotEmpty(d.FindElement(By.Id("Hobbies")).Text);

等待使用Selenium2WebDriver完成Ajax调用

如果您对ajax请求使用jQuery,您可以等到jQuery.active属性为零。其他库可能有类似的选项。

public void WaitForAjax()
{
    while (true) // Handle timeout somewhere
    {
        var ajaxIsComplete = (bool)(driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
        if (ajaxIsComplete)
            break;
        Thread.Sleep(100);
    }
}

您也可以在这里使用Selenium显式等待。然后你不需要自己处理超时

public void WaitForAjax()
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
    wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
}
var wait = new WebDriverWait(d, TimeSpan.FromSeconds(5));
var element = wait.Until(driver => driver.FindElement(By.Id("Hobbies")));

基于Morten-Christiansens答案的Java解决方案

public void WaitForAjax()引发InterruptedException{while(true){Boolean ajaxIsComplete=(Boolean)((JavascriptExecutor)驱动程序).executeScript("return jQuery.active==0");if(ajaxIsComplete){打破}线程睡眠(100);}}

添加一个超时参数:只是一点改进

internal static void WaitForAllAjaxCalls(this ISelenium selenium, IWebDriver driver, int timeout = 40)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        while (true)
        {
            if (sw.Elapsed.Seconds > timeout) throw new Exception("Timeout");
            var ajaxIsComplete = (bool)driver.ExecuteScript("return jQuery.active == 0");
            if (ajaxIsComplete)
                break;
            Thread.Sleep(100);
        }            
    }

只是小的改进:

//Wait for Ajax call to complete
  public void WaitForAjax1() throws InterruptedException
    {
        while (true)
        {
            if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){
                break;
            }
            Thread.sleep(100);
        }
    }

这是我的代码:

public static void WaitForCommission (WebDriver driver) throws Exception {
    for (int second = 0;; second++) {
        if (second >= 30) fail("timeout");
        try { 
            if (IsElementActive(By.id("transferPurposeDDL"), driver)) 
                break; 
            } catch (Exception e) {}
        Thread.sleep(1000);
    }
}
private static boolean IsElementActive(By id, WebDriver driver) {
    WebElement we =  driver.findElement(id);        
    if(we.isEnabled())
        return true;
    return false;
}

这个代码真的很有效。

如果你使用石墨烯,你可以使用这个:

Graphene.waitModel().until((Predicate<WebDriver>) input -> (Boolean) ((JavascriptExecutor) input).executeScript("return jQuery.active == 0"));

"XMLHttpRequest"是用于向服务器发送Ajax请求的协议,因此存在这样的请求表示正在进行基于Ajax的操作。

有许多浏览器插件允许您监视浏览器发送的XMLHttp请求。我个人使用Firefox的Firebug插件,这是一个非常有用的工具。安装后,Firebug会在浏览器窗口的右下角显示一个类似Bug的图标。点击类似bug的图标会启动Firebug,如上图所示。选择"Net",然后选择"XHR"以启动XHR控制台,浏览器发送的所有XML HTTP请求都将显示在该控制台中。

尽可能避免使用thread.sleep()。这里有一段代码,它接受等待时间作为输入,并在指定的时间内运行秒表。

您可以将输入时间(以秒为单位)设置为30。

protected void WaitForAjaxToComplete(int timeoutSecs)
        {
            var stopWatch = new Stopwatch();
            try
            {
                while (stopWatch.Elapsed.TotalSeconds < timeoutSecs)
                {
                    var ajaxIsComplete = (bool)(WebDriver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
                    if (ajaxIsComplete)
                    {
                        break;
                    }
                }
            }
            //Exception Handling
            catch (Exception ex)
            {
                stopWatch.Stop();
                throw ex;
            }
            stopWatch.Stop();
        }

如果您使用Coypu,您可以在AJAX调用后检查元素是否存在,然后单击它:

private void WaitUntilExistsThenClick(string selectorId)
{
    var searchBoxExists = new State(() => browser.FindId(selectorId).Exists());
    if (browser.FindState(searchBoxExists) == searchBoxExists)
    {                
        browser.FindId(selectorId).Click();
    }
}