Handling different types of Alerts in Selenium WebDriver

Today we will discuss different types of alerts that we get on performing different activities on a web application. Selenium has separate provision to handle such alerts. Firstly, we will understand those alerts then we will know how to handle them!

Let’s start!

Are you getting Disable Developer Mode pop up in Google Chrome?

I’ve written an exclusive post to handle disable developer mode extension pop up for you. Click on the link below to read:

Disable Developer Mode Extension in Google Chrome

What is an Alert?

Alert is a kind of push notification, which is generally blocking in nature. It asks the user for some information in which confirming and rejecting an alert also included. An alert may ask you some information to provide through text box; even it throws some information on the screen. Likewise, Alert performs various activities where user consent, as well as user interaction, has a vital role.

Alert could be a part of the functional scenario in your web application testing project. Some tests may fail if it does not pop-ups messages through an alert. Hence, we need to handle Alert with utmost care in Selenium WebDriver.

What are the types of Alert?

There are mainly three types of alerts that we get in web application testing. These three alerts are handled differently. There is different business logic to use them.

Let’s discuss different types of alerts.

Simple Alert

The simple alert displays some information, warning message or some kinds of reminder. Developers place such alert as a checkpoint to make sure user is providing the correct information.

Simple Alert

Simple alert holds mainly two types of data, first the information to display and a button to close the alert pop up which confirms you got the information.

Click on the button below to see Simple Alert Message:

 

Confirm Alert

The confirm alert is mainly used to perform some kind of operation. A user can either accept the alert or dismiss it when confirming alert pops up. So this alert has mainly three data, an operational message, one button to accept it and another button to dismiss it.

Confirm Alert

Click on the button below to see Confirm Alert Message:

Prompt Alert

The prompt alert asks some kind of data from the user. It asks mainly quick data, like Yes or No, Some numeric data, etc. Hence, the prompt alert displays mainly four data- a question, a text area to enter your response, a button to send your response and another button if you disagree then this pop up will be dismissed.

Prompt Alert

Click on the button below to see Prompt Alert Message:

How to handle different types of Alerts in Selenium WebDriver?

Now we have to handle such alert in a different manner, but the initial declaration of the alert will be the same.

Before everything we need to import below from Selenium library:

import org.openqa.selenium.Alert;

Accept Alert

Accepting alert is like clicking on OK button basically. You won’t get any web elements for the Ok button here as these alerts are coming from the script. So here is the code snippet to accept the alert.

//Alert Declaration
Alert alert = driver.switchTo().alert();
	  	  
//Accepting Alert
alert.accept();

Here first we declare alert and we are giving the command to web driver to switch to alert, then we are accepting the alert. Likewise, we follow similar steps in all the alert operation.

Dismiss Alert

Dismissing alert simply means that we are not at all accepting the alert; hence we are clicking on Cancel button. Again you won’t be able to find the web elements to dismiss the alert since it is covered inside a script.  Hence, you need to follow steps as above.

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

//Dismissing an alert
alert.dismiss();

Capture the Alert message

Here also follow the above steps to capture the alert message. Following code snippet helps you to capture the alert message.

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

//Capturing the alert message
alert.getText();

Send your response to Alert

Alert also allows you to send your response. Follow the above steps in this case too, but we use sendKeys() to send our response to an alert. Following code snippet describes well:

//Alert Declaration
 Alert alert = driver.switchTo().alert();
//Sending response to Alert
 alert.sendKeys(arg0);

 

Sample Alert Handling Selenium Test Case:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

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

public class HandlingAlert {
  @Test
  public void alertOperation() {
	  	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  	  WebDriver driver = new ChromeDriver();
	  	  
	  	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  	  
	  	  driver.get(" http://www.inviul.com/handling-alerts-selenium/");
	  	  
	  	  driver.manage().window().maximize();
	  	  
		  //Alert Declaration
	  	  Alert alert = driver.switchTo().alert();
	  	  
	  	  //Accepting Alert
	  	  alert.accept();
	  	
	  	  driver.close();
	  	  
	  	  driver.quit();
	  	  
	    }
}

 

 

Leave a Reply