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
void dismiss()
: Clicks the "Cancel" button on an alert box.
Syntax:driver.switchTo().alert().dismiss();
void accept()
: Clicks the "OK" button on an alert box.
Syntax:driver.switchTo().alert().accept();
String getText()
: Retrieves the text message displayed in the alert box.
Syntax:driver.switchTo().alert().getText();
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
- Set up your environment: Open Eclipse, create a new class file (e.g., "alert_test").
- 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.)
- Launch Firefox: The following code launches Firefox:
- Navigate to the URL: The following code navigates to the test webpage:
- Locate Buttons: Inspect the webpage's HTML to find the "Generate Alert Box" and "Generate Confirm Box" buttons. Note their link text.
- Handle Alerts: This code handles both alert types:
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);
Java Code (Navigating to URL)
// Launch Website
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
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.