Sobes.tech
Junior — Middle

What are the different types of waits used in Selenium for synchronization?

sobes.tech AI

Answer from AI

In Selenium, various types of waits are used to synchronize tests, waiting for the desired state of elements on the page and avoiding errors due to asynchronous loading.

Main types of waits:

  1. Implicit Wait

    • Set once for the driver.
    • When searching for elements, the driver will wait the specified time for the element to appear.
    • Example:
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
  2. Explicit Wait

    • Waiting for a specific condition for a particular element.
    • Uses the WebDriverWait class and conditions from ExpectedConditions.
    • Example:
      WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
      WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
      
  3. Fluent Wait

    • An extension of explicit wait with the ability to configure polling frequency and ignored exceptions.
    • Allows for more flexible control over waiting.
    • Example:
      Wait<WebDriver> wait = new FluentWait<>(driver)
          .withTimeout(Duration.ofSeconds(30))
          .pollingEvery(Duration.ofSeconds(5))
          .ignoring(NoSuchElementException.class);
      
      WebElement element = wait.until(driver -> driver.findElement(By.id("myElement")));
      

Using waits helps make tests more stable and resilient to loading delays.

What are the different types of waits used in… - sobes.tech