31. 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

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.

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.


package Others;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class webdriver_implicit{
                       
            @BeforeClass
            public void beforeClass() {
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
                       
            }
           
            public WebDriver driver;
            @Test
            public void testIsDiplayed_Enabled() throws Exception {
                       
                        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                        driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
                        driver.findElement(By.xpath("//input[@name='namexyz']"));
           
            }

            @AfterClass
            public void afterClass() throws Exception {
                        driver.quit();
            }

}

In Above webdriver test case with 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 Seconds 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. 

3 comments:

  1. Does it automatically gets applied to all the elements in our test case??
    I think implicit wait is only applied for the specific element.

    ReplyDelete
    Replies
    1. For Running above code u will get the following output:

      FAILED: testIsDiplayed_Enabled
      org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name='namexyz']"}
      (Session info: chrome=46.0.2490.71)
      (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86) (WARNING: The server did not provide any stacktrace information)
      Command duration or timeout: 15.06 seconds

      Delete
  2. This comment has been removed by the author.

    ReplyDelete