Monday, 8 October 2018

Handling Popups/Alerts in WebDriver - Examples to Practice

Handling Popups/Alerts
==================

Alerts are also called as Popups/Confirmation Box/Prompt/Authentication Box
There are two types of alerts:
1. Web based alerts/popups
2. Windows based alerts/popups

1. Handling web based popups
-------------------------------------
There are 2 types of web based popups
a. HTML popups
b. Java popups

a. HTML popups:
which are developed in HTML
To handle these type of popups widnow webelements, we can script directly
to perform operation


Exp: Write script to select "6E Senior citizen" checkbox and to close
html popup(url: https://goindigo.in)

Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://goindigo.in");
driver.manage().window().maximize();

//to select "6E Senior citizen"
driver.findElement(By.xpath("//*[@id='oneWay']/form/div[1]/div/div/ul/li[3]/div/label")).click();

Thread.sleep(3000);
//to click on "OK" on popup window
driver.findElement(By.xpath("//*[@id='globalModal']/div/div/div[3]/button")).click();


b. handling Java popups:
if firebug is not showing any html code then it is considered as Java popup
To handle Java popups we use "Alert" class

Syntax: {to create instance object for Alert class}
Alert alt= driver.switchTo().alert();

We can use following commands to work on Alerts
i. alt.accept();
to accept alert(i.e. to click on "OK")
ii. alt.getText();
to read alert message
iii. alt.dismiss();
it will dismiss the alert (i.e. to click on "Cancel")
iv. alt.sendkeys(value);
it will enter text into prompt/edit box on popup window


Exp: Write script for following scenario:
 
Open application (https://secure.aponline.gov.in)
Enter user id
Click on "Enter" button
Read msg from popup window
Close popup window
Enter pwd

Script:
//open application (https://secure.aponline.gov.in)
WebDriver driver= new FirefoxDriver();
driver.get("https://secure.aponline.gov.in");
driver.manage().window().maximize();
//enter user id
driver.findElement(By.id("userId")).sendKeys("Madhukar");
//click on "Enter" button
driver.findElement(By.id("ImageButton1")).click();

Thread.sleep(5000);

//create object for Java alert
Alert alt= driver.switchTo().alert();
//read msg from popup window

String eMsg=alt.getText();
System.out.println(eMsg);
//close popup window
alt.accept();
//enter pwd
driver.findElement(By.id("password")).sendKeys("mercury");



2. Handling windows based alerts/popups:{uploading file}
Selenium does not support windows based popups directly

Exp:  Print popup window or browsing a file while uploading a file.
To handle windows based pop-ups there are several 3rd
party tools available eg., AutoIT, sikuli...etc
There is also a java class caleed Robot Class to handle a windows based popups

Robot Class:
=========
**Robot class is a java based utility which emulates the
keyboard and mouse actions irrespective of the environment of object

Syntax: {to create instance object for Robot class}
Robot rb= new Robot();

Exp:  Create script to upload a file on "https://www.sendfiles.net/" using Robot class

Script: WebDriver driver= new FirefoxDriver();
driver.get("https://www.sendfiles.net/");
driver.manage().window().maximize();

//to click on "START NOW"
driver.findElement(By.linkText("START NOW")).click();
//to click on "Add Files"
driver.findElement(By.xpath("//span[text()='Add Files']")).click();

Thread.sleep(5000);

//to assign file path
StringSelection myFile= new StringSelection("E:\\sam.doc");
//to set vaue into system clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(myFile, null);

//create object for Robot class
Robot rb= new Robot();
//to perform Ctrl+V
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);

rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);

Thread.sleep(3000);

//to perform TAB
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);

Thread.sleep(3000);

//to perform TAB
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);

Thread.sleep(3000);

//to perform TAB
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);


OR

WebDriver driver = new FirefoxDriver();
driver.get("https://www.sendfiles.net/");

driver.findElement(By.linkText("START NOW")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='uploader_browse']/span[2]")).click();
Thread.sleep(3000);
StringSelection myFile= new StringSelection("E:\\Madhukar.docx");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(myFile, null);

Robot rb= new Robot();
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(5000);
rb.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1000);
rb.keyRelease(KeyEvent.VK_ENTER);


Exp: Write script to automate follwoing Scenario

Open application “https://www.gmail.com”
Enter valid Email (seleniumuser15)
Click on "Next"
Enter valid password (Mercury9)
Click on "compose" button
Enter "To" mail id
Enter subject as "Upload File"
Click on the "attachment" icon
Enter the file path in popup window
Click on "enter" button in keyboard using Robot Class to upload a file
Click on "Send" button
Click on Account name icon
Click on "Signout"


Script:

// initialize browser and open gmail
WebDriver driver= new FirefoxDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//to perform login operation
driver.findElement(By.id("identifierId")).sendKeys("seleniumuser15");
driver.findElement(By.xpath("//*[text()='Next']")).click();

driver.findElement(By.name("password")).sendKeys("Mercury9");
driver.findElement(By.xpath("//*[text()='Next']")).click();

Thread.sleep(5000);
String pgTitle= driver.getTitle();
if (pgTitle.contains("seleniumuser15")){
System.out.println("Successful login operation");
}
else{
System.out.println("Unsuccessful login operation");
}
//compose mail
driver.findElement(By.xpath("//*[text()='COMPOSE']")).click();
Thread.sleep(2000);
//-enter "To" mail id
driver.findElement(By.name("to")).sendKeys("Selenium1234@yopmail.com");
//-enter subject
Thread.sleep(2000);
driver.findElement(By.name("subjectbox")).sendKeys("Uploading file");
//-attach file
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@class='a1 aaA aMZ']")).click();
Thread.sleep(4000);

//to select path of file
StringSelection myFile= new StringSelection("E:\\sam.doc");
//to set path of file into system clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(myFile, null);

//create instance object for robot class
Robot rb= new Robot();

//to press Ctrl+V
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);

rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);

Thread.sleep(3000);
//to operate TAB
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);

Thread.sleep(3000);
//to operate TAB
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(3000);

//to click on Enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);

Thread.sleep(5000);
//-send mail
  driver.findElement(By.xpath("//*[text()='Send']")).click();
//signout
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@class='gb_7a gbii']")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("Sign out")).click();



AutoIT:
======
AutoIT v3 is also freeware. It is used to perform mouse movements,
keystrokes on windows based controls
to upload/download a file using selenium webdriver we need three tools:
1. Selenium Webdriver
2. Element identifier (AutoIT window info)
3. AutoIT editor(sciTE-lite)


Download and install AutoIT:
Navigation:
Enter url: "https://www.autoitscript.com/site/autoit/downloads"
Click on 'AutoIT' Downloads option 
Download "AutoIT" by clicking on 'Download AutoIT' button
Install AutoIT (i.e. RUN)
click on "Download Editor"
Run the Downloaded file


Exp: Create script to upload file using AutoIT on www.sendfiles.net page

Procedure:
Step 1: Create script in Eclipse to open application and click to upload file
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.sendfiles.net/");
driver.manage().window().maximize();

driver.findElement(By.linkText("START NOW")).click();
driver.findElement(By.xpath("//*[text()='Add Files']")).click();


Step 2: Create script in AutoIT Editor
Script:
;to wait until popup window to activate
WinWaitActive("File Upload")
;to enter file path
ControlSetText("File Upload","","Edit1","E:\Sample.docx")
;to pause execution
Sleep(2000)
;to click on open
ControlClick("File Upload","","Button1")


Step 3: Save autoIT Editor script and compile
Navigation:
select AutoIT file (.au3)
right click on mouse
select "compile (64/86)"

Step 4: Run AutoIT complied file (.exe file) AutoIT script  file by calling into Eclipse
Syntax:
Runtime.getRuntime().exec(path of .exe file);


========================================================================
Script in Eclipse:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.sendfiles.net/");
driver.manage().window().maximize();

driver.findElement(By.linkText("START NOW")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='uploader_browse']/span[2]")).click();
Thread.sleep(2000);
Runtime.getRuntime().exec("E:\\UploadFileEx.exe");



No comments:

Post a Comment