Thursday 3 October 2013

Explicit and Implicit Waits in Selenium Web Driver

When implementing time synchronization for waiting with Selenium Web Driver technology, we can use two types of waits:

Explicit Wait

In Explicit Wait, we write a code to define a wait statement for certain condition to be satisfied until the wait reaches its timeout period. If WebDriver can find the element before the defined timeout value, the code execution will continue to next line of code. Therefore, it is important to setup a reasonable timeout seconds according to the system response.
For example:

public void ExplicitWait(int timeouseconds)

{

IWebDriver driver = new FirefoxDriver();
driver.Url = “http://www.google.com”;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeouseconds));
IWebElement elementToWait = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Id(“theElementId”));
});

}

Implicit Wait

In Implicit Wait, we define a code to wait for a certain amount of time when trying to find an element or elements. If the Web Driver cannot find it immediately because of its availability, the WebDriver will wait. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.
For example:

public void ImplicitWait(int waitSeconds)

{

WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(waitSeconds));
driver.Url = “http://www.google.com&#8221;;
IWebElement elementToWait = driver.FindElement(By.Id(“theElementId”));

}

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...