Handling Alerts with Selenium WebDriver: Automating Popup Interactions

Learn how to effectively handle various alert types (JavaScript pop-ups, confirmations, prompts) within your Selenium WebDriver tests. This tutorial provides a step-by-step guide and code examples demonstrating how to automate interactions with alerts for robust and comprehensive web testing.



Handling Alerts with Selenium WebDriver

Understanding Alert Handling in Selenium

This section shows you how to manage alerts (pop-up boxes) in Selenium WebDriver. Selenium offers several methods for interacting with different alert types.

Selenium's Alert Handling Methods

  1. void dismiss(): Clicks the "Cancel" button on an alert box.
    Syntax: driver.switchTo().alert().dismiss();
  2. void accept(): Clicks the "OK" button on an alert box.
    Syntax: driver.switchTo().alert().accept();
  3. String getText(): Retrieves the text message displayed in the alert box.
    Syntax: driver.switchTo().alert().getText();
  4. void sendKeys(String stringToSend): Sends text input to an alert box (if it has an input field).
    Syntax: driver.switchTo().alert().sendKeys("Text");

Example: Automating Alert Handling

Let's automate a test case that involves handling alerts. This example will interact with the following scenarios on this webpage: https://www.testandquiz.com/selenium/testing.html:

  • Open the URL
  • Click "Generate Alert Box"
  • Click "Generate Confirm Box"
  • Close the browser

We'll build this test step-by-step.

Step-by-Step Guide

  1. Set up your environment: Open Eclipse, create a new class file (e.g., "alert_test").
  2. Configure Gecko Driver: This step assumes you've already downloaded the GeckoDriver (for Firefox). Set the system property for the driver path. (Refer to previous tutorials if you need help with this step.)
  3. Launch Firefox: The following code launches Firefox:
  4. Java Code (Launching Firefox)
    
    // System Property for Gecko Driver
    System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
    // Initialize Gecko Driver using Desired Capabilities Class
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette",true);
    WebDriver driver= new FirefoxDriver(capabilities);
        
  5. Navigate to the URL: The following code navigates to the test webpage:
  6. Java Code (Navigating to URL)
    
    // Launch Website
    driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
        
  7. Locate Buttons: Inspect the webpage's HTML to find the "Generate Alert Box" and "Generate Confirm Box" buttons. Note their link text.
  8. Handle Alerts: This code handles both alert types:
  9. Java Code (Alert Handling)
    
    //Handling alert boxes
    //Click on generate alert button
    driver.findElement(By.linkText("Generate Alert Box")).click();
    //Using Alert class to first switch to or focus to the alert box
    Alert alert = driver.switchTo().alert();
    //Using accept() method to accept the alert box
    alert.accept();
    
    //Handling confirm box
    //Click on Generate Confirm Box
    driver.findElement(By.linkText("Generate Confirm Box")).click();
    Alert confirmBox = driver.switchTo().alert();
    //Using dismiss() command to dismiss the confirm box
    confirmBox.dismiss();
        

Complete Java Code

Complete Java Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Alert;

public class alert_test {
    public static void main(String[] args) {
        // System Property for Gecko Driver
        System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
        // Initialize Gecko Driver using Desired Capabilities Class
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette",true);
        WebDriver driver= new FirefoxDriver(capabilities);
        // Launch Website
        driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
        //Handling alert boxes
        //Click on generate alert button
        driver.findElement(By.linkText("Generate Alert Box")).click();
        //Using Alert class to first switch to or focus to the alert box
        Alert alert = driver.switchTo().alert();
        //Using accept() method to accept the alert box
        alert.accept();
        //Handling confirm box
        //Click on Generate Confirm Box
        driver.findElement(By.linkText("Generate Confirm Box")).click();
        Alert confirmBox = driver.switchTo().alert();
        //Using dismiss() command to dismiss the confirm box
        confirmBox.dismiss();
        driver.close(); // Close the browser
    }
}

Running this script will automate the alert handling process.