We have covered below types of waits present in Selenium WebDriver in older posts:
In this post, we will learn Explicit Wait.
Implicit wait is wait which is set for a session of WebDriver and it will just make WebDriver to wait till it finds the web element. But there are scenarios in which we need to wait even after locating web element. Consider below scenarios:
- When user clicks on Login button, a text box appears after some time. In beginning text box is disable but gets enable automatically after some time. If we use implicit wait, it will resume execution as soon as element is found and will fail because element is disable and it could not perform desire operation on found element. In this scenario, WebDriver needs to wait till element is enable. How we will make webdriver to wait till element is enable and perform some action?
- Suppose an alert is open and it takes time to disappear once user accepts or dismisses it. You need to wait till alert is disappear. How WebDriver will know when alert is disappeared? Thread.sleep will not solve problem always as you never know how much time alert will take to disappear. Implicit wait will also not work.
- I want to give one real time example. Many applications use toaster messages to show information. There is time defined for a toaster message to disappear. It appears generally at right top corner. Suppose, at right top corner, there is logout button and when toaster message appears it hides logout button. You want to perform some operations so that a toaster message will appear and you want to click on logout. WebDriver will not able to click on logout immediately as toaster message will hide logout button. Here, you need to make WebDriver to wait till toaster messages disappear. It will give exception stating “Other element will receive the click.” because webdriver will be able to locate logout button but will not be able to click.
So, we can understand sleep or implicit wait will just help if web element is not located immediately. It will not allow flexibility to wait till some specific reason is satisfied. So, to handle these condition based wait, selenium webdriver provides “EXPLICIT WAIT”.
What is Explicit Wait?
An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.
There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
Java Code to use Explicit wait:
WebDriver driver= new ChromeDriver();
WebDriverWait wait= new WebDriverWait(driver, 30);
WebElement Element= wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Some Expression"))));
- WebDriverWait is a class. We can create an object of it by passing WebDriver instance and timeout in seconds. WebDriverWait will ignore instances of NotFoundException that are encountered (thrown) by default in the ‘until’ condition, and immediately propagate all others. You can add more to the ignore list by calling ignoring(exceptions to add).
- An exception will be called based on expected condition in case of time out and condition is not satisfied. We can pass timeout only in seconds.
- ExpectedConditions is a class which has canned
ExpectedConditions which are generally useful within webdriver tests. For example: elementToBeClickable etc.
- All canned expected conditions are static methods of ExpectedConditions class. To view all canned conditions, do”ExpectectedCondition.(dot)”.
- This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.
- Like implicit wait, explicit wait will also not wait further if condition is satisfied before timeout reaches.
- Explicit wait is intelligent wait as it waits dynamically for specified condition.
More about Explicit wait:
- Unlike implicit wait, explicit wait is not for entire webdriver session.
- Unlike implicit wait, you need to write explicit wait every time whenever you need it as it is specific to a web element.
- No need to create object of WebDriverWait again and again if you want similar timeout . If you need another timeout duration or driver instance, go for new instance of WebDriverWait.
- We can specify timeout only in seconds. Other units of time are not possible.
- You can reduce test execution time by using explicit wait.
- We can customized polling time or ignore other exceptions as well using WebDriverWait instance. For example:
WebDriverWait wait= new WebDriverWait(driver, 30);
It is more advisable to use explicit wait as it provides more flexibility in customized wait.
That’s it about explicit wait. Hope you must have good idea after reading this.
If you like my posts, please like, comment , share and subscribe.
Do not forget to check frequently asked java programs in interview.
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning