How to Handle Alerts/Popups in Selenium WebDriver
There are
two types of alerts that we would be focusing on majorly:
Windows
based alert pop ups
Web based
alert pop ups
As we know
that handling windows based pop ups is beyond WebDriver’s capabilities, thus we
would exercise some third party utilities to handle window pop ups.
Handling pop
up is one of the most challenging piece of work to automate while testing web
applications. Owing to the diversity in types of pop ups complexes the
situation even more.
What is
Alert box/ Pop up box/ confirmation Box/ Prompt/ Authentication Box?
It is
nothing but a small box that appears on the display screen to give you some
kind of information or to warn you about a potentially damaging operation or it
may even ask you for the permissions for the operation.
Example: Let us consider a real life
example for a better understanding; Let us assume that we uploaded a photograph
on any of these popular social networking sites. Later on, i wish to delete the
uploaded photograph. So in order to delete, i clicked on the delete button. As
soon as I click on the delete button, the system warns me against my action,
prompting – Do you really want to delete the file? So now we have an option to
either accept this alert or reject it.
So ahead in
the session, let’s see how do we reject or accept the alerts depending on their
types. Starting with the web based pop ups.
Let us see
how do we handle them using WebDriver.
Handling web
based pop-up box
WebDriver
offers the users with a very efficient way to handle these pop ups using Alert
interface.
There are
the four methods that we would be using along with the Alert interface.
1) void
dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the
pop up window appears.
2) void
accept() – The accept() method clicks on the “Ok” button as soon as the pop up
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 specified
string pattern into the alert box.
Let us move
ahead and look at the actual implementation.
Scenario to be automated
Launch the
web browser and open the webpage
Click on the
“Try it” button
Accept the
alert
Click on the
“Try it” button again
Reject the
alert
WebDriver
Code using Select Class
Please take
a note that for script creation, we would be using “Learning_Selenium” project
created in the former tutorial.
Step 1: Create
a new java class named as “DemoWebAlert” under the “Learning_Selenium” project.
Step 2: Copy
and paste the below code in the “DemoWebAlert.java” class.
Below is the
test script that is equivalent to the above mentioned 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;
/**
* class
description
*/
public class
DemoWebAlert {
WebDriver driver;
/**
* Constructor
*/
public DemoWebAlert() {
}
/**
* Set up browser settings and
open the application
*/
@Before
public void setUp() {
driver=new
FirefoxDriver();
// Opened the
application
driver.get("file:///F:/Work/Selenium/Testing-Presentation/DemoWebPopup.htm");
driver.manage().window().maximize();
}
/**
* Test to check Select functionality
* @throws InterruptedException
*/
@Test
public void testWebAlert()
throws InterruptedException {
// clicking on
try it button
driver.findElement(By.xpath("//button[contains(text(),'Try
it')]")).click();
Thread.sleep(5000);
// accepting
javascript alert
Alert alert =
driver.switchTo().alert();
alert.accept();
// clicking on
try it button
driver.findElement(By.xpath("//button[contains(text(),'Try
it')]")).click();
Thread.sleep(5000);
// accepting
javascript alert
driver.switchTo().alert().dismiss();
// clicking on
try it button
driver.findElement(By.xpath("//button[contains(text(),'Try
it')]")).click();
Thread.sleep(5000);
// accepting
javascript alert
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
}
/**
* Tear down the setup after
test completes
*/
@After
public void tearDown() {
driver.quit();
}
}
Import Statements
Import
org.openqa.selenium.Alert – Import this package prior to the script creation
The package references to the Alert class which is required 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 is used to switch the control to the recently generated pop up window.
Accept the
Alert
alert.accept();
The above
command accepts the alert thereby clicking on the Ok button.
Reject the
Alert
alert.dismiss();
The above
command closes the alert thereby clicking on the Cancel button and hence the
operation should not proceed.
Comments
Post a Comment