Wait time in selenium web driver with example

Different types of wait time in selenium web driver

At the time of Automation, we need to wait for load URL or wait to load an element, So in this tutorial we are going to discuss Different types of wait time in selenium web driver. mainly there are two types of wait explicit and implicit we will learn one by one. when we need to use explicit wait and when we can user implicit wait.

Why wait is important in order to automation.

Some time web elements take time to load on in the browser because most of the web application or we can say every web application used AJAX, Javascript, and JQuiry. Which takes time to load the complete page. In this situation we have only one solution is wait time in selenium web driver. If we will not use Wait it will show an error like “ElementNotVisibleException” exception. To handle this kind of error we need to user Wait.

To understand the scenario of wait we need to take a real-time example like…..

We need to perform an action with a web element like input field or button. And the element takes 50 sec time to load so the driver will not able to locate that element and It will throw an exception “No such element found”. We can solve this issue in the upcoming step using Wait.

Implicit wait in selenium web driver:

Web driver will wait for the element for a certain amount of time before throwing an exception. In the below example we will wait to load the browser for 60 sec.

How to use implicit wait in selenium web driver.

The implicit wait will tell to the web driver to wait for a certain amount of time before it throws an Exception. The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.

The syntax for the implicit wait for the selenium web driver:

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

A complete example of wait time in selenium web driver (Implicit wait).

package testCases;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

publicclass ImplicitWait {
  WebDriver driver;

  @Test
  publicvoid implicitWait() throws IOException {

    System.setProperty("webdriver.chrome.driver", "D:\\SeleniumAutomation\\ChromeDriver\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("http://awebautomation.com/");

  }

  @AfterClass
  publicvoid closeBrowser() {
    driver.close();
  }
}

 Explicit Wait in selenium web driver

 The explicit wait is a kind of dynamic wait suppose an element is loaded dynamically some time it takes 5 sec or sometimes it takes 10 sec. So the explicit wait will take care of this. The explicit wait is very intelligent and better option to wait for an element.

The question is how it works, So, for example, I put 50 sec explicit wait to locate an element but the element is locate in 20sec then the driver will not wait for next 30 sec. So kind of condition waits for it explicit wait tell to the driver for the wait to load a specific element if the element is found before the time frame driver will skip the reminder time.

The syntax for an explicit wait:

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);

 In the above line of code, we are creating an object of WebDriverWait and it will take two parameters in the constructor first one is the element which we want to locate and the second one is the maximum time frame.

A complete example of the Implicit wait.

package testCases;
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.Test;

publicclass ExplicitWait {
  WebDriver driver;
@Test
publicvoid explicitWait() {
  
    System.setProperty("webdriver.chrome.driver", "D:\\\\SeleniumAutomation\\\\ChromeDriver\\\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.get("http://awebautomation.com/");
    WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"recent-posts-2\"]/ul/li[1]/a"))).click();	

  }

@AfterClass
publicvoid close() {
  driver.close();
  }
}

Fluent wait in selenium web driver

The fluent wait is the advance version of wait in selenium web driver. When we try to locate an element in an interval Like we need to check the presence of an element in every 10 seconds. The fluent wait is a separate class in selenium web driver and by using this class we can wait for the specific condition until it is not satisfied. There will be two parameters in fluent wait 1st one will take the maximum waiting time and the 2nd one will take the pooling time(F).

In fluent wait you can change the default pooling time and the default pooling time in the implicit wait and the explicit wait is 250ms. We can ignore the exception in fluent wait at the time of pooling.

Influent wait, we will define the maximum time for every instance and set the frequency to check the condition.

The syntax for a fluent wait:

Wait<WebDriver>wait = newFluentWait<WebElement>(driver)
        .withTimeout(30, TimeUnit.SECONDS)
        .pollingEvery(5, TimeUnit.SECONDS)
        .ignoring(NoSuchElementException.class);

 WebElementfoo = wait.until(new Function<WebDriver, WebElement>() {
 publicWebElement apply(WebDriver driver) {
 returndriver.findElement(By.id("foo"));
      }
    });

 

Perform the challenging task to learn selenium web driver using java.

Add a Comment

Your email address will not be published. Required fields are marked *