0
0
Selenium Javatesting~10 mins

Alert accept and dismiss in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page with a JavaScript alert, then it accepts the alert and verifies the confirmation message. Next, it triggers a confirm alert, dismisses it, and verifies the cancellation message.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AlertAcceptDismissTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/javascript_alerts");
    }

    @Test
    public void testAlertAcceptAndDismiss() {
        // Click button to trigger alert
        driver.findElement(By.xpath("//button[text()='Click for JS Alert']")).click();

        // Switch to alert and accept it
        Alert alert = driver.switchTo().alert();
        alert.accept();

        // Verify result text after accepting alert
        String resultText = driver.findElement(By.id("result")).getText();
        assertEquals("You successfully clicked an alert", resultText);

        // Click button to trigger confirm alert
        driver.findElement(By.xpath("//button[text()='Click for JS Confirm']")).click();

        // Switch to alert and dismiss it
        Alert confirmAlert = driver.switchTo().alert();
        confirmAlert.dismiss();

        // Verify result text after dismissing confirm alert
        String confirmResult = driver.findElement(By.id("result")).getText();
        assertEquals("You clicked: Cancel", confirmResult);
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser is open at https://the-internet.herokuapp.com/javascript_alerts-PASS
2Finds and clicks the 'Click for JS Alert' buttonJavaScript alert popup appears-PASS
3Switches to alert and accepts itAlert is closed, page shows result messageVerify result text is 'You successfully clicked an alert'PASS
4Finds and clicks the 'Click for JS Confirm' buttonJavaScript confirm alert popup appears-PASS
5Switches to confirm alert and dismisses itConfirm alert is closed, page shows result messageVerify result text is 'You clicked: Cancel'PASS
6Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Alert does not appear or alert text is different than expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after clicking the 'Click for JS Alert' button?
ASwitches to alert and dismisses it
BSwitches to alert and accepts it
CIgnores the alert and continues
DCloses the browser immediately
Key Result
Always switch to the alert before interacting with it, and verify the page shows the expected message after accepting or dismissing alerts.