Favourite Quote

Every artist was first an amateur. - Ralph Waldo Emerson The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be. - Horace Bushnell

Tuesday, October 23, 2012

WebDriver C# Implicit & Explicit Wait


Earlier versions of Selenium most of us ended up using Thread.Sleep()

To wait for objects to render the page fully so that we can access the object from the DOM else Selenium would fail with errors for No such Element

Here is an example of how to use implicit wait using WebDriver FindElement

Code snippet for a single element

Code snippet for multiple elements

Most of the webpages have jQueries running some take longer to load,If a page doesn't load in 60 seconds then it is never going to load

here is the code snippet

However,You will have to ask your developers to set a flag within Javascript that says that the jquery is loaded.

public bool IsPageLoaded(IWebDriver driver)

{

try

{

//First wait for the browser to register the new URL;

if (driver.Url.Equals(OldUrl, StringComparison.OrdinalIgnoreCase))

{

return false;

}

log.Debug("Waiting for Page to load. Current URL: " + driver.Url);

//Second wait for JQuery to become available.

if (IsCheapflightsPage)

{

IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;

return (bool)executor.ExecuteScript("return CF.isReady;");

}

return true;

}

catch (InvalidElementStateException)

{

log.Error("Caught InvalidElementStateException. Assume page not yet loaded and continute waiting.");

return false;

}

}
automationwithselenium.blogspot.com-Google pagerank and Worth

No comments:

Post a Comment