Saturday 7 July 2012

Wait for WebElement in WebDriver

Hey Guys, 

It has been long time, I posted my last post to the blog. Been through a hectic schedule, now frees up to write something related to wait for element in selenium/Web Driver. So lets start....

The biggest challenge in automating a web application is, the loading of a web page is always in the mercy of certain conditions, They are -:

 1) Load on the server
 2) Network speed 
 3) Performance of AUT
 4) Ajax call to load an element

So the major task in automating such application is to wait for the HTML element to appear in the page before your automation test code start performing action on them. So you need to make sure that the Web Element is present before the code start working on it. 

This can be achieved by waiting for the element to appear in the page. But how to do that?  


1) Putting the current thread to sleep mode for definite time period.

What does it mean? When your Test automation is executing the code, put the current thread to sleep for definite time period like 
Thread.sleep(timeToSleep) 


The drawback of using such a way is, the wait time specified is a definite time that means if you specify wait time is 5 Secs then your element appears after 1 sec then you wasted 4 secs and the thread will only wake up after 5 secs. It does not care about whether your element appears by that time or before the time.

2) Have a mechanism to wait in synchronized mode.

Lets see what do we mean by synchronized wait. Synchronized wait means, wait till the condition satisfied once the condition satisfied, come out from the sleep and continue the test execution.

Ok, So far we know why do we need to wait for the element, how to achieve that using sleep methods both synchronized and non synchronized. Now let us go and see how this can be achieved in Selenium/Web Driver.


In Selenium 2.0, this can be achieved via both Explicit and implicit wait. We will see how to implement both in the code below.


Explicit Waits

 Explicit wait is to define to wait for a certain condition to be satisfied before proceeding the test execution. The worst way to have this is -: Thread.sleep().
 WebDriverWait class in combination with ExpectedCondition class provides a elegant way to implement Explicit Wait.

Code-:

WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.co.in");
WebElement TestElement= (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
 @Override
 public WebElement apply(WebDriver d) {
  return d.findElement(By.id("IdOfElement"));
 }});

In the above code, WebDriver will ping the DOM 10 secs or till it finds, to fetch the element.

Implicit Waits

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Code-: 

WebDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.co.in");
WebElement TestElement= driver.findElement(By.id("IdOfElement")
 
Thanks!! 
 

1 comment:

  1. Thank you for writing this post 'Wait for WebElement in WebDriver'

    I had difficulty in clicking on the logout button on gmail and this worked by using the Explicit Wait

    ReplyDelete

Related Posts

Related Posts...