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

WebDriver C#,using log4net - logging


Most of us want our logs to be written to a file externally.

Whether they are application logs,console logs.

In our case have used to grab the logs for the WebDriver C# bindings using the log4net.dll's

Below are step by step instructions as to go about using log4net :

1. Add a config section in the app.config file under configuration

2. Add a appSettings section where you define a key value pair

Which would facilitate ConfigurationManager to get the value

3. I have created a config file other than that of the app.config & named it as log4net.config - below is the configuration

Have used RollingFileAppender and specified a location where these logs are to be written.

RollingFileAppender - This is just like LogFileAppender with an exception doesn't create a new file

Until it has reached the MaximumFilesize that we have configured initially


4. The below code snippet shows how we need to initialize the logger

private static readonly ILog log = LogManager.GetLogger(typeof());

5. Now accessing the key value pair from the app.config file

log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["log4netConfig"]));
automationwithselenium.blogspot.com-Google pagerank and Worth