如何等待jQuery的Ajax请求从华廷完成? [英] How to wait for jQuery Ajax requests to complete from WatiN?

查看:101
本文介绍了如何等待jQuery的Ajax请求从华廷完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写华廷测试来测试的阿贾克斯 Web应用程序和所遇到的一个计时问题与Ajax请求。

I'm writing WatiN tests to test an Ajax web application and have come across a timing issue with Ajax requests.

在一个Ajax请求是通过页面上的操作触发,我想华廷等到请求完成验证,该网页被正确地更新了。

After an Ajax request is triggered by an action on the page, I'd like WatiN to wait until the request is complete before validating that the page was updated correctly.

我有一种感觉,该解决方案将涉及EVAL-荷兰国际集团的JavaScript 登记处理程序 $ .ajaxStart $。ajaxComplete 来跟踪请求是否正在进行中。我会来挖成不久,但想看看其他人已经解决了这一点。好像这将是一个共同的问题,Ajax的测试。

I have a feeling that the solution will involve eval-ing JavaScript to register handlers for $.ajaxStart and $.ajaxComplete to track whether requests are in progress. I'll dig into that shortly, but wanted to see if anybody else has already solved this. Seems like it would be a common problem with Ajax testing.

推荐答案

我已经创建了一些华廷浏览器扩展的方法来解决这个问题,但我仍然有兴趣在其他的解决方案。

I've created a few WatiN Browser extension methods to solve this problem, but am still interested in other solutions.

InjectAjaxMonitor 方法创建的重视,ajaxStart和ajaxComplete事件追踪进展情况的请求数一个JavaScript全局变量。

The InjectAjaxMonitor method creates a javascript global variable that attaches to the ajaxStart and ajaxComplete events to track the number of requests in progress.

每当你需要等待AJAX​​请求到移动之前完成,则可以调用 browserInstance.WaitForAjaxRequest();

Whenever you need to wait for AJAX requests to complete before moving on, you can then call browserInstance.WaitForAjaxRequest();.

public static class BrowserExtensions
{
    public static void WaitForAjaxRequest( this Browser browser )
    {
        int timeWaitedInMilliseconds = 0;
        var maxWaitTimeInMilliseconds = Settings.WaitForCompleteTimeOut*1000;

        while ( browser.IsAjaxRequestInProgress()
                && timeWaitedInMilliseconds < maxWaitTimeInMilliseconds )
        {
            Thread.Sleep( Settings.SleepTime );
            timeWaitedInMilliseconds += Settings.SleepTime;
        }
    }

    public static bool IsAjaxRequestInProgress( this Browser browser )
    {
        var evalResult = browser.Eval( "watinAjaxMonitor.isRequestInProgress()" );
        return evalResult == "true";
    }

    public static void InjectAjaxMonitor( this Browser browser )
    {
        const string monitorScript =
            @"function AjaxMonitor(){"
            + "var ajaxRequestCount = 0;"

            + "$(document).ajaxSend(function(){"
            + "    ajaxRequestCount++;"
            + "});"

            + "$(document).ajaxComplete(function(){"
            + "    ajaxRequestCount--;"
            + "});"

            + "this.isRequestInProgress = function(){"
            + "    return (ajaxRequestCount > 0);"
            + "};"
            + "}"

            + "var watinAjaxMonitor = new AjaxMonitor();";

        browser.Eval( monitorScript );
    }
}

这篇关于如何等待jQuery的Ajax请求从华廷完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆