The waits in selenium are essential when it comes to executing Selenium tests. They help to observe and troubleshoot issues that may occur due to variation in time lag.
While running automation tests, we see ‘ElementNotVisibleException’ This appears when a particular web element with which WebDriver has to interact, is delayed in its loading. To prevent this Exception Selenium Waits must be used.
In automation testing, wait commands direct test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.
Type of Waits In Selenium
1. Implicit Waits in Selenium
Implicit wait specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
Webelement takes some time to be present or desired element is not present immediately. In this case we can use Implicit wait method.
If we set implicit wait as 10 seconds and element is found in 5 seconds, driver will not wait for remaining 5 seconds.
Once We set implicit wait, it will be applicable to all calls of findElement() and findElements() methods. There is no need to write implicit wait code again and again before locating an element.
If we write implicit wait statement multiple times, time specified in latest implicit wait statement will be applicable.
For example: We specify implicit wait as 30 seconds at line no 5 and another implicit wait as 60 seconds at line no 10, then implicit wait as 30 seconds will be applicable for all calls to findElement () and findElements () methods from line no 6-9 and implicit wait as 60 seconds will be applicable from line no 11 onward.
Syntax-
driver. manage (). timeouts (). implicitlyWait (Duration.ofSeconds(30));
2. Explicit Waits in Selenium
Selenium WebDriver provides a special kind of dynamic and customization wait called Explicit wait.
Explicit Waits also known as Dynamic Waits because it is highly specific conditioned. It is implemented by WebDriverWait class.
Explicit wait can be used to wait till our desired condition or conditions are met or it run out of maximum defined wait time.
Write for particular WebElement for particular condition.
Syntax-
WebDriverWait wt=new WebDriverWait (driver, Duration.ofSeconds(5));
wt. until (ExpectedConditions.elementToBeClickable(driver. findElement (By.id (“_”)))). Click ();
3. Fluent Waits in Selenium
FluentWait is class in selenium which implements Wait interface.
WebDriverWait is subclass of FluentWait.
It is an implementation of the Wait interface that may have its timeout and polling interval configured on the fly.
Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
Where we should use FluentWait?
When we do not find suitable expected wait condition in explicit wait.
To handle dynamic web elements.
We need to do more then just waiting.
When we need to create your own customized wait conditions.
4. Thread.sleep() Waits in selenium
1) Thread is a class in JAVA. sleep () is not provided by Selenium WebDriver, so it is not a selenium wait.
2) sleep () is a static method of Thread class so we can use it using class name i.e., Thread.
3) Thread.sleep causes the current thread to suspend execution for a specified period.
4) sleep () methods accept duration in milliseconds. (1 s= 1000 ms). 5)It throws IllegalArgumentException – if the value of millis is negative.
sleep () throws a checked exception which we must either throw or handle it using try catch.
6) Syntax: Thread.sleep(3000);
7) It is a static wait. Execution will be on hold till specified time.
8) Disadvantage-We make WebDriver to wait for 5 seconds. What if search result appears in just a second? WebDriver will still wait for another 4 seconds. It will increase test execution time.
About NoSuchElementException
1) NoSuchElementException is a sub class of Runtime Exception.
2) It is an unchecked exception.
3) It is thrown by findElement (By by) method above. Note only findElement () method not findElements () method as it gives us an empty array rather than exception.
4) It is thrown when WebDriver is not able to locate an element for some reasons. It might be wrong locator or element is not loaded yet.