Implicit Wait in Selenium WebDriver

Implicit wait in Selenium WebDriver introduction:

Implicit wait in WebDriver has solved many issues that occurs due to intensive use of Ajax in any webpage. Ajax intends loading time of each element on webpage to vary.Due to different loading time, it become cumbersome to find all element on web page as soon as web page opens and this causes your script to fail.
Implicit wait specifies the amount of time the driver should wait when searching for an element if it is not immediately present. So to search any element on webpage driver will poll the page till the time it does not found element or time out expires and then it throws exception like “NoSuchElementException”.




Watch video to know more about facts of Implicit Wait

Few important points about Implicit wait:
1-
Implicit wait is called global wait because once it is declared it stay with WebDriver instance for its life time.
2- It comes in to action, as soon as it sees any driver.findElement/driver.findElements statement in code. it means it will wait for all the statements made to find WebElement on webpage.
3- Default wait is “0” that can be custom as per our need depending upon network latency.
4- Implicit wait should be avoided with slow locator like xpath due to its adverse impact on script run time.
Before moving ahead why not we see code snippet taken from WebDriver api which have all the code of TimeOut interface

  
  interface Timeouts {

    
    / *
     * @param time The amount of time to wait.
     * @param unit The unit of measure for {@code time}.
     * @return A self reference.
     */
    Timeouts implicitlyWait(long time, TimeUnit unit);

   
     *
     * @param time The timeout value.
     * @param unit The unit of time.
     * @return A self reference.
     * @see JavascriptExecutor#executeAsyncScript(String, Object...)
     */
    Timeouts setScriptTimeout(long time, TimeUnit unit);

    /**
     * 
     * @param time The timeout value.
     * @param unit The unit of time.
     * @return A Timeouts interface.
     */
    Timeouts pageLoadTimeout(long time, TimeUnit unit);
  }

Source :  WebDriver Code

As we can see in above code that implicitlyWait take two parameters, First parameter takes the time for which driver instance have to wait and second instance is an enum TimeUnit which have constants like Seconds, Minutes, Hours, Days,milliSeconds, micoSeconds and nanoSeconds.
So if someone has taken 5 as time to wait and TimeUnit.SECONDS then it means driver instance will wait for next 5 seconds before throwing exceptions if element is not found on webpage.
code snippet used for implicitlyWait

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

above line of code should be used just after driver instance is initialise.
So lets take one example of implicit wait.




Scenario:
1- Open bing website
2- Enter Selenium Tutorials string in search text field
3- Once string will be entered in textfield, some ajax drop down will appear and in this first choice should be selected.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;


public class IMplicitWaitInWebDriver {
@Test
public void test()
{
	WebDriver driver = new FirefoxDriver();
	
	
	driver.get("http://bing.com");
	driver.findElement(By.name("q")).sendKeys("Selenium Tutorials");
	// Implicit wait :  is global wait for webelement
	driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	
	driver.findElement(By.xpath("//*[@id='sa_2']/div/div")).click();
}

}

Description of above code:
above code will open one firefox instance and will open bing.com and after opening bing.com it will enter Selenium Tutorials string in search box but as soon as we will enter string ajax dropdown will not appear on screen and for this we need to wait for 2-3 seconds so for safer side, have used 5 second in code and after 5 second elapsed code will perform the action of selecting the first dropdown option.

Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.