This is a rather interesting problem. I will provide some context because many people may ask why someone will need something like this.
Background Information
You have to do unit tests (integration) for large and complex .net web forms applications. Currently using visual studio 2010, nunit, watin 2.1, ie9. There are already a few complex fairly complex unit tests, but they are slow, not very reliable, mainly due to synchronization problems. So you need lead time from many hours to more reasonable and keep them reliable.
Current problem
I'm having trouble waiting for asynchronous requests to complete. (This needs to be done to refresh the page from the screen for testing.) The current situation looks something like this:
public static void WaitForAsyncPostBackToComplete(this Browser browser)
{
int waited = 0;
bool isInPostback = true;
while (isInPostback)
{
if (waited > AjaxWaitTime)
{
throw new Exception(string.Format(
"Looks like Ajax reqest timed out. Test waited {0} milliseconds.",
waited));
}
isInPostback = IsInPostback(browser);
if (isInPostback)
{
Thread.Sleep(AjaxSleepTime);
waited += AjaxSleepTime;
}
}
Thread.Sleep(300);
}
private static bool IsInPostback(Browser browser)
{
string function =
"( typeof(Sys) === undefined " +
"|| Sys == undefined || Sys == 'undefined' || Sys==null " +
"|| typeof(Sys.WebForms) === undefined " +
"|| Sys.WebForms==null || Sys.WebForms==undefined|| Sys.WebForms=='undefined'" +
"|| typeof(Sys.WebForms.PageRequestManager) === undefined) " +
"|| Sys.WebForms.PageRequestManager==null || Sys.WebForms.PageRequestManager==undefined|| Sys.WebForms.PageRequestManager=='undefined'" +
"? true " +
": Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();";
string result = browser.EvalSafely(function);
return Convert.ToBoolean(result);
}
, , ajax IsInPostback(). .
, IsInPostback() , , DOM. , , , . - Thread.Sleep(300), DOM. , , , DOM .
, DOM IE9 ? , DOM ( DOM ) , . 50 , . - ?
....