Site icon Learn Automation

WebDriverWait in Selenium

WebDriverwait in Selenium

In this post we will learn about WebDriverWait in Selenium.

In Selenium wait commands mainly used for synchronization purpose because some times WebDriver does not track the active web element or real state of DOM.  For example driver navigate to a page but throw no such element exception because of synchronization issue.

WebDriverWait is the type of explicit wait.

There are two types of explicit wait.

  1. WebDriverWait
  2. Fluent Wait

Click Here to see Fluent wait.


WebDriverWait in Selenium

It is applied on certain web element with defined waiting time and conditions. It can also throw exception when element is not found.

Below following expected conditions that can be used in explicit wait.

  1. alertIsPresent()
  2. elementSelectionStateToBe()
  3. elementToBeClickable()
  4. elementToBeSelected()
  5. frameToBeAvaliableAndSwitchToIt()
  6. invisibilityOfTheElementLocated()
  7. invisibilityOfElementWithText()
  8. presenceOfAllElementsLocatedBy()
  9. presenceOfElementLocated()
  10. textToBePresentInElement()
  11. textToBePresentInElementLocated()
  12. textToBePresentInElementValue()
  13. titleIs()
  14. titleContains()
  15. visibilityOf()
  16. visibilityOfAllElements()
  17. visibilityOfAllElementsLocatedBy()
  18. visibilityOfElementLocated()

Syntax:

WebDriverWait  wait = new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.elementToBeClickable(By.id(“Some id”)));

In the above code ‘WebDriverWait’ is a class. The waiting time of element is 10 sec defined in the WebDriverWait class on the webpage until the ExpectedConditions are met and the condition is elementToBeClickable.


Test script with an explanation

package waits;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExplicitWaits {
    public static void main(String[] args) {
        //To create a new instance of Firefox Driver
        WebDriver driver = new ChromeDriver();
        //To open a website "Automation Testings"
        driver.get("https://automationtestings.com/");
        //To maximize the browser window
        driver.manage().window().maximize();
        //This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds
        WebDriverWait wait = new WebDriverWait (driver, 10);
        //Title of the webpage is "Learn Automation | Automation Skills"
        wait.until(ExpectedConditions.titleIs("Learn Automation | Automation Skills "));
        //If the above condition met then the browser will be closed
        //To close the browser
        driver.close();
        //Change the title " Learn Automation | Automation Skills " as "xyz" in the script and try
        //You will face an execption - Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for title to be "Software Testing Material - A site for Software Tes". Current title: "xyz"
    }
}

The default polling time for implicit and explicit wait is 250 ms.


Selenium WebDriver wait for Page To Load

Sometimes due to slow internet connectivity issue a web page takes a while to load. So in this scenario use will wait for page to load before clicking on the link or button etc.

In automation testing, we have to give some wait commands to wait for page to load.

Some scenario where we should use wait commands-

  1. In Ajax-based application we should use wait commands because in this type of application element may continue to loads after the page loads.
  2. In dynamic dropdown, after selecting values in one dropdown, second dropdown values get populated.

In Selenium, there are different wait commands available for page to load.

  1. Implicit wait
  2. Explicit wait
  3. Fluent wait

Fluent wait in selenium

Read this link for Fluent wait


FAQ – Frequently asked Questions

Q1. Which wait is better in selenium?

To answer of this question will be very specific because the answer may change depending on the web element on which you are working.

But I can suggest explicit wait in this situation where we are using multiple browser and loading time is vary as per browser.

Below is the syntax of explicit wait.

WebDriverWait  wait = new WebDriverWait(driver, 10);
Wait.until(ExpectedConditions.elementToBeClickable(By.id(“Some id”)));

Q2. Is WebDriver wait class or Interface?

WebDriverWait is a class.

Q3. What is Fluent wait in selenium?

The Fluent wait in selenium is to defined maximum time of wait for a condition and also frequency with which we want to check the condition before throwing an ‘ElementNotVisibleException’ exception.

Q4. What is default wait time in Selenium?

In implicit wait the default wait time is 0.


Watch this video:

 

If you are not regular reader of my blog then I highly recommend you to sign up for the free email newsletter using the below link.

Exit mobile version