What are Waits in selenium ? Implicit, Explicit and Fluent waits in Selenium

Sadakar Pochampalli
JasperSoft BI Suite Tutorials - Sadakar Pochampalli )

What are Waits in selenium ?

1) Implicit wait 
2) Explicit wait

1) Implicit wait
a) implicitlyWait()
b) pageLoadTimeout()
c) setScriptTimeout()

2) Explicit wait 
a) WebDriverWait
b) Fluent

Implicit wait | implicitlyWait()

  • implicitlyWait is applied to all the web elements on the web page.
  • It will throw "No Such Element Exception" after reaching the time. 
  • Implicit wait stays in place for the entire duration for which the browser is open.
  • The default value of implicit wait is 0.
  • Implicit wait is applied for the lifetime of the Webdriver, it can extend the test execution times to a large value depending on the number of elements on which it is being called.
  •  When to use implicitlyWait? 
  •   It is recommended to use implicit wait only when you are in complete control of the script.
  • Example: 
    import this package : import java.time.Duration;
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60))

Implicit wait | pageLoadTimeout()

  • It waits for the page to load completely for the specified number of seconds. 
  • Default value is : 0 
  • Example:

    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.get("https://demoqa.com/login");
  • WebDriver will wait for a maximum of 30 seconds before moving ahead with the interaction with the element.

Implicit wait | setScriptTimeout()

  • The setScriptTimeout command waits for the asynchronous parts of the web page to finish loading for a specified number of seconds.
  • Example:
    driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);
    ((JavascriptExecutor) driver).executeScript("alert('hello world');");
    ((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");
  • WebDriver will wait for a maximum of 15 seconds before moving ahead with the interaction with the element.

Explicit wait | WebDriverWait

  • It is a conditional wait strategy 
  • Wait until the the condition specified becomes true or the time duration is elapsed. 
  • Explicit wait is only applied on the specified element. 
  • Commonly used conditions are : 
    presenceOfElementLocated()
       elementToBeClickable()
       visibilityOfElementLocated()
  • WebDriverWait class and ExpectedConditions specifies the time and condition for which the WebDriver needs to wait.

  • Example-1 : 
      WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='searchBox']")));
  • Example-2: 
    WebElement searchBox = new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='searchBox']")));
  • Example-3: 
    WebElement searchBox = new WebDriverWait(driver, Duration.ofSeconds(30))
    .until(driver -> driver.findElement(By.name("//input[@id='searchBox']")));

Explicit wait| Fluent wait

  • Fluent wait is similar to Explicity wait 
  • It takes an additional argument frequency i.e., polling time. 
  • frequency number tells the WebDriver to keep on checking for the element at regular time intervals wait till the maximum of "Duration.ofSeconds"
  • If the element becomes avaialble earlier, we proceed with the  test execution and finish quickly. 
  • Example: 
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class);

    • Maximum waiting time is 30 seconds 
    • polling time is 5 seconds 
    • WebDriver keeps on checking for the element for every 5 seconds to a maximum of 30 seconds ignoring will ignore the exception NoSuchElementException while the driver finds the element in the maximum time.

  • When to use ? 
    When dealing with AJAX elements 


Which is best wait ? When to use which wait ? 

Explicit wait : 
  • Saves the time and is not applicable for all elements.
  • Wait only based on condition and time for a specified element. 

Implicit wait : 
  • It is applied once for all the elements and the next wait cycle starts once the previous one completes. 
  • Implicitly waits needs a better understanding of scripts.
There is no best type of wait, based on the demand of the element find strategy we use any of the waits above.  


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


What is polling time ? 
The polling time (or polling interval) is the time interval in which Selenium starts searching again after the last failed try. 
  • It depends on the type of browser driver you are working on. 
  • Some may have 500 milliseconds while some may have 1 second as polling time. 
  • The polling time is inbuild in implicitlyWait and there is no way to modify the time interval. 
  • The default pooling period for implicit and explicit wait is 250 ms.
  •  During implicitlyWait, the WebDriver will poll the DOM for certain specified time units while trying to find any element. 
  •  If the element is found earlier, the test executes at that point otherwise the WebDriver waits for the specified duration.

Thread.sleep()
  • Thread.sleep() pauses the execution
  • Sleep is a static method that belongs to the ‘Thread’ class of Java.
  • Thread.sleep() sets the condition to an exact time period to wait.
What is the difference between sleep and selenium waits ? 
  • Thread.sleep() will stop the execution of the script for the time specified in the script, irrespective of the fact that the element on the web page has been found.
  • Selenium waits do not wait for the complete duration of time. If the WebDriver is able to find the element before the specified time duration, it moves on to the next line of code. 
  • This helps in reducing the overall time of script execution by a considerable margin.
Selenium setSpeed()
setSpeed() sets the desired speed of execution or delays execution by a specified amount of time before each operation.

Feedback