Selenium WebDriver - Implicit, Explicit and Fluent Wait

In this post, you will learn about different waits in Selenium. Waits are required to tell Selenium to wait for a specific amount for elements on the web page before throwing "No Such Element Exception". There are three types waits in Selenium Webdriver, out of which first two are the most popular.
  1. Implicit Wait
  2. Explicit Wait
  3. Fluent Wait

Implicit Wait

Implicit wait is used to set a default wait time (say 30 secs) in your automation script to wait for an  element on the page.

Key Points

  • Implicit wait should be declared in your setup method (at beginning lines of your code).
  • Default wait time for Implicit wait is 0 secs.
  • Once implicit wait is set, it is applicable for the whole automation script.
  • In simple words, you can also say that implicit wait is the maximum time set between two steps/commands of the automation script.
  • Webdriver throws "No Such Element Exception" if element is not found in specified time.

Syntax:

// Implicit wait - Set wait of 10 seconds
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);



Explicit Wait

Explicit wait can be called as conditional wait. It tells the WebDriver to wait for certain conditions before maximum time exceeded.

Key Points

  • Explicit wait is intelligent wait, as it waits certain conditions.
  • It applies to only single element and not to whole script.
  • It provides a better approach to handle dynamic ajax elements.
  • Explicit wait is the best way to replace Thread.sleep() (as static waits are not recommended in automation).
  • WebDriver throws "ElementNotVisibleException" if explicit wait fails.

Syntax:

// Explicit Wait
 WebDriverWait wait = new WebDriverWait(driver, 10); 
 wait.until(ExpectedConditions.alertIsPresent());

All Expected Conditions that can be used in Explicit Wait:

    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()

    Fluent Wait

    Fluent wait is kind of conditional wait but with frequency. It means fluent wait is used to wait for a condition with a regular frequency/time interval to be checked before throwing "ElementNotVisibleException".

    Example

    There might be a scenario where an element X might load with in 30 secs. In this case we can set 5 secs of frequency, so it will check after every 5 secs for the element X. So, ideally it will check for 6 times before throwing "ElementNotVisibleException". If element X loads within 30 secs, program flow would move to the next step of automation script.

    Syntax:

    // Fluent Wait
     Wait wait = new FluentWait(WebDriver reference)
       .withTimeout(timeout, SECONDS)
       .pollingEvery(timeout, SECONDS)
       .ignoring(Exception.class);
    
     WebElement foo = wait.until(new Function() {
      public WebElement applyy(WebDriver driver) {
       return driver.findElement(By.id("foo"));
      }
     });





    Refer Selenium Webdriver Tutorials Series



    Author: Vaneesh Behl
    Subscribe FB Group Techlistic.com
    Join Telegram channel at https://t.me/techlistic
    Feel free to ask queries or share your thoughts in comments or email me.

    Comments

    Popular Posts

    Top 10 Demo Websites to Practice Selenium Webdriver Online

    Selenium Essentials - How to become expert in Automation Testing?

    Top 25 Must to know Selenium Webdriver Commands List

    Selenium WebDriver - Browser and Navigation Commands

    Selenium IDE - Sample Script With Most Commonly Used Commands