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;
    }
}

5 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
  4. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
    360DigiTMG artificial intelligence course in hyderabad

    ReplyDelete
  5. Selenium is an open-source software testing framework for automating web application testing. It validates web applications and checks compatibility issues across different platforms to determine whether they function correctly. Developers and testers use multiple programming languages, including Java, Python, Ruby, etc., to write Selenium test scripts that help them automate interactions
    with a web browser.

    ReplyDelete