liveBook · Manning

Implicit wait

As explained in Testing Spring Boot Applications

An implicit wait is a WebDriver timeout policy that repeatedly searches for a page element until it appears or the timeout expires.

An implicit wait is a WebDriver timeout policy that repeatedly searches for a page element until it appears or the timeout expires. Configure it once, and Selenium applies it whenever a test locates an element. This suits asynchronous single-page applications, where JavaScript updates the page after HTTP requests. section 4.5.1

Configure a one-second implicit wait

After creating the ChromeDriver, configure driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1)). A later findElement(By.id("cityResults")) call waits for the matching element instead of requiring a separate explicit wait statement. section 4.5.1 section 4.5.2

Use the policy with these practical boundaries:

  • Apply it when page interactions trigger asynchronous updates and elements may not exist immediately. section 4.5.1
  • Let Selenium repeat findElement or findElements queries until elements appear or the timeout expires. section 4.5.2
  • Keep browser lifecycle management separate: quit the driver and stop its service after tests finish. section 4.5.1
Listing 4.21 Selenium test setup
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class SeleniumTests {

  @LocalServerPort int port;
  static ChromeDriverService driverService;
  static WebDriver driver;
  private String baseUrl;

  @BeforeAll
  static void startChromeBrowser() throws Exception {
    driverService = ChromeDriverService.createDefaultService(); #1
    driverService.start(); #1

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");
    driver = new ChromeDriver(driverService, options); #2
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1)); #3
  }

  @AfterAll
  static void stopChromeBrowser() {
    driver.quit(); #4
    driverService.stop(); #4
  }

}

Inspect the complete Selenium setup, including headless Chrome, driver startup, the implicit-wait configuration, and cleanup. section 4.5.1

Listing 4.22 Test the auto-completion feature with Selenium
@Test
void autocomplete() {
  driver.get("http://localhost:" + port + "/?mode=modern"); #1

  var citySearchInput = driver.findElement(By.id("citySearch")); #2
  citySearchInput.sendKeys("jak"); #3

  var autocompleteResults = #4
      driver.findElement(By.id("cityResults"))
        .findElements(By.cssSelector(".autocomplete-item"))
        .stream()
        .map(WebElement::getText)
        .map(String::trim);
  assertThat(autocompleteResults) #5
    .containsExactly( #5
        "Djakotomé (Benin)",
        "Jakarta (Indonesia)",
        "Kamirenjaku (Japan)"
    );
}

See the policy in use while testing autocomplete: type into a field, locate delayed results, collect their text, and assert the order. section 4.5.2

How Testing Spring Boot Applications covers it

How deep it goes
Defines repeated element lookup, shows one-time configuration, and stops short of broader Selenium use cases.
Examples
Demonstrates a one-second ChromeDriver wait and an autocomplete test that finds and verifies three dynamically displayed cities.
What it assumes
Assumes familiarity with Selenium WebDriver element queries and asynchronous page behavior.
What you can do after
Apply it via Selenium by configuring a driver timeout that handles delayed element appearance during browser tests.
How it connects
Connects WebDriver timeouts with Selenium element lookup, asynchronous JavaScript updates, and browser-based testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.