In this Selenium Tutorial, you will learn how to handle alerts in Selenium Java with real-time code examples and screenshots. We’ll cover various types of alerts and provide practical examples to help you manage them effectively in your automated tests.

What are Alerts in Selenium Java

In Selenium, an alert is a pop-up window that interrupts the normal flow of execution to display a message or prompt the user for input. Selenium provides methods to handle these alerts, allowing you to accept, dismiss, or retrieve information from them in your automated tests.

Types of Alerts in Selenium Java

Simple Alert: Displays a basic message and an “OK” button to close the alert.
Confirmation Alert: Asks the user to confirm an action with “OK” and “Cancel” buttons.
Prompt Alert: Requests user input with a text box and provides “OK” and “Cancel” buttons.

Methods to Handle Alerts in Selenium Java

  1. accept(): Clicks the “OK” button on the alert, effectively accepting it.
Alert alert = driver.switchTo().alert();
alert.accept();

2. dismiss(): Clicks the “Cancel” button on the alert, effectively dismissing it.

Alert alert = driver.switchTo().alert();
alert.dismiss();

3. getText(): Retrieves the text message displayed on the alert, allowing you to capture and use the message in your test.

Alert alert = driver.switchTo().alert();
String alertText = alert.getText();

4. sendKeys(String text): Enters text into a prompt alert’s input field, enabling you to provide user input.

Alert alert = driver.switchTo().alert();
alert.sendKeys("Your input text here");
alert.accept();

Handling Alerts in Selenium Webdriver using Java

How to handle simple alerts in Selenium WebDriver?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class HandleingSimpleAlert {

	public static void main(String[] args) throws InterruptedException {

		// TODO Auto-generated method stub

		WebDriverManager.chromedriver().setup();

		// Create an instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();

		// Delete all cookies.
		driver.manage().deleteAllCookies();

		// Maximize the browser.
		driver.manage().window().maximize();

		// Navigate to a website
		driver.get("https://the-internet.herokuapp.com/javascript_alerts");

		Thread.sleep(3000);

		driver.findElement(By.xpath("//button[normalize-space()='Click for JS Alert']")).click();

		Thread.sleep(3000);

		driver.switchTo().alert().accept();

		Thread.sleep(3000);

		String expText = "You successfully clicked an alert";

		String actText = driver.findElement(By.id("result")).getText();

		if (actText.contentEquals(actText)) {
			System.out.println("Test Case Pass");
		} else {
			System.out.println("Test Case Fail");
		}

		// close the WebDriver instance
		driver.quit();

	}

}

How to handle confirmation alert in Selenium Java?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class HandielingConfirmAlert {

	public static WebDriver driver;

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		WebDriverManager.chromedriver().setup();

		// Create an instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();

		// Delete all cookies.
		driver.manage().deleteAllCookies();

		// Maximize the browser.
		driver.manage().window().maximize();

		// Navigate to a website
		driver.get("https://the-internet.herokuapp.com/javascript_alerts");

		Thread.sleep(3000);

		// acceptMethod();

		// Thread.sleep(3000);
		// dismissMethod();
		driver.findElement(By.xpath("//button[normalize-space()='Click for JS Confirm']")).click();

		Thread.sleep(3000);

		// Alert window with okay button
		driver.switchTo().alert().accept();

		Thread.sleep(3000);

		String expText = "You successfully clicked an alert";

		String actText = driver.findElement(By.id("result")).getText();

		if (expText.contentEquals(actText)) {
			System.out.println("Test Case Pass: " + actText);
		} else {
			System.out.println("Test Case Fail: " + actText);
		}

		Thread.sleep(3000);

		// Alert window with cancel button

		driver.findElement(By.xpath("//button[normalize-space()='Click for JS Confirm']")).click();

		Thread.sleep(3000);

		driver.switchTo().alert().dismiss();

		Thread.sleep(3000);

		String expTextDismiss = "You clicked: Cancel";

		String actTextDismiss = driver.findElement(By.id("result")).getText();

		if (expTextDismiss.contentEquals(actTextDismiss)) {
			System.out.println("Test Case Pass: " + actTextDismiss);
		} else {
			System.out.println("Test Case Fail: " + actTextDismiss);
		}

		// close the WebDriver instance
		driver.quit();

	}

}

How to handle prompt alert in selenium java with example

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class HandlingPromteAlert {

	public static void main(String[] args) throws InterruptedException {

		// TODO Auto-generated method stub

		WebDriverManager.chromedriver().setup();

		// Create an instance of the ChromeDriver
		WebDriver driver = new ChromeDriver();

		// Delete all cookies.
		driver.manage().deleteAllCookies();

		// Maximize the browser.
		driver.manage().window().maximize();

		// Navigate to a website
		driver.get("https://the-internet.herokuapp.com/javascript_alerts");

		Thread.sleep(3000);

		driver.findElement(By.xpath("//button[normalize-space()='Click for JS Prompt']")).click();

		Thread.sleep(3000);

		Alert alert = driver.switchTo().alert();

		String alertText = alert.getText();
		System.out.println(alertText);

		alert.sendKeys("Welcome to Selenium Tutorial");

		alert.accept();

		Thread.sleep(3000);

		String expText = "You entered: Welcome to Code Collaborate";

		String actText = driver.findElement(By.id("result")).getText();

		if (actText.contentEquals(actText)) {
			System.out.println("Test Case Pass");
		} else {
			System.out.println("Test Case Fail");
		}

		// close the WebDriver instance
		driver.quit();

	}

}

By Avinash

Leave a Reply

Your email address will not be published. Required fields are marked *