Intermediate 20 min 5 scenarios

Dynamic Waits Automation Practice

Practice explicit waits, implicit waits, and Playwright auto-waiting — handling delayed alerts, lazy elements, spinners, and state changes in real automation scenarios.

Scenario 1: Wait for a delayed alert (2 seconds)

Click the button — a browser alert fires after a 2-second delay. Use explicit wait for alertIsPresent().


Scenario 2: Wait for hidden element to appear (3 seconds)

Element is hidden until the delay completes. Wait for visibility before asserting its text.


Scenario 3: Wait for disabled button to become enabled (3 seconds)

Click "Activate Button" to start the countdown. Wait for the target button to become clickable before interacting.


Scenario 4: Wait for loading text to change (3 seconds)

Status text transitions from idleLoading... Data Loaded!. Assert the final text after the delay.


Scenario 5: Wait for spinner to disappear (3 seconds)

A spinner appears on click and vanishes after 3 seconds. Wait for it to become hidden before asserting the done state.

What You'll Learn

Selenium (Java)

  • WebDriverWait.until()
  • ExpectedConditions.visibilityOf()
  • ExpectedConditions.elementToBeClickable()
  • FluentWait
  • implicitlyWait()
Tutorial video coming soon

Test Cases

Introduction

Waits are critical for stable automation — without them tests fail on slow networks or dynamic pages. There are three types of waits in Selenium:

  1. Implicit Wait — global timeout applied to every findElement call
  2. Explicit Wait — wait for a specific condition on a specific element
  3. Fluent Wait — explicit wait with polling interval and ignored exceptions

Playwright auto-waits for elements to be ready before acting — explicit waits are rarely needed, but custom timeouts can be set.

Key Methods Summary

Wait Type Selenium (Java) Playwright (JS) Playwright (Python)
Implicit implicitlyWait(Duration) Not applicable Not applicable
Wait for visible visibilityOfElementLocated(By) toBeVisible() to_be_visible()
Wait for clickable elementToBeClickable(By) toBeEnabled() to_be_enabled()
Wait for text textToBePresentInElement(el, text) toHaveText("text") to_have_text("text")
Wait for URL urlContains("path") toHaveURL(...) to_have_url(...)
Custom timeout new WebDriverWait(driver, Duration) { timeout: 10000 } timeout=10000

1. Implicit Wait

Sets a default timeout for all findElement calls across the session.

Selenium (Java)

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// All findElement calls will now wait up to 10s before throwing NoSuchElementException

Playwright (JS)

// Playwright auto-waits — no implicit wait needed.
// To set a global default timeout:
page.setDefaultTimeout(10000); // 10 seconds

Playwright (Python)

# Playwright auto-waits — set global timeout if needed:
page.set_default_timeout(10000)  # 10 seconds

2. Explicit Wait — wait for element to be visible

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement"))
);
System.out.println(element.getText());

Playwright (JS)

// Playwright waits automatically — just assert/interact
await expect(page.locator("#dynamicElement")).toBeVisible({ timeout: 10000 });
const text = await page.locator("#dynamicElement").textContent();

Playwright (Python)

expect(page.locator("#dynamicElement")).to_be_visible(timeout=10000)
text = page.locator("#dynamicElement").text_content()

3. Explicit Wait — wait for element to be clickable

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(
    ExpectedConditions.elementToBeClickable(By.id("submitBtn"))
);
button.click();

Playwright (JS)

await page.locator("#submitBtn").waitFor({ state: "visible" });
await page.locator("#submitBtn").click(); // auto-waits for clickable

Playwright (Python)

page.locator("#submitBtn").wait_for(state="visible")
page.locator("#submitBtn").click()  # auto-waits for clickable

4. Wait for text to appear on page

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.textToBePresentInElementLocated(
    By.id("statusMsg"), "Completed"
));

Playwright (JS)

await expect(page.locator("#statusMsg")).toHaveText("Completed", { timeout: 10000 });

Playwright (Python)

expect(page.locator("#statusMsg")).to_have_text("Completed", timeout=10000)

5. Wait for element to disappear (spinner/loader)

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingSpinner")));

Playwright (JS)

await page.locator("#loadingSpinner").waitFor({ state: "hidden", timeout: 15000 });

Playwright (Python)

page.locator("#loadingSpinner").wait_for(state="hidden", timeout=15000)

6. Wait for URL to change

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.urlContains("/dashboard"));

Playwright (JS)

await expect(page).toHaveURL(/dashboard/, { timeout: 10000 });

Playwright (Python)

expect(page).to_have_url(re.compile("dashboard"), timeout=10000)

7. Fluent Wait (poll until condition)

Selenium (Java)

Wait<WebDriver> fluentWait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(15))
    .pollingEvery(Duration.ofSeconds(2))
    .ignoring(NoSuchElementException.class);
 
WebElement element = fluentWait.until(
    d -> d.findElement(By.id("asyncData"))
);

Playwright (JS)

// Use waitForFunction for custom polling
await page.waitForFunction(() => {
  const el = document.getElementById("asyncData");
  return el && el.textContent.trim().length > 0;
}, { timeout: 15000, polling: 2000 });

Playwright (Python)

page.wait_for_function(
    "() => document.getElementById('asyncData')?.textContent?.trim().length > 0",
    timeout=15000,
    polling_interval=2000
)

8. Wait for page to fully load

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(driver -> ((JavascriptExecutor) driver)
    .executeScript("return document.readyState").equals("complete"));

Playwright (JS)

await page.waitForLoadState("networkidle"); // or "load" or "domcontentloaded"

Playwright (Python)

page.wait_for_load_state("networkidle")  # or "load" or "domcontentloaded"

📄 Also Read: Top 10 Best Automation Practice Website

Frequently Asked Questions