In this article we learn the what is implicit wait in selenium, explicit wait in selenium and fluent wait in selenium.
Synchronization (Wait in Selenium WebDriver)
It is wait or hold till the object or its properties get fulfilled.
Why Synchronization?
1. To match the speed of tool with the application speed (Because tool speed is high).
2. To match the speed of our tool with the application which is under test.
When to or more components involved to perform any action. we except these components to work together with same pace. the coordination between these components to run parallel is called synchronization.
Synchronization can be classified into two categories (Types of Synchronization)
1. Unconditional Synchronization
2. Conditional Synchronization
1. Unconditional Synchronization
In this unconditional synchronization we just specify timeout value only.
We will make the tool to wait until certain amount of time and then proceed further.
In this there is another sub-type is Thread.sleep().
What is Thread.sleep?
In this we have to make the application to wait for certain amount of time by specifying the timeout value.
Syntax: Thread.sleep()
2. Conditional Synchronization
In conditional synchronization we specify a condition along with timeout value, so that tool waits to check for the condition for a particular condition to specify.
There are four types of conditional synchronization.
i) Implicit Wait in selenium
ii) Explicit Wait in selenium
iii) Fluent Wait in selenium
iv) Page Load Timeout Wait in selenium
i) What is Implicit Wait in Selenium WebDriver?
The implicit wait in selenium is used to tell the we driver to wait for a certain amount of time before it throws a “No such element exception”.
The default setting is “0”.
Once we set the time, the we driver will wait for the element for that time before throwing an exception.
Compile time polymorphism is used in implicit wait.
We will pass argument as timeout(hour, day, month, year, sec) and Timeunit.seconds through implicitlywait constructor so it will behave as compile time polymorphism.
Implicit wait is globally declared.
It is used when we know exactly how much time will have to load web page.
Syntax:
driver.manage().timeouts().implicitlywait(Timeout, Timeunit.secomds);
where,
Timeout- Waiting time
Timeunit.Seconds- Unit for time period
Example:
driver.manage().timeouts().implicitlywait(10, Timeunit.secomds);
ii) What is Explicit Wait in Selenium WebDriver?
The explicit wait in selenium is used to tell the web driver to wait for certain conditions (Expected conditions) or maximum time exceeded before throwing “”element not visible exception”.
It can be applied for only specified elements (It is applicable to specific element).
Explicit wait is nothing but Explicit wait = Waiting time + condition.
Explicit wait gives us better option than implicit wait as will wait for dynamically loaded AJax (AJax- Asynchronus Javascript and XML) element.
Syntax:
WebDriverWait wait = new WebDriverWait(driver, Timeout);
WebElement expwait;
expwait = wait.until (Expected Conditions.VisibilityofElementLocated(By.xpath(” “).click()));
where,
Until- Repeatedly applies the instances input value to the given predict write the timeout expires or the predict evaluate to be true.
Example:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement wait1;
wait1 = wait.until(ExpectedConditions.visibilityofElementLocated(By.xpath(” “).click()));
Expected conditions that can be used in explicit wait:
1. alertIsPresent();
2. elementIsDisplayed();
3. isSelected();
4. enebled();
5. elementToBeClickable();
6. TitleIs();
7. Visibilityof();
iii) What is Fluent Wait in Selenium WebDriver?
The fluent wait in selenium is used to defined maximum time for the web driver to wait for a condition as well as the frequency with which we want to check the condition before throwing an “Element not visible exception”.
It checks for the web element at regular interval until the object is found or timeout happens.
Fluent wait is Fluent wait = condition + Waiting time + frequency
Syntax:
Wait W = new FluentWait(WebDriver refrence)
W.withtimeout(timeout, seconds)
W.pollingEvery(timeout, seconds)
W.ignoring(Exception.class)
where,
withtimeout- Set how long to wait for the evaluate condition to be true.
pollingEvery- Set hoe often the condition should be evaluated.
Ignoring- Configure the instance to ignore specific type of exception while waiting for a condition.
Example:
Wait <WebDriver> W = new FluentWait <WebDriver> (driver);
W.withtimeout(10, TimeUnit.Seconds)
W.pollingEvery(5, TimeUnit.Seconds)
W.ignoring(NoSuchElementExeption.class)
.until(ExpectedConditions.visibilityofElementLocated(By.xpath(” “).click()));
In this instead of passing TimeUnit.Seconds we can able to pass “TimeUnit.NONOSECONDS”.
Where,
Frequency- Setting up a repeat cycle with the time frame to verify the condition at regular interval of time.
iv) What is Page Load Timeout Wait in Selenium WebDriver?
Set the amount of time to wait for a page load to complete before throwing an error.
Syntax:
driver.manage().timeouts().pageLoadTimeOut(TimeOut, TimeUnit.Seconds);
Example:
driver.manage().timeouts().pageLoadTimeOut(10, TimeUnit.Seconds);.