0
0
Selenium Javatesting~15 mins

Alert accept and dismiss in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify alert accept and dismiss functionality
Preconditions (1)
Step 1: Click the button that triggers the alert
Step 2: Switch to the alert
Step 3: Accept the alert
Step 4: Verify the alert is closed and the expected success message is displayed
Step 5: Click the button that triggers the alert again
Step 6: Switch to the alert
Step 7: Dismiss the alert
Step 8: Verify the alert is closed and the expected cancellation message is displayed
✅ Expected Result: Alert is accepted and dismissed correctly, and the correct messages appear after each action
Automation Requirements - Selenium WebDriver with Java and TestNG
Assertions Needed:
Verify alert text before accepting or dismissing
Verify the success message after accepting the alert
Verify the cancellation message after dismissing the alert
Best Practices:
Use explicit waits to wait for alert presence
Use try-catch to handle NoAlertPresentException
Use Page Object Model to separate page actions
Use TestNG assertions for validation
Automated Solution
Selenium Java
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;

public class AlertAcceptDismissTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/alert-test"); // Replace with actual URL
    }

    @Test
    public void testAlertAccept() {
        WebElement alertButton = driver.findElement(By.id("alertButton"));
        alertButton.click();

        // Wait for alert to be present
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());

        // Verify alert text
        String alertText = alert.getText();
        Assert.assertEquals(alertText, "Are you sure you want to proceed?", "Alert text mismatch");

        // Accept the alert
        alert.accept();

        // Verify success message displayed after accepting
        WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("message")));
        Assert.assertEquals(message.getText(), "You accepted the alert!", "Success message mismatch after accept");
    }

    @Test(dependsOnMethods = "testAlertAccept")
    public void testAlertDismiss() {
        WebElement alertButton = driver.findElement(By.id("alertButton"));
        alertButton.click();

        Alert alert = wait.until(ExpectedConditions.alertIsPresent());

        // Verify alert text
        String alertText = alert.getText();
        Assert.assertEquals(alertText, "Are you sure you want to proceed?", "Alert text mismatch");

        // Dismiss the alert
        alert.dismiss();

        // Verify cancellation message displayed after dismissing
        WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("message")));
        Assert.assertEquals(message.getText(), "You dismissed the alert!", "Cancellation message mismatch after dismiss");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses Selenium WebDriver with TestNG to automate alert accept and dismiss actions.

setUp(): Opens the browser and navigates to the test page.

testAlertAccept(): Clicks the button to trigger alert, waits for alert, verifies alert text, accepts alert, then verifies the success message.

testAlertDismiss(): Clicks the button again, waits for alert, verifies alert text, dismisses alert, then verifies the cancellation message.

Explicit waits ensure the alert and messages are present before interacting or asserting. Assertions check the alert text and messages to confirm correct behavior. The @AfterClass method closes the browser after tests.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits for alert presence
Not switching to alert before accepting or dismissing
Hardcoding alert text without verifying it
Not verifying the result message after alert action
Bonus Challenge

Now add data-driven testing with 3 different alert messages and expected results

Show Hint