Contents
Introduction
In Java, Robot is a class that belongs to the java.awt package. It also extends the Object class. The class is used to generate native system input events for test automation, self-running demos, and other applications where the control of the mouse and keyboard is required. In other words, we use the Java Robot class to trigger the input events, such as mouse click, keypress, etc. In this section, we will discuss the Java Robot class and its methods. Along with this, we will also create a Java program to control the keyboard and mouse. By using the Robot class we will create a Java program and control the keyboard and mouse.
The primary purpose of the Robot class is to facilitate automated testing of Java platform implementations. In short, the Robot class is used to control the peripherals (mouse and keyboard). It facilitates the three main functionalities, provides control over the mouse and keyboard, and captures the screen also.
Different Methods in Robot Class with Description
Method Name | Description |
createScreenCapture(Rectangle screenRect) | The method captures an image from the screen in a rectangular shape. |
delay(int ms) | It is used to sleep for a specified time. |
getAutoDelay() | It returns the number of milliseconds this robot sleeps after generating an event. |
getPixelColor(int x, int y) | It returns the color of the pixel of the specified coordinates. |
isAutoWaitForIdle() | The method returns whether this Robot automatically invokes the waitForIdle() after generating an event. |
keyPress(int keycode) | The method presses the specific key. |
Release(int keycode) | It releases the specified key. |
mouseMove(int x, int y) | It moves the mouse pointer over the specified coordinates. |
mousePress(int buttons) | It presses the mouse buttons. |
mouseRelease(int buttons) | The method releases the mouse buttons. |
mouseWheel(int wheelAmt) | It scrolls the mouse wheel. |
setAutoDelay(int ms) | It sets the number of milliseconds this Robot sleeps after generating an event. |
setAutoWaitForIdle(boolean isOn) | It sets whether this Robot automatically invokes the waitForIdle() method after generating an event. |
toString() | It returns a string representation of the Robot. |
toString() | It waits until all events currently on the event queue have been processed. |
Difference between Robot class and Actions class in Selenium
Similar to Robot Class, Actions Class also performs Keyboard and Mouse interactions.
But there are some differences between the two, that are listed below:
Robot Class | Actions Class |
Robot class uses the native system event to perform mouse and keyboard events | Actions class uses the WebDriver API to send the commands to the browser to perform the actions. |
Robot class enables selenium to perform all mouse actions including scroll | Actions class in selenium only simulates a mouse, it does not scroll mouse |
Robot class performs or controls the mouse and keyboard events using system native events | Actions class in selenium uses Webdriver API to send commands to the browser and perform operations (through JSON wire protocol) |
Robot class defined into java.awt package | Actions class is defined into openqa.selenium.interactions package |
Robot exists within the standard lib and doesn't require a browser driver | Actions class isn't bundled with the standard lib and requires a browser driver |
Program to Open Notepad and Type Content Using Keyboard Simulation
Let us see an example to use Keyboard actions to open Notepad and type content to it:
WriteinNotepad.java
package com.vas.keymouse;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;
public class WriteinNotepad {
// Java program to demonstrate working of Robot Class
public static void main(String[] args) throws IOException,
AWTException, InterruptedException
{
String command = "notepad.exe";
Runtime run = Runtime.getRuntime();
run.exec(command);
try {
Thread.sleep(2000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create an instance of Robot class
Robot robot = new Robot();
// Press keys using robot. A gap of
// of 500 milli seconds is added after
// every key press
robot.keyPress(KeyEvent.VK_CAPS_LOCK);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_H);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_F);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_R);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_M);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_N);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_U);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_M);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_Y);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_N);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_N);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_J);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_A);
Thread.sleep(500);
}
}
Example for Capturing Screenshot using Robot Class
The above code opens a blank Notepad file and
types "HELLO FROM NUMPYNINJA" onto it
with a delay of 500 ms before typing out each character.
The output is posted below:
Program to Capture Screenshot Using Keyboard and Mouse Simulation
Another important feature of Robot Class is Capturing Screenshot.
Let us see an example to capture screenshot with Robot class
Step 1: Create an Object for the respective browser using WebDriver
Step 2: Get the URL of the corresponding webpage and wait for the page to load completely
Step 3: Find the element on the web page using the element locators
Step 4: Enter the content using sendKeys() method
Step 5: To handle the pop-ups we use Robot class, using this we create an instance of Robot Class in the code say Robot robot = new Robot().
Step 6: Then, press Tab key 4 times and press Enter Key
Step 7: Get Screen Size of the screen using Rectangle class
Step 8: Capture ScreenShot of the page using BufferedImage
Step 9: Mention the path and name of the screenshot file
Step 10: Now, write the image captured in the file path
KeyBoardActions.java
package com.vas.keymouse;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class KeyBoardActions {
public static void main(String[] args) throws IOException,AWTException, InterruptedException {
Robot robot = new Robot();
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.name("q")).sendKeys("Google");
Thread.sleep(4000);
robot.mouseWheel(100);
robot.mouseMove(30,100) ;
robot.delay(100);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000);
System.out.println("Tab1");
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000);
System.out.println("Tab2");
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000);
System.out.println("Tab3");
robot.keyPress(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1000);
System.out.println("Tab4 and Enter");
Thread.sleep(2000);
//Get Screen Size
Rectangle size = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
//Capture ScreenShot
BufferedImage image = robot.createScreenCapture(new Rectangle(size));
//Store image into file
File path = new File("D://profile.jpg");
//Write image path
ImageIO.write(image,"JPG", path);
System.out.println("Screenshot captured and saved successfully");
driver.quit();
}
}
When we run the above java file, it navigates to Google.com and enters ”Google” in search box
Then the Tab action is performed four times as below:
Tab 1 - Moves to cross symbol
Tab 2- Moves to 'Search by Voice' tab
Tab 3: Moves to 'Search by image' Tab
Tab 4: Moves to 'Google Search' Tab
And Enter Key is pressed to click “Google Search” and the result is as below:
The screenshot is captured and saved in “D:/profile.jpg”
The console output is displayed as:
Advantages of Robot class
It provides control over the Keyboard as well as Mouse events.
It offers a way to handle an interaction with Operating system pop-ups support of which is not possible with Selenium Web Driver API.
Robot class is especially useful in managing file upload/download actions by interacting with OS pop-ups.
It is easy to consume in the Java Selenium scripts as this class is part of Java package
Capturing screenshots is also possible using Robot Class
Limitations of Robot class
Mouse or keyboard event will only work on the current instance of the window
It is difficult to switch among different frames or window
Some methods like MouseMove() depends on the screen resolution. So, there is possibility that code will not work on other machines.
While executing a robot event, if the code execution is moved to another window, the mouse or keyboard event will still remain on the previous window.
Conclusion
With this, we come to an end of our blog about file uploading using Robot Class. You can check my other blog on "File-upload-by-keyboard-mouse-actions-using-robot-class". Happy Coding!!!
Comments