Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Handling pop-ups and Alerts in Selenium

Handling pop-ups and Alerts in Selenium

January 17th, 2020

Handling pop-ups and Alerts in Selenium

There is a guideline that needs to proceed while doing web page testing. The system will throw alerts if we miss following these rules. Dealing with alerts when testing the application is so repetitive process and it is time-consuming.
The below topics will be covered as part of this article.

  • Alert and Usage of Alert in Selenium
  • Alert types in Selenium
  • Handling of Alerts in the Selenium WebDriver
  • Handling of popups in the Selenium WebDriver
  • Applications where Alerts are used

What is meant by Alert in Selenium?

Generally, Alert is defined as the interface between UI (User Interface) and web page. It is a mini message box that displays a notification on the screen to notify the user of any information or to seek permission to do any operations. Alerts are used for warning purposes too. For example, if the user is testing a web application and trying to log into the application by giving invalid data into mandatory fields like username and password, then the system will be throwing an alert.
Alert and Popup

Why Alerts are used?

Basically, Alert is used for displaying the warning messages. It will display like pop-ups on the screen. Alerts will be displayed because of more user actions in the screen. For instance, if the user clicked on the button which will display a message or HTML page will ask for more information while filling an online form. This is called as an alert.

Alert Types in Selenium

Alerts are classified into three types,

  • Simple Alert
  • Prompt Alert
  • Confirmation Alert

Simple Alert:

The simple alert is typically used to show some information for the end-user. An alert message which has just an OK button in it is called the simple alert. The simple alert will be the first alert in the test page. The below code is used to read text from Alert and it will accept that alert.
Alert simpleAlert = driver.switchTo().alert();

Simple Alert

Prompt Alert

The prompt alert is mainly used to get some inputs from the end-user. It will have a way to include a text message to alert the box. Sendkeys() method is used to type a text in the alert box.

promptAlert.sendKeys(“Alert accepted”);

Confirmation Alert:

Confirmation alert is displayed with an accept or reject option. Alert.accept() method is used to accept and Alert.dismiss() method is used to reject the alert.

Conform Alert

Handling of Alerts in the Selenium WebDriver

Selenium WebDriver has some functionalities used to handle the alerts which are a difficult task. Let us see how it will happen. When a user runs the test script, driver control still exists on a browser even though the alert gets generated. Interface methods are used to do some actions like accepting alerts, dismissing alerts, writing text on alert window, capturing text from alert window. When user changes the control to alert the window from the current browser window. Lets we discuss in detail below.
Void dismiss():
This method gets invoked when the user clicks on the ‘Cancel’ button in an alert box

Void accept():
This method gets invoked when the user clicks on the ‘Ok’ button in an alert box
String getText():
This method gets invoked when user wants to capture alert message.
Void sendKeys:
This method gets invoked when user wants to post some text/data into alert box

Handling of Pop-up window in the Selenium WebDriver:

Like Alert handling, Pop-up handling in Selenium is also a tricky task when testing the application. WindowHandle() function is used for handling the pop-up windows. To control the mouse and keyboard functions, the robot class is used. Robot class will be used for closing pop-ups. The integration of Alert and popup in a single Program and executing the program code with Eclipse IDE.

import org.openqa.selenium.Alert;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class AlertHandlingDemo {
public static void main(String[] args) throws InterruptedException  {
System.setProperty(“webdriver.chrome.driver”, “C:\\chromedriver.exe”);
WebDriver driver =new ChromeDriver();
// Switching to Alert
Alert alert = driver.switchTo().alert();
// Capturing alert message.
String alertMessage= driver.switchTo().alert().getText();
// Displaying alert message
// Accepting alert

Applications where Alerts are used

Alerts are widely used in the following areas, namely:

  • e-commerce website
  • Banking sectors
  • Filling of online HTML application forms

Related Blogs