Are You Mixing Waits In Selenium WebDriver? – A Bad Practise

Selenium WebDriver provides two types of waits mechanisms:-

  1. Implicit Wait
  2. Explicit Wait – WebDriverWait and FluentWait

Implicit wait applies for a session of WebDriver and comes in to effect when WebDriver is trying to locate a web element or a list of web elements. Generally, we have a common practice of setting up implicit wait when we initialize a browser and use Explicit Wait wherever needed. But this we are doing wrong. We are mixing up waits which may end up with unexpected wait time.

There is no overriding of waits in Selenium WebDriver i.e. implicit wait will not override explicit wait or vice versa. In fact if you have both waits applied then both waits will be applicable in common scenarios.

Official website of Selenium also says the same:-

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

Let’s see this behavior using some examples so that we can understand it better.

In the below example, I will set an implicit wait and will try to locate a web element with wrong ID value.

package MixingWaitsExamples;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ImplicitWaitExample {

        WebDriver driver;
        @Test
        public void mixinfWaitsForLocatingElement()
        {
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
                // Setting up implicit wait
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                driver.manage().window().maximize();
                driver.get("http://www.demoqa.com/");
                // Timer starts
                System.out.println("Wait starts at : "+new Date());
                // Locating wrong element
                driver.findElement(By.id("SomeWrongId"));
        }
        
        @AfterMethod
        public void printExitTime()
        {
                System.out.println("Wait ends at : "+new Date());
                driver.quit();
        }
}

It should throw NoSuchElementException after 30 seconds i.e. implicit wait timeout.

Without polling interval

You will see WebDriver will poll at an interval of 500 MS for 10 seconds and will throw timeout exceptions as there is no such element.

package MixingWaitsExamples; import java.time.Duration;
import java.util.Date;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class ExplicitWaitWithDefaultPollingIntervalExample { WebDriver driver; @Test public void mixinfWaitsForLocatingElement() { // Initializing browser WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); // Loading URL driver.get("http://www.demoqa.com/"); // Setting up explicit wait with default polling interval WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Timer starts System.out.println("Wait starts at : "+new Date()); // Locating wrong element wait.until(new Function() { @Override public WebElement apply(WebDriver driver) { System.out.println("Retrying at : "+new Date()); return driver.findElement(By.id("sdfds")); } }); } @AfterMethod public void printExitTime() { System.out.println("Wait ends at : "+new Date()); driver.quit(); }
} 

Output

With polling interval

This time I will set a polling interval as well. Explicit timeout as 10 seconds with polling interval of 2 seconds.

package MixingWaitsExamples; import java.time.Duration;
import java.util.Date;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class ExplicitWaitWithDivisibleIntervalExample { WebDriver driver; @Test public void mixinfWaitsForLocatingElement() { // Initializing browser WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); // Loading URL driver.get("http://www.demoqa.com/"); // Setting up explicit wait with custom polling interval WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.pollingEvery(Duration.ofSeconds(2)); // Timer starts System.out.println("Wait starts at : "+new Date()); // Locating wrong element wait.until(new Function() { @Override public WebElement apply(WebDriver driver) { System.out.println("Retrying at : "+new Date()); return driver.findElement(By.id("sdfds")); } }); } @AfterMethod public void printExitTime() { System.out.println("Wait ends at : "+new Date()); driver.quit(); }
}

Output

Now WebDriver will poll to check condition on an interval of 2 seconds and terminate after 10 seconds.

In the above example, I have set polling interval 2 seconds which divides explicit timeout 10 seconds completely ~ 5. Let’s set a polling interval that is not completely divisible by the explicit wait. For example:- Explicit wait = 20 seconds and polling interval = 6 seconds

package MixingWaitsExamples; import java.time.Duration;
import java.util.Date;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class ExplicitWaitWithNonDivisibleInterval { WebDriver driver; @Test public void mixinfWaitsForLocatingElement() { // Initializing browser WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); // Loading URL driver.get("http://www.demoqa.com/"); // Setting up explicit wait with custom polling interval WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20)); wait.pollingEvery(Duration.ofSeconds(6)); // Timer starts System.out.println("Wait starts at : "+new Date()); // Locating wrong element wait.until(new Function() { @Override public WebElement apply(WebDriver driver) { System.out.println("Retrying at : "+new Date()); return driver.findElement(By.id("sdfds")); } }); } @AfterMethod public void printExitTime() { System.out.println("Wait ends at : "+new Date()); driver.quit(); }
}

Did you observe something strange? Defined Explicit wait timeout was 20 seconds but actually it took 24 seconds. In console log, it appeared as 20 seconds only but you get the actual time taken by taking differentiation of starting and ending time printed above.

Explicit wait with polling interval and time taken to evaluate expected condition may make actual explicit timeout longer. You can go through with some scenarios here:-

Working Mechanism Of Polling Interval In Explicit Wait – Selenium WebDriver

Let’s mix implicit and explicit wait and observe the behavior.

package MixingWaitsExamples; import java.time.Duration;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class MixingOfWaitsForLocatingElement { WebDriver driver; @Test public void mixinfWaitsForLocatingElement() { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); // Setting up implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Load URL driver.get("http://www.demoqa.com/"); // Setting up explicit wait with polling interval as 12 seconds WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20)); wait.pollingEvery(Duration.ofSeconds(6)); // Timer starts System.out.println("Wait starts at : "+new Date()); wait.until(new Function() { @Override public WebElement apply(WebDriver driver) { System.out.println("Retrying at : "+new Date()); // Implicit wait will be applicable here return driver.findElement(By.id("sdfds")); } }); } @AfterMethod public void printExitTime() { System.out.println("Wait ends at : "+new Date()); driver.quit(); }
}

Actual explicit timeout is taken:- 26 Seconds (Greater than defined explicit wait 20 seconds). The first time condition was checked at 36th seconds and the second was at 47 seconds. The difference is of 11 seconds which is a sum of 5 seconds (time taken by the findElement method because of an implicit wait) and 6 seconds (defined polling interval). I have explained this logic with examples here.

You can see how a delay in evaluating an expected condition due to implicit wait and polling interval (main reason) increased explicit timeout. This is the reason it is said do not mix implicit wait and explicit wait. It will impact if a scenario where both waits are applicable as discussed above. Custom polling interval restricts WebDriver to check timeout which causes increased explicit timeout.

package MixingWaitsExamples; import java.time.Duration;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class MixinfWaitsForLocatingElement { WebDriver driver; @Test public void mixinfWaitsForLocatingElement() { // Setting up browser WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); // Setting up implicit wait greater than explicit wait driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Load URL driver.get("http://www.demoqa.com/"); // Setting up explicit wait with default polling interval WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Timer starts System.out.println("Wait starts at : " + new Date()); wait.until(new Function() { @Override public WebElement apply(WebDriver driver) { System.out.println("Retrying at : " + new Date()); // Implicit wait will be applicable here return driver.findElement(By.id("sdfds")); } }); } @AfterMethod public void printExitTime() { System.out.println("Wait ends at : " + new Date()); driver.quit(); }
} 

You can see this time implicit wait did not impact explicit timeout and timed out after 10 seconds with the message “Supplied function might have stalled”.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning