Different kind of waits in selenium

Today we will discuss about Waits in selenium. You will learn about different waits, why it is important and how to implement in the script.

First of all, What is a wait? In general term, wait will halt the execution for a certain amount of time until a required condition is met. So why do we need waits in selenium? Because now a days, most of the web applications are build upon AJAX and JavaScript. So, when a webpage is loaded by browser, the different elements of the page may load at different time intervals.T his makes locating elements difficult: if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleException exception. Using waits, we can solve this issue. Wait provides a time for browser to met certain condition, mostly, regarding the elements to load properly.

You may ask that we already have sleep() method in java so what is the need of separate waits in selenium. Sleep() method is not a dynamic wait. It is a static wait that means, if we call sleep method  in our execution, then it will halt the execution for that amount of time irrespective of whether the condition is met or not. This can severely impact the execution time of scripts. But in dynamic waits,the execution will resume as soon as the condition is met.

For e.g. suppose we have given 20 seconds time limit in dynamic wait to wait for certain element to appear in DOM, and that element appears in 5 seconds. So, driver will not wait till 20 seconds to resume execution, it will resume execution as soon as element is found. But in static wait, execution will be halted for 20 seconds even if element appears in 5 seconds.

Selenium provides 3 types of dynamic waits:

  • Implicit Wait: Implicit wait tells the browser to keep looking for element in DOM for a certain amount of time, which is not immediately present. In other words, Implicit command will ask the selenium to wait for some time before throwing No Such Element Exception. The default wait time is 0. Make note that implicit waits will be in place for the entire time the browser is open. This means that implicit wait applies for all the elements on a web application.

wait1.jpg

I included Implicit wait in First Script Here, we are calling pre -defined implicitlyWait() method which accepts 2 arguments. First is the amount of time we want to wait,  and the second is the time unit like seconds, minutes milliseconds etc. I have given 20 seconds as wait time so if element is not loaded in DOM within this time frame then web driver will throw exception.

  • Explicit Wait: Explicit wait is used where we need to wait for a certain condition before proceeding forward with execution. If the defined time is exceeded, it will throw ElementNotVisibleException. There are some predefined ExpectedConditions that are frequently used when automating web browsers such as:
    • AlertIsPresent
    • ElementIsVisible
    • ElementToBeClickable(By locator)
    • elementToBeClickable(WebElement element)
    • ElementToBeSelected(By locator)
    • FrameToBeAvailableAndSwitchToIt(String)
    • frameToBeAvailableAndSwitchToIt(By locator)
    • PresenceOfAllElementsLocatedBy
    • TextToBePresentInElement
    • VisibilityOfAllElementsLocatedBy(By locator)
    • visibilityOfElementLocated (By locator)

You can read in detail about them here

wait2

As we can see in above image, we created an object of class WebDriverWait. We set a maximum time limit of 20 seconds. Then we are defining a condition which webdriver will match to see if the condition is true before throwing an exception. Explicit waits are useful in a situations where a particular condition takes a lot of time say more than 1 minute, (one minute is a big time period in automation) then we can apply explicit wait for that condition because if we set implicit wait of 1 minute, it will be applied for all elements and the execution will be slow.

wait3

We need to import the classes shown above.

  • Fluent Wait: The fluent wait is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an ElementNotVisibleException exception. It means that it will check the element periodically after certain amount of time. Suppose, a particular element appears sometimes within a second or sometimes after 30 seconds. So, In this case it is better to use fluent wait as it will check periodically for these type of elements.

wait4.jpg

We need to import the classes shown below.

wait5

In line 1, we are creating an object of FluentWait. In the second line, we are calling withTimeout() method which takes two arguments First is the amount of time we want to wait,  and the second is the time unit like seconds, minutes milliseconds etc. Then we need to set the polling time, i.e, the time interval after which webdriver will check if the element is loaded in DOM. We can ignore any exception with this wait:

wait6

We need to add .ignoring() in our syntax if we want to exclude any exception. We can use this wait into our code like this:

wait7.jpg

We are creating an anonymous class  in which we are returning a WebElement for which we need to apply fluent wait. In this example, we are returning an element whose id is userName. So, driver is going to look for this element in DOM after every 2 seconds for 60 seconds, after that it will throw an exception if element is not found.

Anonymous class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class.

wait8

We need to import the classes shown above.

Note: Most of the scenarios can be covered by using explicit wait as it already has lots of common Expected Conditions. So unless you want to write your own conditions for apply method, you can use either of the explicit or fluent wait.

Hope you have learned something new today. If you have any queries regarding waits,  please feel free to comment below.

Have a great Day!!!

Leave a comment