How to test Alerts and Popups in Selenium: The Ultimate Guide
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.
Table Of Contents
- 1 What is an Alert/Popup?
- 2 What are the different types of alerts/popups that you might encounter?
- 3 How you should handle an Alert in Selenium WebDriver
- 4 How should you handle web dialog boxes/popup windows using Selenium?
- 5 An easier alternative to Selenium to test popups – Testsigma
- 6 Summary
- 7 Frequently Asked Questions:
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.
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:
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.
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.
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:
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
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 no-code alternative to Selenium that is open source, easy to use and reduces your test creation and maintenance time as well. Not only that, Testsigma lets you automate tests for web, mobile, APIs, as well as Desktop.
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:
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.