How To Handle Javascript Alerts/PopUps In Selenium

I am happy to see you another new post on Softwaretestingo, and we will discuss another new concept of the Selenium WebDriver tutorial series. In this post, we are going to discuss What an alert is. How can you handle the Windows- and web-based alerts through the Java selenium automation script?

When we are testing an application, we test that application with some predefined rules, and when it does not expect as per the predefined rules, it will send an error or display an alert. So, dealing with those alerts is challenging, but in this tutorial, I will try to share how you can handle these alerts.

During this post, I am trying to cover the below topics:

  • What is an Alert?
  • Types of alerts
  • How you can handle the alerts
  • How you can handle popup windows

So, let us try to understand each topic individually and with real-time examples for better understanding.

What is an Alert?

Alerts are small message boxes displayed on the screen to give some information or ask permission to perform some operation. Sometimes, it’s also used for warning purposes.

Types of Alerts in Selenium

Mainly, we are dealing with two types of alerts:

  • Windows-based alert pop-ups
  • Web-based alert pop-ups

As we know, by using the Selenium WebDriver, it is impossible to test window-based applications; that’s why we are using some third-party tools to handle the window-based pop-ups.

There are mainly 3 types of alerts are there that are:

  • Simple alert
  • Prompt alert
  • Confirmation alert

Simple alert: The simple alert has only an OK button. These alerts are mainly used to display some information to the user.

Prompt alert: In this type of alert, you will get a text box with an OK and cancel button. These alerts are mainly used when you need more information while running your automation script. To enter the information in the alert box, use the sendKeys() method.

Confirmation alert: This operating system alert comes with an option to accept or dismiss. To accept the alert, you can use the alert.accept(), and to dismiss, you can use the method alert.dismiss().

I hope you have an overall idea about the different types of alerts. Now, you can easily understand how to handle different types of alerts.

How to handle Alerts in Selenium WebDriver?

Handling alerts is one of the tricky tasks, but Selenium WebDriver provides some predefined methods, making the handling process easy.

When executing our automation script, we create an object of the browser class, and that driver object has control over all the browser operations, even on alert. To handle the alert, you can use the alert interface methods to perform the required operations.

Alert Methods:

  • Void dismiss(): When you call this method, it will click on the cancel button. driver.switchTo().alert().dismiss();
  • Void accept(): When you call this method, it will click the OK button. driver.switchTo().alert().accept();
  • String getText(): This method lets you get the alert message text. driver.switchTo().alert().getText();
  • Void sendKeys(String stringToSend): This method will help you to send data to the alert box. driver.switchTo().alert().sendKeys(“Text”);

Link: https://www.guru99.com/alert-popup-handling-selenium.html

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment