Alerts/Popups in Selenium WebDriver
Different types of alerts available testing web applications There are two types of alerts
- Windows based alert popups
- Web-based alert popups
That handling window based popups are beyond WebDriver’s capabilities. Thus we exercise some third party utilities to handle window popups. Handling popup is challenging the piece of work to automate testing web applications. The diversity in types of popup complexes the situation even more.
What are Alert box/ Pop up box/ confirmation Box/ Prompt/ Authentication Box?
It is nothing but a small box that appears on the display screen some kind of information. To warn damaging operation or it may even ask you for the permissions for the operation.
Example:
We uploaded a photograph on any popular social networking sites. Later on, I wish to delete the uploaded photograph. So to delete, I clicked on the delete button. I click on the delete button, the system warns me against my action. Do you want to delete the file? So now we have an option to either accept this alert or reject it. So ahead of the session, let’s see how do we reject or accept the alerts depending on their types. Starting with the web-based popup.
Web Based Popups
Handling web based popup box
WebDriver offers the users to handle these popups using Alert interface. There are the four methods using along with the Alert interface.
1) void dismiss()
The dismiss() method clicks on the “Cancel” button as soon as the popup window appears.
2) void accept()
The accept() method clicks on the “Ok” button as soon as the popup window appears.
3) String getText()
The getText() method returns the text displayed on the alert box.
4) void sendKeys(String stringToSend)
The sendKeys() method enters the string pattern into the alert box.
Explanation of Application under Test
We have designed a web page in a way to include a few fundamental types of web elements. This is the same application introduce discussing Select class earlier in this series.
-
Hyperlink:
The two hyperlinks “Google” and “abodeQA”. It provides re-directs the user to “http://www.google.com/” and “http://www.abodeqa.com/”on the click event.
-
Dropdown:
The create for selecting colors, fruits, and animals with a value set to default.
-
Button:
“try it” buttons create to show up the pop-up box having OK and Cancel buttons upon click event.
Scenario to be automating
- Launch the web browser and open the web page
- Click on the “Try it” button
- Accept the alert
- Click on the “Try it” button again
- Reject the alert
Code Walk-through
Import Statements
Import org.openqa.selenium.Alert
The Import this package the script creation the package References to the Alert class. Which need to handle the web-based alerts in WebDriver.
Object Creation for Alert class
Alert alert = driver.switchTo().alert(); We create a reference variable for Alert class and references it to the alert.
Switch to Alert
Driver.switchTo().alert(); The above command used to switch the control to the recently generated popup window.
Accept the Alert
alert.accept(); The above command accepts the alert by clicking on the Ok button.
Reject the Alert
alert.dismiss(); The above command closes the alert thereby clicking on the Cancel button. The operation should not proceed.
Window Based Pop Ups
We need to handle popups generated by windows. like a print popup or a browsing window while uploading a file. These popups have always been a little Selenium is an automation testing tool. They support only web application testing. That means, it doesn’t support windows based applications and window alert is one of them. But it alone can’t help the situation along with third party problem can overcome. There are several third party tools available for handle window based popups.
So now let’s handle a window based pop up using Robot class.
Robot class is a java based utility which emulates the keyboard and mouse actions. Before moving ahead, let us take a moment to have a look at the application under test.
Explanation of Application under Test
As an application under test, we would be using “gmail.com”. The application doesn’t need any more introductions.
Scenario to be automating
- Launch the web browser and open the application – “gmail.com
- Enter valid username and password
- Click on the sign in button
- To Click on the compose button
- A Click on the attach icon
- Select the files to upload with the window based pop up.
Code Walk-through
Import Statements
import java.awt.Robot
The Import this package before the script creation. The package references to the Robot class in java. We simulate keyboard and mouse events.
import java.awt.event.KeyEvent
The package allows the user to use keyPress and keyRelease events of the keyboard.
Object Creation for Robot class
Robot rb =new Robot(); We create a reference variable for Robot class and instantiate it.
KeyPress and KeyRelease Events
rb.keyPress(KeyEvent.VK_D); rb.keyRelease(KeyEvent.VK_D); The keyPress and keyRelease methods to the certain key on the keyboard.
Best Selenium Training Center in Hyderabad
Best Selenium Training Center in Hyderabad: We provide professional Best Selenium Training Center in Hyderabad.