Testing With Selenium WebDriver (65 Blogs)
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (55 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to handle Alerts and Pop-ups in Selenium

Published on May 30,2019 1.1K Views
6 / 14 Blog from Selenium Webdriver

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Testing a web page requires a specific set of rules to be followed but, when you tend to miss it, the system will end up throwing an alert. Dealing with these alerts while testing an application is too tedious. Through the medium of this blog on how to handle alerts in Selenium, I will help you understand all about alerts and pop-ups in Selenium.

I will be covering the below topics in this article:

So, let’s get started.

What is an Alert?

When you test a web application using Selenium and try logging in to a website but, fail to add in the mandatory fields like mail ID or the password, the system throws an alert.

Alerts in Selenium - Edureka

Alerts are basically an interface between the current web page and UI. It can also be defined as a small message box which displays an on-screen notification to give the user some kind of information or ask for permission to perform a certain kind of operation. It may be also used for warning purpose.

Now, let’s see why you should use this alert while testing an application. 

Why is an Alert used in Selenium?

Alert is basically used to display a warning message. It is a pop-up window that comes up on the screen. There are many user actions that can result in an alert on the screen. For example, if you click on a button that displays a message or maybe when you entered a form, HTML page asked you for some extra information. This is an alert.

Alerts are the unhandled window when you navigate for the first time to a webpage.

Types of Alerts in Selenium

There are mainly 3 types of Alerts, namely:

  1. Simple Alert
  2. Prompt Alert
  3. Confirmation Alert

Let me explain them in detail: 

  • Simple Alert: A simple alert just has an OK button on them. They are mainly used to display some information to the user. The very first alert on the test page is a simple alert. The following code will read the text from the Alert and then accept the alert.

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

Simple alert - Alerts in Selenium - Edureka
  • Prompt Alert: In prompt alerts, you get an option to add a text field to the alert box. This is specifically used when some input is required from the user. You can use the sendKeys() method to type some text in the Prompt alert box. 
promptAlert.sendKeys("Accepting the alert");

Prompt alert - Alerts in Selenium - Edureka

  • Confirmation Alert: This type of alert comes with an option to accept or dismiss the alert. In order to accept the alert, you can use the Alert.accept()and to dismiss, use Alert.dismiss()
confirmationAlert.dismiss();

Confirmation alert - Alerts in Selenium - Edureka

Once you are clear with the different types of alerts, it will be easy to understand how to handle these alerts.

How to handle Alerts in Selenium WebDriver?

Handling the alerts is a tricky task but, Selenium Webdriver provides functionalities that make this process way too easy. I will help you understand how this can be done.

While running the test script, the driver control will be on the browser even after the alert is generated. Once you switch the control from the current browser to the alert window, you can use the Alert Interface methods to perform the required actions such as accepting the alert, dismissing the alert, getting the text from the alert window, writing some text on the alert window, etc.,

Let us learn about these Alert Interface Methods.

1) Void dismiss()
This method is called when the ‘Cancel’ button is clicked in the alert box.

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

2) Void accept()

This method is called when you click on the ‘OK’ button of the alert.

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

3) String getText()

This method is called to capture the alert message.

driver.switchTo().alert().getText();

4) Void sendKeys(String stringToSend)

This is called when you want to send some data to alert box.

driver.switchTo().alert().sendKeys("Text");

In order to understand how to handle the alerts in Selenium, let’s work on the demo. In this scenario, we will consider using our own Edureka demo site to illustrate how to handle alerts in Selenium Webdriver.

  • Launch the web browser and open the webpage.
  • Click on the button, “Display Alert” which generates the alert box and then click on OK(accept) button.
</pre>
<!DOCTYPE html>
<html>
<head>
<style>

h1 {
color: red;
margin-left: 40px;
}

button {
color: white;
margin-left: 40px;
background-color: black;
border: none;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}

button:hover {
background-color: silver;
color: black;
}

.column {
float: left;
width: 33.33%;
}

/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}

</style>
</head>
<body>
<div class="row">

<div class=column>
<pre>

</pre>
<button>Edureka Courses</button>
<pre>

</pre>
<button>Edureka Youtube Channel</button>
<pre>

</pre>
<button>Edureka Blog</button>
<pre>

</pre>
<button>Edureka Community</button>
</div>

<div class=column>
<div align="center"><img src="Logo.png" alt="Edureka" width="400" height="80"></div>
<h1><center>Alerts in Selenium</center></h1>

<div align="center"><button id="alert" onclick="myFunction()">Display Alert</button></div>
</pre>

</div>

<div class=column>
<pre>

</pre>
<div align="center"><img src="sel.png" alt="Edureka" width="200" height="200"></div>
</div>

</div>

<script>
function myFunction() {
alert("This is a Selenium alert box!");
var person = prompt("Do you like Edureka?", "Yes/No");
var popuResult = confirm("Confirm pop up with OK and Cancel button");
}
</script>

</body>
</html>
<pre>

Hope you guys understood how to handle the alert box while testing an application.

Moving further to the discussion, I will demonstrate how to handle the pop-up window during execution.

How to handle Pop-ups in Selenium Webdriver

Handling the pop-ups is also one of the humdrum tasks, let’s see how to handle the pop-ups while testing an application. 
To handle the pop-ups, we use the robot class to handle the window.

Robot class is used to manage the keyboard and mouse functions. This robot class is used in order to close the pop-up window. We can even get the window handle of the pop-up window using the WindowHandle() function.  

</pre>
<!DOCTYPE html>
<html>
<head>
<style>

h1 {
color: red;
margin-left: 40px;
}

button {
color: white;
margin-left: 40px;
background-color: black;
border: none;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}

button:hover {
background-color: silver;
color: black;
}

.column {
float: left;
width: 33.33%;
}

/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}

</style>
</head>
<body>
<div class="row">

<div class=column>
<pre>

</pre>
<button>Edureka Courses</button>
<pre>

</pre>
<button>Edureka Youtube Channel</button>
<pre>

</pre>
<button>Edureka Blog</button>
<pre>

</pre>
<button>Edureka Community</button>
</div>

<div class=column>
<div align="center"><img src="Logo.png" alt="Edureka" width="400" height="80"></div>
<h1><center>Alerts in Selenium</center></h1>

<div align="center"><button id="PopUp" onclick="return popup(this, 'notes')">PopUp</button></div>

</pre>

</div>

<div class=column>
<pre>

</pre>
<div align="center"><img src="sel.png" alt="Edureka" width="200" height="200"></div>
</div>

</div>

<script type="text/javascript">
function popup()
{
myWindow = window.open("", "myWindow", "width=400,height=200");
myWindow.document.write("<p>This is a selenium popup</p>");
}

</script>
</body>
</html>
<pre>

Let’s integrate the Alert box and the popup box into a single page and execute the code by running it on the Eclipse IDE.


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Alerts {

public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("file:///C:/Users/Vaishnavi/Downloads/alerts%20in%20selenium-edureka.html");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(By.id("alert")).click();
Thread.sleep(3000);
Alert alert = driver.switchTo().alert();
String alertMessage = driver.switchTo().alert().getText();
System.out.println(alertMessage);
Thread.sleep(3000);
alert.accept();
Thread.sleep(3000);
driver.findElement(By.id("PopUp")).click();
Robot robot = new Robot();
robot.mouseMove(400, 5);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(2000);
driver.quit();

}

}

Now, let’s take a look at the application areas of this alerts

Alerts in Selenium: Application areas

  • Alert functions are used mainly in banking, e-commerce sites. 
  • Online application forms

Now with this, we come to an end to this “Alerts in Selenium” blog. I Hope you guys enjoyed this article and understood how to handle Alerts in Selenium. Now that you have understood how Alerts work in Selenium, check out the Selenium Certification Course by Edureka, a trusted online learning company with a network of more than 650,000 satisfied learners spread across the globe. This course is designed to introduce you to the complete Selenium features and its importance in testing software. Got a question for us? Please mention it in the comments section of “Alerts in Selenium” and we will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.