如何等待从WatiN完成jQuery Ajax请求?

我正在编写WatiN测试来测试Ajax Web应用程序,并遇到了Ajax请求的计时问题。

在页面上的操作触发Ajax请求之后,我希望WatiN等到请求完成后再validation页面是否正确更新。

我有一种感觉,解决方案将涉及评估JavaScript以注册$.ajaxStart$.ajaxComplete处理程序,以跟踪请求是否正在进行中。 我很快就会深入研究,但想知道其他人是否已经解决了这个问题。 似乎这是Ajax测试的常见问题。

我已经创建了一些WatiN Browser扩展方法来解决这个问题,但我仍然对其他解决方案感兴趣。

InjectAjaxMonitor方法创建一个javascript全局变量,该变量附加到ajaxStart和ajaxComplete事件以跟踪正在进行的请求的数量。

无论何时需要等待AJAX​​请求完成再继续,您都可以调用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 ); } } 

此解决方案不能很好地工作,因为.ajaxStart仅针对第一个Ajax请求进行调用,而每次ajax请求完成时.ajaxComplete调用.ajaxComplete 。 如果你在控制台中运行这个简单的代码:

 $.ajax({url:"/"}); $.ajax({url:"/"}) 

并在.ajaxStart.ajaxComplete处理程序方法中添加一些日志记录,您可以看到.ajaxStart处理程序只会被调用一次而.ajaxComplete处理程序将被调用两次。 因此ajaxRequestCount将变为负数,并且所有设计都被搞砸了。

如果你想保留你的设计,我建议你使用.ajaxSend而不是.ajaxStart

另一种解决方案是使用.ajaxStop而不是.ajaxComplete ,但通过这样做,您不需要ajaxRequestCount ,您只需要一个布尔值来说明是否在场景后面运行了ajax请求。

可以找到非常有用的信息: http : //api.jquery.com/category/ajax/global-ajax-event-handlers/

希望这可以帮助。

我在使用WatiN进行一些测试时遇到了这个问题。 我发现在WatiN的1.1.0.4000版本 (2007年5月2日发布(最新版本是2009年12月20日的2.0 RC2 )),声称在测试中添加了更好的处理Ajax的支持:

为了更好地支持对支持AJAX的网站的测试,此版本为您的工具箱添加了更多选项。

添加了一个新方法,该方法将等待某个属性具有特定值。 在需要等到元素值更新的情况下,这可能很方便。

例:

 // Wait until some textfield is enabled textfield.WaitUntil("disable", false.ToSting, 10); // Wait until some textfield is visible and enabled textfield.WaitUntil(new Attribute("visibile", new BoolComparer(true)) && new Attribute("disabled", new BoolComparer(false))); 

有关更多信息,请参阅发行说明的链接 。

我还没有详细研究过,所以我不知道它在哪些情况下可能有用。 但是,如果有人遇到这个问题,可能值得一提。