How to use implicit wait in selenium webdriver and why

There are 2 types of waits available in Webdriver/Selenium 2 software testing tool. One of them is Implicit wait and another one is explicit wait. Both (Implicit wait and explicit wait) are useful for waiting in WebDriver. Using waits, we are telling WebDriver to wait for a certain amount of time before going to next step. We will see about explicit wait in my upcoming posts. In this post let me tell you why and how to use implicit wait in webdriver.

Why Need Implicit Wait In WebDriver
As you knows sometimes, some elements takes some time to appear on software web application page when browser is loading the page. In this case, sometime your webdriver test will fail if you have not applied Implicit wait in your test case. If implicit wait is applied in your test case then webdriver will wait for specified amount of time if targeted element not appears on page. As you know, we can Set default timeout or use "setTimeout" command in selenium IDE which is same as implicit wait in webdriver.

If you write implicit wait statement in you webdriver software testing script then it will be applied automatically to all elements of your test case. I am suggesting you to use Implicit wait in your all test script of software web application with 10 to 15 seconds. In webdriver, Implicit wait statement is as bellow.

How To Write Implicit Wait In WebDriver
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

Above statement will tell webdriver to wait for 15 seconds if targeted element not found/not appears on page. Le we look at simple example to understand implicit wait better.

Copy bellow given @Test method part and replace it with the @Test method part of example given on this page(Note : @Test method is marked with pink color in that example).

@Test
 public void test () 
  {  
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
   driver.findElement(By.xpath("//input[@name='namexyz']"));
  }

In Above webdriver test case with JUnit example,  1st Element 'xpath("//input[@name='fname']")' will be found on page but element xpath("//input[@name='namexyz']") is not there on page. So in this case webdriver will wait for 15 to locate that element on page because we have written implicit wait statement in our code. At last webdriver test will fail because xpath("//input[@name='namexyz']") is not on the page.

17 comments:

  1. I have a doubt. If i put implicit wait for 30 seconds and then if webdriver searches at the first time and if element not present then will it wait for total 30 seconds and then search for the second time or it will look for it immediately at 10 or 15 second if the element appears??

    if someone answers this it would be great help. Thanks!

    ReplyDelete
    Replies
    1. in implicit wait, if webdriver cannot find webelement in starting, it will wait for specified time duration. Webdriver will not search during this wait interval. Once specified time is over, it will try to search again for last time before throwing any exception.
      In Fluent wait and explicit wait, if wait time is 30 seconds, webdriver tries to search for element after some specified time say 500 miliseconds

      Delete
    2. Implicit wait tries to find the element in first go, if element is not present implicit wait tries to find the element after 500ms of first polling, if element is not available on second time also then implicit wait tries thired time after 500 ms of second try and it goes on till the time reaches the 30 seconds

      Delete
  2. Do we have to mention implicit wait once in a script or where ever we required.as Thread. Sleep.pls answer
    Thanks in advance

    ReplyDelete
  3. I need help with my code. I try to use implicit waits to replace my working code using Thread.sleep but it doesnt work.. Can you help me out?
    http://stackoverflow.com/questions/41348805/my-code-thread-sleep-works-but-not-implicit-wait

    ReplyDelete
  4. Could you please help me? I am trying to replace my Thread.Sleep code using Implicit but I can never get it to work.
    http://stackoverflow.com/questions/41348805/my-code-thread-sleep-works-but-not-implicit-wait

    ReplyDelete
  5. Awesome...It's works perfectly fine with me. Thread sleep not works for me at all also i tried to driver wait but no luck. I just placed driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) and magic.

    ReplyDelete
  6. Awesome...It's works perfectly fine with me. Thread sleep not works for me at all also i tried to driver wait but no luck. I just placed driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) and magic.

    ReplyDelete
  7. where do you put it? in @Before() class, or...?

    ReplyDelete
  8. Implicit & Explicit Wait time not working. Only thread.sleep(10); works
    It takes to long to put thread.sleep(10); after every line
    Any suggestions.
    driver.manage().timeouts().implicitlyWait(driver.wait(10,TimeUnit.SECONDS));
    WebDriverWait wait = new WebDriverWait(driver, 10);

    ReplyDelete