How to wait for jQuery Ajax requests to complete from WatiN?

后端 未结 3 445
臣服心动
臣服心动 2021-02-02 03:27

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

After an Ajax request is triggered by an action on the

3条回答
  •  灰色年华
    2021-02-02 03:54

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

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

    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 );
        }
    }
    

提交回复
热议问题