Add nicer hook for waiting for AJAX
Brought to you by:
jvmenen
I use WatiN for integration tests on a site that makes use of AJAX using jQuery. I need to be able to easily wait for all AJAX responses to return after, for example, clicking a button.
My current way to do that is to extend IE and override WaitForComplete and eval some javascript so I know whether there are any pending requests:
class JQueryAwareIE : IE { public override void WaitForComplete(int waitForCompleteTimeOut) { base.WaitForComplete(waitForCompleteTimeOut); while(AreAjaxRequestsPending()) { Thread.Sleep(50); } base.WaitForComplete(waitForCompleteTimeOut); } private bool AreAjaxRequestsPending() { try { return Eval("(typeof($) !== 'undefined')?($.active):\"0\"") != "0"; } catch (UnauthorizedAccessException) { //Javascript is disabled so we know there are no outstanding AJAX requests return false; } } }
Yuck!
I notice that there is an IEWaitForComplete class that contains the following code:
if (ieDocument != null) { WaitWhileMainDocumentNotAvailable(ieDocument.HtmlDocument); WaitWhileDocumentStateNotComplete(ieDocument.HtmlDocument); WaitForFramesToComplete(ieDocument.HtmlDocument); }
If this code were refactored so that classes containing a 'thing to wait for' used a common interface (and I could inject such a class into an IE instance) then I could make a jQuery 'thing to wait for' and life would be better.