What is wait in selenium?
Have you ever come across an “ Elements not visible exception” while running tests in Selenium? If your answer is yes this occurs when web driver is not able to interact with a particular web element because of a delayed response. This is known as the Synchronization problem.
Synchronization problem: Automation script will execute faster than application response, so the specific element is not tested.
Here comes Selenium wait commands in order to prevent this exception. Wait commands help to load the web page completely or wait until a particular element is available. Below are the types of wait commands in Selenium:
Thread.sleep()
Implicit wait
Explicit wait
Fluent wait
Thread.sleep(): When we use thread.sleep method in our testing, it will stop the execution of the script for amount of time we specified even though the element is found before specified time. Time unit in thread.sleep is in milliseconds. Sleep function will throw interruptedException, this can be handled using try-catch.
Syntax: Thread.sleep(500)
In the above example I used thread.sleep before giving username, that means it will stop executing script for 1000 milliseconds once user clicks on sign-in link. Disadvantage of thread.sleep() is if a different page of same application takes time to load, we have to use multiple thread.sleep() in our script. This is where we can use implicit wait.
Implicit Wait: Implicit wait will tell the web driver to wait for a certain amount of time before throwing an exception. If an element is not available immediately, it will wait for a specified time and will throw an exception. The default setting for wait time is 0(zero). We can set the time and the webDriver will wait for that particular time. Implicit wait time is fixed and it is applied to all the web elements or actions.
Syntax: driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Example:
In the above example I have mentioned webDriver to wait for 30 seconds before it is going to interact with another element. Here I mentioned time in seconds, but we can also use other units of time as shown below.
From the below image we can see that implicitlywait is strikedoff, this is because implicitly wait is deprecated in selenium 4.
The above method is available till selenium3, but in selenium4 there is another method which we can use for implicit wait. Signature for this method is changed, instead of using implicitly wait with long and timeunit here we use “duration of class”. Syntax for that is as below.
Syntax: driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
Explicit waits: Explicit wait commands tell the webDriver to wait until a certain condition is met before proceeding further. Explicit wait will stop the execution of script based on a condition for the specified time. Once the specified time is completed then it will throw an exception. Explicit wait uses expected conditions to perform the tests when the tester is not sure of the amount of time an element takes to load.
Expected Conditions: These are the conditions provided by selenium WebDriver which are used for performing explicit waits. When we use expected conditions in our tests WebDriver will wait until a certain condition is met before proceeding for further test execution. Below are the expected conditions that can be used in tests.
alertIsPresent()
elementSelectionStateToBe()
elementToBeClickable()
elementToBeSelected()
frameToBeAvaliableAndSwitchToIt()
invisibilityOfTheElementLocated()
invisibilityOfElementWithText()
presenceOfAllElementsLocatedBy()
presenceOfElementLocated()
textToBePresentInElement()
textToBePresentInElementLocated()
textToBePresentInElementValue()
titleIs()
titleContains()
visibilityOf()
visibilityOfAllElements()
visibilityOfAllElementsLocatedBy()
VisibilityOfElementLocated()
There are two types of explicit waits. They are:
WebDriverWait
FluentWait
WebDriverWait: WebDriverWait is applied to specific elements with defined expected condition and time. This wait throws an exception when an element is not available or a certain condition is not met.
Syntax: //driver will wait for 10 seconds until the web element is available to click
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
//driver will click the button once it is available
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='disable']"))).click();
Example:
The above code will instruct the webDriver to wait for 10 seconds to click the button before throwing an exception. If the button is available before 10 seconds it will click the button immediately and will move forward with the next set of code, this way it is saving time and will execute scripts faster.
Fluent Wait : Fluent wait tells the webDriver to wait a maximum amount of time until a certain condition(web element) is met before throwing an exception. Web driver will check for the web element at regular intervals until it finds it or till timeout happens.
In the above example, a wait time of 30 seconds is given to find a specific element to be visible before clicking it. There is a polling time of 10 seconds between each check. Once the element is available it will click the element and close the browser.
Polling frequency: In fluent wait polling frequency is used to check if an element is available after the specified polling frequency time.
Ignore Exception: Incase if WebDriver cannot find an element during polling we can ignore any exception such as 'NoSuchElement' exception.
Implicit vs Explicit:
Implicit wait | Explicit wait |
Implicit wait is applied to all the elements in web page | Explicit wait is applied to a specific web element |
Expected conditions are not specified for an element | Expected conditions are specified for an element |
Can be used when we know an element may be visible in a certain time. | Can be used when we are not sure when the element is visible. |
Conclusion: Selenium Wait commands are needed to ensure the scripts run without any exceptions. These commands help testers when dealing with dynamic web pages where different elements load at different times, then there is a possibility of test failures as the web driver will not be able to interact with the web element to perform certain actions. These errors can be removed using Selenium wait commands by pausing test scripts for a specific period of time or until a condition is met.