Happy to see you another new post of Softwaretestingo, we are going to discuss another new concept of selenium WebDriver tutorial series. In this post, we are going to discuss What is alert? How can you handle the window based and web-based alerts through Java selenium automation script?
When we are testing an application, we test that application with some set of predefined rules, and when it does not expect as per the predefined rules, it will send an error or an alert is displaying. So dealing with those alerts is a challenging task, 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 one by one and with some real-time examples for better understanding.
What is an Alert?
Alerts are nothing but small message boxes that are displayed on the screen to give some information or ask permission to perform some operation. Sometimes its also use for warning purposes.
Types of Alerts in Selenium
Mainly we are dealing with two types of alerts that are:
- Windows-based alert pop-ups
- Web-based alert pop-ups
As we know that by using the Selenium WebDriver its is not possible to test window bases 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 types of the alert 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 types of the alert are mainly used when you need some more information at the time of running your automation script. To enter the information in the alert box, you can you the sendKeys() method.
Confirmation alert: This type of os alerts are coming 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 you can handle different types of alert easily.
How to handle Alerts in Selenium WebDriver?
Handling alerts is one of the tricky tasks, but Selenium WebDriver provides some of the predefined methods, which makes the handling process so easy.
When we are executing our automation script, we are creating an object of browser class, and that driver object has control over all the browser operations even on alert too. For handling the alert, you can use the alert interface methods to perform 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 on the Ok button. driver.switchTo().alert().accept();
- String getText(): By using this method you can get the alert message. 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
Leave a Reply