Posted in Selenium

What is the difference between Implicit wait and Explicit wait in Selenium WebDriver?

Implicit wait()  is used to inform WebDriver that there could be cases When a page is loaded by the browser, the elements which we want to interact with may not load instantaneously. In those cases WebDriver has to continuously listen to DOM for a given amount of time until either the time expires or the element is found.

In implicit wait, we will actually define a wait time globally, so it is applied to all the elements in the script. Below Implicit Wait() says driver that hey wait for N number of seconds before you throw an error or exception.

 driver.manage().timeouts().implicitlyWait(N, TimeUnit.SECONDS); 

Explicit Wait(): Explicit Wait will make your code to wait for certain condition to occur before moving forward.

Explicit Wait() is 2 types

  1. WebDriverWait :It is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum timeout. 
 WebDriverWait wait = new WebDriverWait(driver, TimeOut);  
wait.until(ExpectedConditions.visiblityOfElementLocated(locator)); 

The following are the suggestions if you type ExpectedConditions.

  1. alertIsPresent()
  2. elementSelectionStateToBe()
  3. elementToBeClickable()
  4. elementToBeSelected()
  5. frameToBeAvaliableAndSwitchToIt()
  6. invisibilityOfTheElementLocated()
  7. invisibilityOfElementWithText()
  8. presenceOfAllElementsLocatedBy()
  9. presenceOfElementLocated()
  10. textToBePresentInElement()
  11. textToBePresentInElementLocated()
  12. textToBePresentInElementValue()
  13. titleIs()
  14. titleContains()
  15. visibilityOf()
  16. visibilityOfAllElements()
  17. visibilityOfAllElementsLocatedBy()
  18. visibilityOfElementLocated

2. Fluent Wait : fluent wait is used to tell the web driver to wait for a condition, as well as the frequency or poll with which your WebDriver monitors or listens to DOM.

Frequency or poll : Setting up a repeat cycle with the time frame to verify/check the condition at the regular interval of time

Wait wait = new FluentWait(driver)    
    .withTimeout(30, SECONDS)    
    .pollingEvery(5, SECONDS)   
    .ignoring(NoSuchElementException.class);

Ex: fluentwait 10 sec, polling 2 sec means every 2 sec it checks DOM for the element

Conclusion: Explicit Waits are smarter than Implicit Waits because they don’t wait for the max time out.  Instead it waits for the time till the condition becomes true.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s