testsigma
left-mobile-bg

How to test Alerts and Popups in Selenium: The Ultimate Guide

November 25, 2024
Supratik Ray
right-mobile-bg
How to test Alerts and Popups in Selenium: The Ultimate Guide
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

Remember that time when you were browsing a website and got caught by a pop-up asking you to download a guide? Just because you could not find a button to escape that pop-up, you ended up leaving it!

Imagine this happens to your application. You might end up losing potential users. As a website owner, this is the worst thing that could happen to you. Thus testing such alerts and popups is essential every time you make a release. A quicker way to do this is to automate your tests

Selenium is a widely used solution for testing every aspect of your web application, including alerts and popups.

In this article, we will explore how to automate your tests for alerts and popups. 

What is an Alert/Popup?

An alert inside a browser is a small window that appears on a web page and displays a message to users. 

A “popup” usually refers to a separate window or tab that opens in a web browser. Popups can be triggered by various user actions, such as clicking a link or button or by JavaScript code running on a web page.

What are the different types of alerts/popups that you might encounter?

  • Simple Alerts: These are simple notifications that display a message and an “OK” button. They are used to inform the user about something. Some real-world examples of simple alerts are:

    • A reminder on your calendar app, 
    • A warning saying “site not trusted”
    • An error message saying “invalid username or password”
    • A software alerting that an update is available for the same.

  • Confirmation Alerts: These are used to confirm an action with the user. They have a message and two buttons: “Confirm” and “Cancel.” Here are a few real-life experiences of you facing confirmation alerts:

    • An alert on an app asking you to confirm if you want to grant an app permission to access your device’s camera or microphone
    • A message asking if you’re sure you want to navigate away from a web page with unsaved changes
    • A message asking you if you want to close the streaming platform when an audio or a video is in play
    • A dialog box asking you if you want a website to access your location
    • A website asking you if you want to allow all cookies
  • Prompt Alerts: These will ask the user for input. They have a message and a text field, as well as “OK” and “Cancel” buttons. Some general prompt alerts include:

    • A prompt asking you to enter a password to unlock your device or application
    • A dialog box asking you to enter a search query
    • A prompt asking you to enter a new username or password when you’re on a website trying to create a new account
    • A prompt asking you to enter the SSID of a wireless network.
  • Authentication Alerts: These alerts ask the user for a username and password. They are commonly used in web applications that are protected by a login. Real-world authentication alerts may include:
    • A dialog box asking you for a username and password to access a protected API
    • A message asking you for a username and password to log into a VPN
    • A prompt asking for your username and password to log into a netbanking portal
  • Warning/Error Alerts: These alerts display warning or error messages to the user. They usually have a message and an “OK” button. You get warning/ error alerts when working on a digital device. Some warning/error alerts include:

    • Your device is running low on storage
    • You’ve exceeded the number of login attempts on a website
    • You’ve entered a wrong UPI pin
    • You are trying to delete a critical system file

How you should handle an Alert in Selenium WebDriver

This example demonstrates how to switch to the alert, get the text of the alert, and interact with it by either accepting it, dismissing it, or sending a text to it. The code below is written using Java and Selenium WebDriver:

import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class AlertExample { public static void main(String[] args) throws InterruptedException { // Setting the path of chrome driver System.setProperty(“webdriver.chrome.driver”, “Example Path”); // Creating an instance of chrome webdriver WebDriver driver = new ChromeDriver(); // Navigating to the website driver.get(“http://eg.com/alerts”); // Finding the element that triggers the alert driver.findElement(By.id(“alertButton”)).click(); // Switching to the alert Alert alert = driver.switchTo().alert(); // Getting the text of the alert String alertText = alert.getText(); System.out.println(“Alert text: ” + alertText); // Clicking the OK button alert.accept(); // Clicking the Cancel button alert.dismiss(); // Sending a text to the alert alert.sendKeys(“Example text to the alert”); // Close the browser driver.quit(); } }

How should you handle web dialog boxes/popup windows using Selenium?

There are a few different ways to test a dialog box or a popup in Selenium.

1. Dismissing the dialog box or popup window: You can use the dismiss() method to dismiss an alert or confirm the dialog box. You can also use the send_keys() method to send a text to a prompt dialog box before dismissing it.

// Dismiss an alert Alert alert = driver.switchTo().alert(); alert.dismiss(); // Dismiss a confirm dialog Alert confirm = driver.switchTo().alert(); confirm.dismiss(); // Dismiss a prompt dialog Alert prompt = driver.switchTo().alert(); prompt.sendKeys(“Text to Prompt”); prompt.dismiss();

2. Accepting the dialog box or popup window: You can use the accept() method to accept an alert or confirm the dialog box. The send_keys() function can also be used to enter text into a prompt dialogue box before closing it.

// Accept an alert Alert alert = driver.switchTo().alert(); alert.accept(); // Accept a confirm dialog Alert confirm = driver.switchTo().alert(); confirm.accept(); // Accept a prompt dialog Alert prompt = driver.switchTo().alert(); prompt.sendKeys(“Text to Prompt”); prompt.accept();

3. Handling dialog boxes or popup windows that appear unexpectedly: You can use the ExpectedConditions() and the until() method of the WebDriverWait class to wait for a dialog box or popup window to appear and then handle it as described below:

import org.openqa.selenium.By; import org.openqa.selenium.Alert; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class AlertHandlingExample { public static void main(String[] args) { WebDriver driver = …; // create an instance of the web driver // Wait for an alert to appear, and then dismiss it WebDriverWait wait = new WebDriverWait(driver, 10); Alert alert = wait.until(ExpectedConditions.alertIsPresent()); alert.dismiss(); // Wait for a confirm dialog to appear, and then accept it wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“confirm-dialog”))); alert = driver.switchTo().alert(); alert.accept(); // Wait for a prompt dialog to appear, enter some text, and then accept it wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“prompt-dialog”))); alert = driver.switchTo().alert(); alert.sendKeys(“Enter the text”); alert.accept(); } }

An easier alternative to Selenium to test popups – Testsigma

Selenium, sometimes, can be time-consuming and tough for beginners. It requires knowledge of code and a lot of setup at your end. If you are looking for an open-source tool that is very easy to set up and reduces most of your test creation and maintenance time, then you should try Testsigma. 

For a detailed comparison between Testsigma and Selenium, check here: https://testsigma.com/comparison-automated-testing-tools-selenium-testsigma 

Summary

Here, we covered the various types of alerts and pop-ups that you might encounter while testing your web application. We also discussed the best way to automate them using Selenium.

While Selenium is widely used for web browser automation, it can sometimes become challenging for a team that is not well-versed in coding. Testsigma is a low code test automation tool that lets you automate your tests for web, mobile, desktop and APIs, 10x faster, and from the same place. You can author, maintain and execute your tests – all from the same platform. 

Explore Testsigma to achieve a better test automation ROI, faster.

Start here

Frequently Asked Questions:

How many types of popups can be tested via Selenium?

There are mainly six types of popups that you can test using Selenium. They are:

  • Alert popups
  • Confirm popups
  • File upload popups
  • Hidden division popups
  • Prompt popups
  • New window popups 

How to write code for popups in Selenium?

To handle alerts in Selenium, you can use the ‘Alert class’. Here is an example of how to carry out a simple alert popup handling in Selenium using Java:

import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class AlertPopup { public static void main(String[] args) throws InterruptedException { System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”); WebDriver driver = new ChromeDriver(); driver.get(“http://www.example.com/alert”); driver.findElement(By.id(“alert-button”)).click(); Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); System.out.println(“Alert text: ” + alertText); alert.accept(); driver.quit(); } }

You can modify this code as needed to handle different types of popups or perform various actions. For example, you can use alert.dismiss() to dismiss the alert instead of accepting it.

Can we automate popups in Selenium?

Yes, you can automate a popup in Selenium using the ‘Alert’ class.

Testsigma Author - Supratik Ray

Supratik Ray

With experience across countless domains, Supratik is an avid writer and editor with a significant grasp of technical blogs.

image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

By submitting the form, you would be accepting the Privacy Policy.

RELATED BLOGS


How to Write Test Cases for Trading Application Testing?
RITIKA KUMARI
TEST AUTOMATIONTESTING DISCUSSIONS
Scriptless Test Automation | What , Why it Matters & Examples
KIRUTHIKA DEVARAJ
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
RAUNAK JAIN
TEST AUTOMATION