How to handle alerts in Selenium WebDriver

Selenium WebDriver is able to handle alerts in addition to the windows and frames.

What is an alert?

Alert is a popup or message box which displays on the screen. Alerts can appear on the screen due to various user actions. For example, alert can appear when you click on a button or trying to navigate to other page.


selenium alert handling

 

Syntax:

Alert alert;


Alert is an interface which has below methods to execute different actions.

void accept() – It is similar to the Ok button. It clicks on the Ok button on the alert box.

void dismiss() – It is similar to the Cancel button.

void sendKeys() – It send the text on to the alert box.

getText() – It retrieves the text from the alert box. Used to evaluate the text present on the prompt or alert boxes.

Types of Alerts:

  1. Simple alert: It just have an Ok button in it. It is an informative kind of alert.
  2. Prompt alert: This alert asks the user for some information. User can enter the information into this alert using sendKeys() method.
  3. Confirmation alert: This type of alert asks for the user confirmation whether to proceed or not. For instance, when you are trying to delete a file, you may encounter this tyepe of alert.

 

How to handle alerts using Selenium WebDriver:

Example:


package com;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AlertHandling {

static WebDriver driver;
String word = “selenium”;
String altMessage;

@BeforeTest
public void before() {
// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

// implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// this opens the url
driver.get(“file:///C:/Users/username/Desktop/Selenium/NewFolder/alerts.html”);
}

@Test
public void alert() throws Exception {
Thread.sleep(3000);
// click on alert button
driver.findElement(By.id(“alert_id1”)).click();


// changes the focus to displayed alert
Alert alt = driver.switchTo().alert();

// Gets the text present on the alert
altMessage = driver.switchTo().alert().getText();

// prints the alert message
System.out.println(altMessage);

// Accepting alert
alt.accept();
}

@Test
public void confirmPopup() throws Exception {
Thread.sleep(3000);
// click on alert button
driver.findElement(By.id(“alert_id2”)).click();

// changes the focus to displayed alert
Alert alt = driver.switchTo().alert();

// Gets the text present on the alert
altMessage = driver.switchTo().alert().getText();

// prints the alert message
System.out.println(altMessage);

// Accepting alert
alt.accept();

//
driver.findElement(By.id(“alert_id2”)).click();
// Dismisses or Cancel the popup
alt.dismiss();
}

@Test
public void Prompt() throws Exception {
Thread.sleep(3000);
// click on alert button
driver.findElement(By.id(“alert_id3”)).click();

// changes the focus to displayed alert
Alert alt = driver.switchTo().alert();

// Gets the text present on the alert
altMessage = driver.switchTo().alert().getText();

// prints the alert message
System.out.println(altMessage);

// Send text into the prompt dialogue box
alt.sendKeys(“Selenium99”);
// Accepting alert
alt.accept();

driver.findElement(By.id(“alert_id3”)).click();
// Dismisses or Cancel the popup
alt.dismiss();
}

@AfterTest
public void tearDown() {

driver.close();
}
}

Output on the Console:

Hello! This is an alert box.
PASSED: alertMethod

You can try this on yourself by following below steps:

1. Open Notepad.

2. Copy and paste below code.

<!DOCTYPE html>
<html>
<body>

<h2>Click the button to display an alert box/confirm popup.</h2>
<p><b>Alert box:</b></p>
<button id=”alert_id1” onclick=”myFunction1()”>Click here</button>
<br />
<p><b>Confirm popup:</b></p>
<button id=”alert_id2” onclick=”myFunction2()”>Submit</button>
<br />
<p id=”demo”></p>
<p><b>Prompt dialog:</b></p>
<button id=”alert_id3” onclick=”myFunction3()”>Enter</button>
<script>
function myFunction1() {
alert(“Hello! This is an alert box.”);
}

function myFunction2() {
confirm(“Hello! This is a confirm popup.”);
}

function myFunction3() {
var name = prompt(“Please enter your name”, “Selenium user”);
if (name != null) {
document.getElementById(“demo”).innerHTML = “Hello ” + name + “! Have a great day.”;
}
}
</script>

</body>
</html>

3. Save notepad with .html extension.

4. Change the file path in selenium code and run the script.


Check other selenium topics:
TestNG

How to handle mouse hover events using Selenium WebDriver

WebElement actions in selenium WebDriver

Top 25 selenium interview questions

How to take screenshot using Selenium WebDriver


Tags: Selenium handle alerts. Handle alerts using selenium web driver, alerts.