How to handle Window Based Pop Ups in Selenium Webdriver
At times while
automating, we get some scenarios, where we need to handle pop ups generated by
windows like a print pop up or a browsing window while uploading a file.
Handling
these pop-ups have always been a little tricky as we know Selenium is an
automation testing tool which supports only web application testing, that
means, it doesn’t support windows based applications and window alert is one of
them. However Selenium alone can’t help the situation but along with some third
party intervention, this problem can be overcome.
There are
several third party tools available for handling window based pop-ups along
with the selenium.
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
(AUT).
Explanation
of Application under Test
As an
application under test, we would be using “gmail.com”. I believe the
application doesn’t require any more introductions.
Scenario to be automated
Launch the
web browser and open the application – “gmail.com”
Enter valid
username and password
Click on the
sign in button
Click on the
a compose button
Click on the
attach icon
Select the
files to be uploaded with the window based pop up.
WebDriver
Code using Robot 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 “DemoWindowAlert” under the
“Learning_Selenium” project.
Step 2: Copy
and paste the below code in the “DemoWindowAlert.java” class.
Below is the
test script that is equivalent to the above mentioned scenario.
import
java.awt.Robot;</pre>
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{
// enter a
valid email address
driver.findElement(By.id("Email")).sendKeys("xxxxxxxxxxxxxx@gmail.com");
// enter a
valid password
driver.findElement(By.id("Passwd")).sendKeys("xxxxxxxxx");
// click on
sign in button
driver.findElement(By.id("signIn")).click();
Thread.sleep(30000);
// click on
compose button
driver.findElement(By.xpath("//div[@class='z0']//div[contains(text(),'COMPOSE')]")).click();
// click on
attach files icon
driver.findElement(By.xpath("//div[contains(@command,'Files')]//div[contains(@class,'aaA')]")).click();
// creating
instance of Robot class (A java based utility)
Robot rb
=new Robot();
// pressing
keys with the help of keyPress and keyRelease events
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();
}
}
Import Statements
import
java.awt.Robot – Import this package prior to the script creation The package
references to the Robot class in java which is required simulate keyboard and
mouse events.
import
java.awt.event.KeyEvent – The package allows the user to use keyPress and
keyRelease events of 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 simulate the user pressing and releasing a certain key
on the keyboard respectively.
Comments
Post a Comment