Robot Class can simulate Keyboard and Mouse Event
Robot Class can help with upload/download of files when using a selenium web driver
Robot Class can easily be integrated with the current automation framework (keyword, data- driven or hybrid)
package com.javabykiran;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Robot_Class_Example {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.javabykiran.com/");
Robot robot = new Robot();
// move mouse point to specific location
robot.mouseMove(630, 420);
// delay is to make code wait for mentioned milliseconds before executing next step
robot.delay(1500);
//press left click
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
//release left click
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(1500);
//press keyboard arrow key to select Save radio button
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(2000);
// press enter key of keyboard to perform above selected action
robot.keyPress(KeyEvent.VK_ENTER);
}
}
While automating a web based application we will come across many specific scenarios where any selenium command is launched but actual event is not fired.
This causes termination of execution of test and it reports some errors. Here Java Robot Class comes into the picture.
Using Robot Class we can simulate keyboard and mouse events in Selenium. This class is very easy to use with automation process. It can be easily integrated with the current automation framework.
Let’s See an example of GMAIL where we have to automate below test cases:
Step 1) Open URL Gmail
Step 2) Enter Valid Login Credentials
Step 3) Verify the Gmail has Logged in Successfully
Step 4) Click on Compose
Step 5) Enter to Address in Compose mail
Step 6) Enter Subject in Compose Mail
Step 7) Click on Attachment link
Step 8) Attach any file (JPG or doc) from your local machine.
Step 9) click on Send
Step 10) Verify for the success message
Now in the above test cases we can automate the entire ‘Test’ step with selenium web driver except ‘Attaching’ a file. Since selenium can only recognize web based application we have to use a separate tool like AutoIT to choose a file from local machine to upload.
Here ‘Java Robot’ Class also comes in picture. Using ‘Robot Class’ we can simulate keyboard and mouse events in Selenium.
Robot class is used to (generate native system input events) take the control of mouse and keyboard. Once we get the control, we can do any type of operation related to mouse and keyboard through Java code.
‘Robot Class’ is available under ‘java.awt.package’.
The packages that we need to import while working in Robot class are as follows:
import java.awt.AWTException;
import java.awt.Robot;
The very first step should be creating an Object of robot class.
Robot robot=new Robot ();
Methods in Robot Class can be effectively used to make the interaction with popups in Web Applications.
Selenium does not provide support to handle browser pop-ups or the native operating system pop-ups. To handle these kind of pop-up, take the help of Robot Class.
This is also used while we need to handle file upload and download activity using selenium web Driver.
There are different Methods used in robot class.
Some of the popular methods are:
.keyPress();
.mousePress();
.mouseMove();
.keyRelease();
.mouseRelease();
.keyPress()
robot.keyPress(KeyEvent.VK_ESC);
This will press Escape key on the keyboard.
.mousePress()
robot.mousePress(InputEvent.BUTTON1_MASK);
This will press Left mouse button.
.mouseMove()
robot.mouseMove(coordinates.getX(), coordinates.getY());
This will move the mouse pointer to X and Y co-ordinates.
.keyRelease()
robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
This will release the CAPS_LOCK key.
.mouseRelease()
robot.mouseRelease(InputEvent.BUTTON1_MASK);
This will release Left mouse button.
Note: keyPress will send an event that a key has been pressed down. For Releasing the press effect on the key if we have pressed a key using robot.keyPress(KeyEvent.VK_CONTROL)
, then we should Release it too. Otherwise once our Java application runs, keyboard will continue with virtually pressed CTRL key.
Selenium cannot handle file downloading because browsers use native dialogs for downloading files. Sometime we need to download file from AUT (Application under Test). There are several ways to automate download file in Selenium but here we see download file using AutoIT in Selenium WebDriver.
Follow the below steps:
Download AutoIT tool
package com.javabykiran;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GmailAttachExample {
static WebDriver driver;
public static void main(String args[]) throws AWTException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\JAVABYKIRAN\\Desktop\\chromedriver.exe");
driver = new ChromeDriver();
String BaseUrl = "https://www.gmail.com";
driver.get(BaseUrl);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.id("Email")).sendKeys("javabykiran@gmail.com ");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("kiranselenium");
driver.findElement(By.id("signIn")).click();
Set< String> allwindows = driver.getWindowHandles();
System.out.println(allwindows);
driver.findElement(By.xpath("//div[@class='T-I J-J5-JiT-I-KE L3']")).click();
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
}
driver.findElement(By.id(":ne")).sendKeys("Enter receipients Email Id");
System.out.println(driver.findElement(By.id(":ne")). getAttribute("value"));
driver.findElement(By.xpath("//input[@name='subject- box']")).sendKeys("Test Selenium");
driver.findElement(By.xpath("//div[@id=':o1']")).sendKeys("Hi Test" + "I am sending a Testing Mail"
+ "Thanks" + "JBK ");
driver.findElement(By.id(":or")).click();
// Using Robot class in the below code to attach a file in compose Email
StringSelection filePath = new StringSelection("C:\\ Users\\JAVABYKIRAN\\Pictures\\city.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().set-Contents(filePath, null);
Robot rb = new Robot();
Thread.sleep(5000);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyRelease(KeyEvent.VK_V);
Thread.sleep(6000);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(10000);
driver.findElement(By.xpath("//div[@id=':mp']")).
click();
driver.close();
}
}
Now install both whatever we have downloaded.
Open Programs → AutoIT tool → SciTE Script Editor and add the below mentioned AutoIT script in AutoIT editor and save it as ‘DownloadFile.au3’ in your system
Explanation for above
Line 1 : WinWait(“[CLASS:#MozillaDialogClass]”,””,8)
Wait for 8 seconds to appear download and save dialog. Used class property of download dialog.
MozillaDialogClass – we brought this from finder tool.
How to open finder tool.
Drag highlighted box to download window. And OK button you will see this MozillaDialogClass class.
Line 2 : Send(“!s”)
Perform keyboard ALT key down + s + ALT key up action to select Save File Radio button using keyboard shortcut.
Line 3 : Sleep(9000)
Wait for 9 seconds
Line 4: Send(“{ENTER}”)
After that it downloads the document
Once the file is saved, we need to convert the ‘DownloadFile.au3’ to ‘DownloadFile.exe’. To do this we need to compile the ‘DownloadFile.au3’, right click and select option as below.
In Eclipse, add the below mentioned Selenium Script and run. Write below program to download syllabus of selenium from javabykiran.com.
Project structure looks like this.
package com.jbk;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FileDownloadAutoIt {
public static void main (String [] args) throws Exception{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//To open URL
driver.get("http://www.javabykiran.com/sample-web-page-to-test.html");
//Download Text File
driver.findElement(By.xpath(“XPATH OF DOWNLOAD LINK”)).click();
//To call the AutoIT script
Runtime.getRuntime().exec("E:\\batch\\selNote\\AutoIt\\src\\DownloadFile.exe");
//'close' method is used to close the browser window
//driver.close();
}
}