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. For further details, you can refer to the Selenium Certification Course.
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 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.
Find out our Selenium Training in Top Cities/Countries
India | Other Cities/Countries |
Bangalore | US |
Hyderabad | UK |
Pune | Canada |
Chennai | Australia |
Mumbai | Singapore |
Kolkata | Edinburgh |
Now, let’s see why you should use this alert while testing an application.
Why is an Alert used in Selenium?
An alert is a pop-up window that displays a warning message. Many user actions can result in an alert on the screen. For example, if you click on a button that displays a message or if you enter a form, an HTML page may ask 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:
- Simple Alert
- Prompt Alert
- 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();
- 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");
- 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();
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> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%0Ah1%20%7B%0Acolor%3A%20red%3B%0Amargin-left%3A%2040px%3B%0A%7D%0A%0Abutton%20%7B%0Acolor%3A%20white%3B%0Amargin-left%3A%2040px%3B%0Abackground-color%3A%20black%3B%0Aborder%3A%20none%3B%0Apadding%3A%2015px%2032px%3B%0Atext-align%3A%20center%3B%0Atext-decoration%3A%20none%3B%0Adisplay%3A%20inline-block%3B%0Afont-size%3A%2016px%3B%0A%7D%0A%0Abutton%3Ahover%20%7B%0Abackground-color%3A%20silver%3B%0Acolor%3A%20black%3B%0A%7D%0A%0A.column%20%7B%0Afloat%3A%20left%3B%0Awidth%3A%2033.33%25%3B%0A%7D%0A%0A%2F*%20Clear%20floats%20after%20the%20columns%20*%2F%0A.row%3Aafter%20%7B%0Acontent%3A%20%22%22%3B%0Adisplay%3A%20table%3B%0Aclear%3A%20both%3B%0A%7D%0A%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<style>" title="<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> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%3E%0Afunction%20myFunction()%20%7B%0Aalert(%22This%20is%20a%20Selenium%20alert%20box!%22)%3B%0Avar%20person%20%3D%20prompt(%22Do%20you%20like%20Edureka%3F%22%2C%20%22Yes%2FNo%22)%3B%0Avar%20popuResult%20%3D%20confirm(%22Confirm%20pop%20up%20with%20OK%20and%20Cancel%20button%22)%3B%0A%7D%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<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> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%0Ah1%20%7B%0Acolor%3A%20red%3B%0Amargin-left%3A%2040px%3B%0A%7D%0A%0Abutton%20%7B%0Acolor%3A%20white%3B%0Amargin-left%3A%2040px%3B%0Abackground-color%3A%20black%3B%0Aborder%3A%20none%3B%0Apadding%3A%2015px%2032px%3B%0Atext-align%3A%20center%3B%0Atext-decoration%3A%20none%3B%0Adisplay%3A%20inline-block%3B%0Afont-size%3A%2016px%3B%0A%7D%0A%0Abutton%3Ahover%20%7B%0Abackground-color%3A%20silver%3B%0Acolor%3A%20black%3B%0A%7D%0A%0A.column%20%7B%0Afloat%3A%20left%3B%0Awidth%3A%2033.33%25%3B%0A%7D%0A%0A%2F*%20Clear%20floats%20after%20the%20columns%20*%2F%0A.row%3Aafter%20%7B%0Acontent%3A%20%22%22%3B%0Adisplay%3A%20table%3B%0Aclear%3A%20both%3B%0A%7D%0A%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<style>" title="<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> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0Afunction%20popup()%0A%7B%0AmyWindow%20%3D%20window.open(%22%22%2C%20%22myWindow%22%2C%20%22width%3D400%2Cheight%3D200%22)%3B%0AmyWindow.document.write(%22%3Cwp-p%3EThis%20is%20a%20selenium%20popup%3C%2Fwp-p%3E%22)%3B%0A%7D%0A%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<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.