Skip to main content

Why WaitEngine?

Timing is the number one cause of flaky Selenium tests. Most teams try to solve it with scattered WebDriverWait boilerplate or succumb to Thread.sleep.

Plain Selenium provides the primitives — WebDriverWait and ExpectedConditions — but leaves the implementation and lifecycle entirely up to you. Over time, every test suite develops the same set of issues.


The two synchronization traps​

Without a centralized wait strategy, test suites invariably fall into two traps:

1. Scattered WebDriverWait​

Every page object and test method instantiates its own wait:

// Page A
new WebDriverWait(driver, Duration.ofSeconds(5)).until(...);

// Page B
new WebDriverWait(driver, Duration.ofSeconds(10)).until(...);

// Utility class
new WebDriverWait(driver, Duration.ofSeconds(30)).until(...);

Timeouts become arbitrary and inconsistent across tests. If a slower staging environment requires increasing explicit timeouts from 5 seconds to 10 seconds, developers must touch dozens of files or build bespoke wrapper classes.

2. Flaky Thread.sleep​

When explicit waits feel tedious or when modern JavaScript apps re-render components mid-interaction, engineers insert blind sleeps:

Thread.sleep(2000); // "Just to be safe"

A 2-second sleep runs for 2 seconds even when the element was ready in 50 milliseconds. Multiplied across hundreds of tests, build times balloon. Worse, under heavy CI server load, that 2-second sleep is often not enough, and the test flakes anyway.


Centralized auto-waiting with WaitEngine​

WaitEngine centralizes explicit waits behind a clean, static API powered by a single configuration key: timeouts.explicit in selenium-boot.yml (default: 5 seconds).

selenium-boot.yml
timeouts:
explicit: 5

When you need an explicit synchronization point, call WaitEngine directly without instantiating wait objects or passing WebDriver around:

import com.seleniumboot.wait.WaitEngine;

// Clean, static synchronization driven by selenium-boot.yml
WaitEngine.waitForClickable(By.id("checkout-btn")).click();

And when using Selenium Boot's locators ($(), getByRole(), getByLabel()), explicit waits disappear entirely: locator actions auto-wait up to timeouts.explicit by default.


Before & after​

Synchronizing dynamic elements​

Plain Selenium (verbose instantiation + potential stale reference):

// Manual WebDriverWait setup with hardcoded timeout
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement saveBtn = wait.until(
ExpectedConditions.elementToBeClickable(By.id("save-settings"))
);
saveBtn.click(); // Can throw StaleElementReferenceException if the DOM re-rendered

// Blind sleep often added to await asynchronous background saves
try {
Thread.sleep(1500);
} catch (InterruptedException ignored) {}

Selenium Boot (WaitEngine + auto-waiting locators):

// WaitEngine re-fetches elements on poll and respects centralized config:
WaitEngine.waitForClickable(By.id("save-settings")).click();

// Or with Locator — zero explicit wait code needed:
$("#save-settings").click();

Out of the box condition coverage​

WaitEngine provides pre-built conditions for real-world web scenarios without requiring custom ExpectedCondition implementations:

MethodWhat it waits for
WaitEngine.waitForVisible(By)Element is present in DOM and displayed
WaitEngine.waitForInvisible(By)Loading spinners, modals, or overlays to disappear
WaitEngine.waitForClickable(By)Element is visible and enabled for interaction
WaitEngine.waitForText(By, text)Element displays expected visible text
WaitEngine.waitForTextMatches(By, regex)Element text matches a regular expression pattern
WaitEngine.waitForUrlMatches(regex)Current browser URL matches a regex
WaitEngine.waitForStaleness(WebElement)Previous element detached / replaced after AJAX reload
WaitEngine.waitForPageLoad()document.readyState === "complete"
WaitEngine.waitForNetworkIdle()No network requests in flight (opt-in; CDP on Chrome/Edge, BiDi on Firefox)

…without hiding Selenium​

WaitEngine is a convenience, not a wall:

  1. Native Selenium types: Methods accept standard Selenium By locators and return standard WebElement or Alert objects.
  2. Full escape hatch: Need a condition that isn't built in? Pass any native Selenium ExpectedCondition<T> into WaitEngine.wait(...):
    WaitEngine.wait(ExpectedConditions.numberOfWindowsToBe(2));
  3. No hidden magic: WaitEngine uses standard WebDriverWait and ExpectedConditions under the hood. It simply standardizes the timeout, wire protocol, and error reporting across your entire suite.

Wait strategy comparison​

Synchronization MethodTimeout SourceRe-checks Stale ElementsNetwork-AwareTest Code Verbosity
Thread.sleep()HardcodedNoNoLow, but adds dead wait time
Scattered WebDriverWaitHardcoded per callManual handling neededNo (requires custom CDP wiring)High boilerplate
WaitEngineselenium-boot.ymlYesOpt-in (waitForNetworkIdle on CDP/BiDi)Low, static one-liners
Auto-waiting Locator ($(), getBy*)selenium-boot.ymlYesDOM-readiness (not network-driven)Zero explicit wait code

(Note: Auto-waiting locators synchronize on DOM readiness — element presence, visibility, and clickability. When awaiting background AJAX/fetch requests without immediate UI mutations, opt into WaitEngine.waitForNetworkIdle().)


Next steps​