Buy From Flipkart

Friday, 7 February 2014

Selenium WebDriver : Implicit Wait Vs Explicit Wait Vs Custom-expected Wait

Using Selnium 2 (i.e WebDriver) we have number of ways for implementing WaitForAnElement, In this post will try to explain them with an example.

WaitForElement with an implicit wait :

The Selenium WebDriver provides an implicit wait for synchronizing tests. When an implicit
wait is implemented, if WebDriver cannot find an element in the Document Object
Model (DOM), it will wait for a defined amount of time for the element to appear in the DOM.

Once set, the implicit wait is set for the life of the WebDriver object's instance. However, an
implicit wait may slow down your tests when an application responds normally, as it will wait
for each element appearing in the DOM and increase the overall execution time.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Until the end of a test or an implicit wait is set back to 0, every time an element is searched
using the findElement() method, the test will wait for 10 seconds for an element to appear.

WaitForElement with an explicit wait:

The Selenium WebDriver also provides an explicit wait for synchronizing , which provides
a better control when compared with an implicit wait. Unlike an implicit wait, you can write
custom code or conditions for wait before proceeding further in the code.

An explicit wait can only be implemented in cases where synchronization is needed and the
rest of the script is working fine.

The Selenium WebDriver provides WebDriverWait and ExpectedCondition classes for
implementing an explicit wait.

The ExpectedCondition class provides a set of predefined conditions to wait before
proceeding further in the code. The following are some common conditions
that we frequently come across when automating web browsers supported by the
ExpectedCondition class:

An element is visible and enabled          elementToBeClickable(By locator)              
An element is selected                          elementToBeSelected(WebElement element)
Presence of an element                         presenceOfElementLocated(By locator)        
Title                                                    titleContains(java.lang.String title)                  

WebDriverWait wait = new WebDriverWait(driver, 10);
 Next, ExpectedCondition is passed to the wait.until() method as follows:

The WebDriverWait object will call the ExpectedCondition class object every 500
milliseconds until it returns successfully.

WaitForElement with custom-expected conditions :

The Selenium WebDriver also provides a way to build custom-expected conditions along with
common conditions using the ExpectedCondition class.

The Selenium WebDriver provides the ability to implement the custom ExpectedCondition
class along with the WebDriverWait class for creating a custom-wait condition, as needed
by a test. In this example, created a custom condition, which returns a WebElement
object once the inner findElement() method locates the element within a specified
timeout as follows:

WebElement message = (new WebDriverWait(driver, 5))
.until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver d) {
return d.findElement("qaautomationworld"));

Other popular posts: