Beginner 10 min 6 scenarios

Alerts & Dialogs Automation Practice

Master alert and dialog handling in Selenium & Playwright — simple alerts, confirm dialogs, prompt inputs, toast notifications, modal alerts, and advanced dialogs.

Scenario 1: Simple Alert

Click the button, then use driver.switchTo().alert() to accept it and verify the alert text.


Scenario 2: Confirm Alert

Accept or dismiss the confirm dialog, then assert the returned boolean value.


Scenario 3: Prompt Alert

Use Alert.sendKeys() to type in the prompt, then accept and assert the captured value.


Scenario 4: Toast Alert

Trigger the toast, then assert the [data-sonner-toast] element is visible in the DOM.


Scenario 5: Sweet Alert (Modal)

Open the modal dialog, then assert the title text and click Accept or Cancel.


Scenario 6: Advanced UI Dialog

Open the dialog, assert the share link input value, copy it, then close with the Close button.

What You'll Learn

Selenium (Java)

  • driver.switchTo().alert()
  • Alert.accept()
  • Alert.dismiss()
  • Alert.getText()
  • Alert.sendKeys()
Tutorial video coming soon

Test Cases

Introduction

JavaScript dialogs are browser-native popups that pause page execution. There are three types:

  1. Simple Alert — shows a message with only an OK button
  2. Confirm Dialog — shows OK and Cancel buttons
  3. Prompt Dialog — shows a text input with OK and Cancel

Selenium uses switchTo().alert() to interact with them. Playwright uses event-based dialog listeners.

Key Methods Summary

Action Selenium (Java) Playwright (JS) Playwright (Python)
Switch to alert driver.switchTo().alert() page.on("dialog", ...) page.on("dialog", ...)
Accept (OK) alert.accept() dialog.accept() dialog.accept()
Dismiss (Cancel) alert.dismiss() dialog.dismiss() dialog.dismiss()
Read message alert.getText() dialog.message dialog.message
Type in prompt alert.sendKeys("text") dialog.accept("text") dialog.accept("text")

1. Simple Alert — accept it

Selenium (Java)

driver.findElement(By.id("simpleAlertBtn")).click();
 
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();

Playwright (JS)

page.once("dialog", async (dialog) => {
  console.log(dialog.message());
  await dialog.accept();
});
await page.locator("#simpleAlertBtn").click();

Playwright (Python)

page.once("dialog", lambda dialog: dialog.accept())
page.locator("#simpleAlertBtn").click()

2. Confirm Dialog — accept and dismiss

Selenium (Java)

// Accept
driver.findElement(By.id("confirmAlertBtn")).click();
driver.switchTo().alert().accept();
 
// Dismiss
driver.findElement(By.id("confirmAlertBtn")).click();
driver.switchTo().alert().dismiss();

Playwright (JS)

// Accept
page.once("dialog", (dialog) => dialog.accept());
await page.locator("#confirmAlertBtn").click();
 
// Dismiss
page.once("dialog", (dialog) => dialog.dismiss());
await page.locator("#confirmAlertBtn").click();

Playwright (Python)

# Accept
page.once("dialog", lambda d: d.accept())
page.locator("#confirmAlertBtn").click()
 
# Dismiss
page.once("dialog", lambda d: d.dismiss())
page.locator("#confirmAlertBtn").click()

3. Prompt Dialog — type text and accept

Selenium (Java)

driver.findElement(By.id("promptAlertBtn")).click();
Alert prompt = driver.switchTo().alert();
System.out.println(prompt.getText());
prompt.sendKeys("QA Playground");
prompt.accept();

Playwright (JS)

page.once("dialog", async (dialog) => {
  console.log(dialog.defaultValue());
  await dialog.accept("QA Playground");
});
await page.locator("#promptAlertBtn").click();

Playwright (Python)

page.once("dialog", lambda d: d.accept("QA Playground"))
page.locator("#promptAlertBtn").click()

4. Verify result after alert interaction

Selenium (Java)

driver.findElement(By.id("confirmAlertBtn")).click();
driver.switchTo().alert().accept();
 
WebElement result = driver.findElement(By.id("alertResult"));
assertTrue(result.isDisplayed());
assertEquals("You clicked OK!", result.getText());

Playwright (JS)

page.once("dialog", (d) => d.accept());
await page.locator("#confirmAlertBtn").click();
await expect(page.locator("#alertResult")).toHaveText("You clicked OK!");

Playwright (Python)

page.once("dialog", lambda d: d.accept())
page.locator("#confirmAlertBtn").click()
expect(page.locator("#alertResult")).to_have_text("You clicked OK!")

5. Wait for alert to appear

Selenium (Java)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();

Playwright (JS)

// Playwright auto-waits — register handler before triggering
page.once("dialog", (d) => d.dismiss());
await page.locator("#triggerBtn").click();

Playwright (Python)

# Register before triggering
page.once("dialog", lambda d: d.dismiss())
page.locator("#triggerBtn").click()

📄 Also Read: Top 10 Best Automation Practice Website

Frequently Asked Questions