Friday, June 26, 2009

Avoiding WaitForPageLoad

public bool WaitForBrowserStability(int maxWait)
{
    bool retValue = false;
    DateTime dtStart = DateTime.Now;
    do
    {
        System.Threading.Thread.Sleep(sleepTime);
        if (IsBrowserLoaded())
        {
            retValue = true;
            break;
        }
    } while (((TimeSpan)DateTime.Now.Subtract(dtStart)).TotalMilliseconds < maxWait);
    return retValue;
}
public bool IsBrowserLoaded()
{
    try
    {
        return ("true" == _selObj.GetEval("((\"complete\" == selenium.browserbot.getCurrentWindow().document.readyState) && (null == selenium.browserbot.getCurrentWindow().event))"));
    }
    catch (SeleniumException selExc)
    {
        Log.Warn(DateTime.Now.ToString() + " : Selenium error encountered. " + selExc.Message);
        _selObj.SelectWindow("");
        return false;
    }
    catch (Exception exc)
    {
        throw exc;
    }
}

3 comments:

  1. Dude .. nice .. but can u tell me what is the difference between that and WaitForPageLoad ? I do realize WaitForPageLoad is sometimes a bit dodgy, but it still works ... why do you need to avoid it ??

    Thanks ...

    ReplyDelete
  2. Sometimes when page is already loded, or for some javascript exception in AUT the page do not loads even when you perform some operation expecting it to load, in such scenario WaitForPageLoad throws exception after given time.

    ReplyDelete
  3. hi,

    Any idea about Webdriver...

    Can u give me some intro and guiding to work with webdriver selenium 2.0...

    thx
    SCSVEL

    ReplyDelete