Check for DOM

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:

//Function is called after making an async request to wait for DOM to be updated
    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)
            {
                // Request is not completed, wait a bit for next polling.
                Thread.Sleep(AjaxSleepTime);
                waited += AjaxSleepTime;
            }
        }
        // IMPORTANT: Wait for DOM to be actually updated???
        Thread.Sleep(300);
    }

    // Check if something is in postback now.
    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 , . - ?

....

+3
2

, - , , .

( ) , Sys.WebForms.PageRequestManager.getInstance(). get_isInAsyncPostBack(); ( true) DOM.

, ASP.NET Web Forms JS. , - , JavaScript, 100% .

, , :

  • , 100% .
  • ( AJAX ) .

, , , 100% .

, , , , , , .

0

"Ajax Requests Completed Flag" javascript #.
HTML:

<input type='hidden' id='hdnRequestCompletedFlag' value='0' />

JavaScript:

var NoOfCompletedRequests = 0;
var NoOfRequests = 10; // Your number of requests.
$.ajax({
    ...,
    success: function( data ) { // This function same for all the ajax requests.
        NoOfCompletedRequests++;
        if(NoOfCompletedRequests == NoOfRequests)
            $('#hdnRequestCompletedFlag').val('1');
    }
});

#

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) && (hdnRequestCompletedFlag.Value == '1');
}
+1

All Articles