In this article, will discuss Alert popup boxes in web applications using Selenium. Generally, in a web page, we use popup boxes for various reasons.
What is a Popup box?
It is a small message/dialog box that appears on the top of a web application and it provides some information/warning to the user. Sometimes, it also expects input information from the user.
In web applications, we have three kinds of popup boxes,
- Alert box
- Confirm box
- Prompt box
Alert box
Alert box looks just like below.
An alert box is used when an application wants to display some information to the user. The user has to click on the OK button when the alert box shows up to proceed further.
Confirm box
Confirmation popups look just like below.
A confirm box is used when an application wants to get confirmation from the user. The user has to click on either Yes/Ok button or No/Cancel button when the confirm box shows up to proceed further.
Prompt box
Prompt popups look just like below.
A prompt box is used when an application expects input from the user immediately after opening it. The user has to click on either Yes/Ok button or No/Cancel button after entering the input when the confirm box shows up to proceed further.
From developer tools, we can’t inspect any button or textbox present on any type of these popup boxes. Don’t panic, because Selenium provides a way to handle these three type of popup boxes. But you may have a question in your mind; why do we need to handle these alerts?
The answer to this question is “these popup boxes will appear on top of the user’s screen and it blocks the screen. It means the user can’t perform any operation on the screen until the user handles these popup boxes.”
So it is mandatory that we need to handle these popup boxes.
The operations that we can perform on these popups using selenium are,
- Accept
- Dismiss
- SendKeys
- GetText
Accept refers to clicking on the Ok button.
Dismiss refers to clicking on the Cancel button.
Sendkeys refers to entering text into a textbox available in the prompt box.
GetText refers to get the label text present on the popup box.
Here I’m using the below Website for demonstrating the examples on popup boxes.
https://www.hyrtutorials.com/p/alertsdemo.html
The webpage looks some similar to the following:
The output of the popup box will be displayed below the last field; i.e., Popup box output.
Below is the selenium script for handling popup boxes.
Selenium Script
- String alertBoxOutput, popupText;
- System.setProperty("webdriver.chrome.driver", "./Resources/chromedriver.exe");
- WebDriver driver = new ChromeDriver();
- driver.manage().window().maximize();
- driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
- driver.get("https://www.hyrtutorials.com/p/alertsdemo.html");
- Thread.sleep(2000);
-
-
- driver.findElement(By.id("alertBox")).click();
- popupText = driver.switchTo().alert().getText();
- Assert.assertEquals(popupText, "I am an alert box!");
- driver.switchTo().alert().accept();
- alertBoxOutput = driver.findElement(By.id("output")).getText();
- Assert.assertEquals(alertBoxOutput, "You selected alert box");
-
-
- driver.findElement(By.id("confirmBox")).click();
- popupText = driver.switchTo().alert().getText();
- Assert.assertEquals(popupText, "Press a button!");
- driver.switchTo().alert().accept();
- alertBoxOutput = driver.findElement(By.id("output")).getText();
- Assert.assertEquals(alertBoxOutput, "You pressed OK in confirm box");
- driver.findElement(By.id("confirmBox")).click();
- driver.switchTo().alert().dismiss();
- alertBoxOutput = driver.findElement(By.id("output")).getText();
- Assert.assertEquals(alertBoxOutput, "You pressed Cancel in confirm box");
-
-
- driver.findElement(By.id("promptBox")).click();
- popupText = driver.switchTo().alert().getText();
- Assert.assertEquals(popupText, "Please enter your name:");
- driver.switchTo().alert().sendKeys("Reddy");
- driver.switchTo().alert().accept();
- alertBoxOutput = driver.findElement(By.id("output")).getText();
- Assert.assertEquals(alertBoxOutput, "You entered text Reddy in prompt box");
- driver.findElement(By.id("promptBox")).click();
- driver.switchTo().alert().dismiss();
- alertBoxOutput = driver.findElement(By.id("output")).getText();
- Assert.assertEquals(alertBoxOutput, "You pressed Cancel in prompt box.");
-
- driver.quit();
If you are interested in watching this full information explained in a video, please watch this video
I hope you find this article useful. Please provide your valuable feedback, questions, or comments about this article.