Efficient Approaches to Manage Windows and Web-Based Popups/Notifications in Selenium WebDriver:
In our last guide, we went over the different types of waits that the WebDriver offers. Additionally, we also dived into the variety of navigation options that the WebDriver provides.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
In this Selenium WebDriver guide, we will emphasize how to control the different types of alerts that pop up during web applications testing and the techniques to manage them.
We will primarily concentrate on two types of alerts:
- Notification popups based on Windows
- Notification popups based on the Web
It’s important to note that WebDriver does not have the capability to manage windows based popups. Therefore, we will use third-party tools to manage these window popups.
One of the most difficult tasks when automating web applications is managing popups. This task becomes even more challenging due to the variety of types of popups.
What Exactly Are Notification/Popup/Confirmation Box/Prompt/Authentication Box?
A notification is a small box that appears on the display to provide information, alert about a possibly dangerous operation, or seek permission for an operation.
Example: Let’s use a real-life instance to better comprehend notifications. Suppose we posted a photograph on a well-known social networking site. Later, we decide to remove the uploaded photograph. When we click the delete button, the system cautions us about our action and asks, “Do you genuinely want to delete the file?” Now, we are given the choice to either accept or dismiss this notification.
Now, let’s see how we can manage these alerts based on their types, beginning with web-based notifications.
Notifications Based on Web
Let’s investigate how to control web-based alerts using WebDriver.
Managing web-based alert box
WebDriver provides an effective method for managing these alerts utilizing the Alert interface.
The Alert interface allows us to utilize four methods:
1) void dismiss() – The dismiss() method clicks the “Cancel” button when the alert window appears.
2) void accept() – The accept() method clicks the “OK” button when the alert window appears.
3) String getText() – The getText() method retrieves the text displayed on the alert box.
4) void sendKeys(String stringToSend) – The sendKeys() method inputs the specified string into the notification box.
Moving on, let’s analyze the real-world implementation.
Application under Test Explanation
We have developed a website that contains a variety of web elements. This is the same application we utilized while going over the Select class.
- Hyperlinks: There are two hyperlinks titled “Google” and “abodeQA” that redirect the user to “http://www.google.com/” and “http://www.abodeqa.com/” respectively once clicked.
- Dropdown: We have created three dropdowns for selecting colors, fruits, and animals with a predefined value set.
- Button: A “Try it” button is created that displays a popup box with OK and Cancel buttons when clicked.
(For an enlarged view, click on the image)
The following HTML code is used to design the webpage:
<html>
<head><title> Testing Select Class </title>
<body>
<div id="header">
<ul id="linkTabs">
<li>
<a href="https://www.google.com/">Google</a>
</li>
<li>
<a href="http://abodeqa.wordpress.com/">abodeQA</a>
</li>
</ul>
</div>
<div class="header_spacer"></div>
<div id="container">
<div id="content" style="padding-left: 185px;">
<table id="selectTable">
<tbody>
<tr>
<td>
<div>
<select id="SelectID_One">
<option value="redvalue">Red</option>
<option value="greenvalue">Green</option>
<option value="yellowvalue">Yellow</option>
<option value="greyvalue">Grey</option>
</select>
</div>
</td>
<td>
<div>
<select id="SelectID_Two">
<option value="applevalue">Apple</option>
<option value="orangevalue">Orange</option>
<option value="mangovalue">Mango</option>
<option value="limevalue">Lime</option>
</select>
</div>
</td>
<td>
<div>
<select id="SelectID_Three">
<option value="selectValue">Select</option>
<option value="elephantvalue">Elephant</option>
<option value="mousevalue">Mouse</option>
<option value="dogvalue">Dog</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
confirm("Press a button!");
}
</script>
</body>
</html>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Scenario to be Automated
- Open the webpage after launching the web browser
- Click on the “Try it” button
- Accept the alert
- Click the “Try it” button once again
- Dismiss the alert
WebDriver Code Utilizing Select Class
It’s worth noting that for building the script, we will use the “Learning_Selenium” project that we developed in the previous guide.
Step 1: Under the “Learning_Selenium” project, create a new Java class titled “DemoWebAlert”.
Step 2: In the “DemoWebAlert.java” class, copy and paste the following code.
Here is the test script that corresponds to the previously outlined scenario.
import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class DemoWebAlert { WebDriver driver; public DemoWebAlert() { } @Before public void setUp() { driver=new FirefoxDriver(); driver.get("file:///F:/Work/Selenium/Testing-Presentation/DemoWebPopup.htm"); driver.manage().window().maximize(); } @Test public void testWebAlert() throws InterruptedException { driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click(); Thread.sleep(5000); Alert alert = driver.switchTo().alert(); alert.accept(); driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click(); Thread.sleep(5000); driver.switchTo().alert().dismiss(); driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click(); Thread.sleep(5000); System.out.println(driver.switchTo().alert().getText()); driver.switchTo().alert().accept(); } @After public void tearDown() { driver.quit(); } }
Code Breakdown:
Import Statements:
import org.openqa.selenium.Alert; – Import this package before starting script creation to reference the Alert class, which is required for managing web-based alerts in WebDriver.
Alert class Object Creation:
Alert alert = driver.switchTo().alert();
Assign the alert to a reference variable for the Alert class.
Alert Switch:
driver.switchTo().alert();
Use this command to switch control to the recently generated popup window.
Accept the Alert:
alert.accept();
This command accepts the alert by clicking the OK button.
Dismiss the Alert:
alert.dismiss();
This command closes the alert by clicking the Cancel button and stops the operation from continuing.
Alerts/Popups Based on Windows
At times, during automation, we encounter situations where we have to handle popups that are created by windows, such as a print popup or a file-upload browse window.
Recommended reading: How to manage file upload in Selenium
Dealing with these kinds of popups can be tricky. Selenium is an automation testing tool that caters to web application testing exclusively. It does not support testing windows-based apps, and window alerts fall into that category. Nonetheless, we can overcome this limitation with the assistance of third-party tools.
Several third-party tools are available for managing window-based popups in conjunction with Selenium.
Now, let’s manage a window-based popup using the Robot class.
Keyboard and mouse actions can be emulated using the Robot class, a utility based on Java.
Before we proceed, let’s first discuss the application under test (AUT).
Application Under Test Explanation:
We will use “gmail.com” as the application under test. No explanation is needed for this application, as it is well-known.
The Following Scenario Should be Automated:
- Open “gmail.com” after launching the web browser
- Input a valid username and password
- Click the sign-in button
- Click the compose button
- Click the attach files icon
- Use the window-based popup to choose the files to be uploaded
WebDriver Code Utilizing Robot Class:
Please note that for building the script, we will use the “Learning_Selenium” project that was developed in the preceding guide.
Step 1: Under the “Learning_Selenium” project, create a new Java class named “DemoWindowAlert”.
Step 2: In the “DemoWindowAlert.java” class, copy and paste the following code.
Here is the test script that corresponds to the previously outlined scenario.
import java.awt.Robot; import java.awt.event.KeyEvent; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class DemoWindowAlert { WebDriver driver; @Before public void setUp() { driver=new FirefoxDriver(); driver.get("https://gmail.com"); driver.manage().window().maximize(); } @Test public void testWindowAlert() throws Exception{ driver.findElement(By.id("Email")).sendKeys("[email protected]"); driver.findElement(By.id("Passwd")).sendKeys("TestSelenium"); driver.findElement(By.id("signIn")).click(); Thread.sleep(30000); driver.findElement(By.xpath("//div[@class='z0']//div[contains(text(),'COMPOSE')]")).click(); driver.findElement(By.xpath("//div[contains(@command,'Files')]//div[contains(@class,'aaA')]")).click(); Robot rb =new Robot(); rb.keyPress(KeyEvent.VK_D); rb.keyRelease(KeyEvent.VK_D); Thread.sleep(2000); rb.keyPress(KeyEvent.VK_SHIFT); rb.keyPress(KeyEvent.VK_SEMICOLON); rb.keyRelease(KeyEvent.VK_SEMICOLON); rb.keyRelease(KeyEvent.VK_SHIFT); rb.keyPress(KeyEvent.VK_BACK_SLASH); rb.keyRelease(KeyEvent.VK_BACK_SLASH); Thread.sleep(2000); rb.keyPress(KeyEvent.VK_P); rb.keyRelease(KeyEvent.VK_P); rb.keyPress(KeyEvent.VK_I); rb.keyRelease(KeyEvent.VK_I); rb.keyPress(KeyEvent.VK_C); rb.keyRelease(KeyEvent.VK_C); Thread.sleep(2000); rb.keyPress(KeyEvent.VK_ENTER); rb.keyRelease(KeyEvent.VK_ENTER); Thread.sleep(2000); } @After public void tearDown() { driver.quit(); } }
Code Breakdown:
Import Statements:
import java.awt.Robot; – Import this package before beginning script creation to reference the Robot class in Java, which is needed to simulate keyboard and mouse events.
import java.awt.event.KeyEvent; – This package enables the user to utilize the keyPress and keyRelease events of a keyboard.
Creation of Robot class Object:
Robot rb = new Robot();
Instantiate a reference variable for the Robot class.
KeyPress and KeyRelease Events:
rb.keyPress(KeyEvent.VK_D);
rb.keyRelease(KeyEvent.VK_D);
Members pressing and releasing a specific key on the keyboard are simulated by the keyPress and keyRelease methods.
Final Thoughts
In this guide, we went over how to utilize the Alert class in WebDriver to handle web-based alerts/popups. Additionally, we discussed how the Robot class can be used to interact with window-based popups using keyboard events.
Key Takeaways:
- Alerts are tiny boxes that show up on the screen to provide information, give warnings about possibly harmful operations, or ask for permission for an operation.
- Two main types of alerts are:
- Alert popups that are based on Windows
- Alert popups that are based on the Web
- A package must be imported before developing a WebDriver script for handling dropdowns and making the Select class accessible.
- WebDriver offers an effective way to handle web-based alerts with the help of the Alert interface.
- void dismiss() – When the alert window appears, the dismiss() method clicks the “Cancel” button.
- void accept() – When the alert window appears, the accept() method clicks the “OK” button.
- String getText() – The getText() method retrieves the text shown on the alert box.
- void sendKeys(String stringToSend) – The sendKeys() method types the specified string into the alert box.
- Handling window-based popups can be difficult because Selenium only supports testing of web applications. Nonetheless, this limitation can be circumvented with the aid of third-party tools, such as the Robot class.
- The Robot class, a Java-based utility that can emulate keyboard and mouse actions, can be used effectively to manage window-based popups using keyboard events.
- The keyPress and keyRelease methods simulate a user pressing and releasing a specific key on the keyboard.
Subsequent Guide #17: In our next guide, we will explore other frequently used WebDriver commands, including exceptions handling and iframe handling. Additionally, we will investigate the get commands offered by WebDriver.
Through rapid examples, we plan to make these concepts easy to understand for the readers, enabling them to incorporate them into their daily scripting.
Note for the Readers: Stay tuned for the upcoming guide and automate web pages featuring both web-based and window-based alerts using WebDriver’s “Alert class” and Java’s “Robot Class”. We encourage you to express any inquiries or feedback about this guide or any previous ones in the comments section below.