
๐What is Robot Class in Selenium WebDriver?
Robot Class in Selenium WebDriver: The Robot Class in Selenium WebDriver is a built-in class in Java AWT (Abstract Window Toolkit) used to simulate native system input events such as mouse movements, keyboard presses, and screen capturing. It is especially useful in scenarios where Selenium alone cannot interact with system-level pop-ups or OS windows.
The Robot Class enables automation of:
- Download/print pop-ups
- File upload dialogs
- Interactions with non-browser applications (e.g., Notepad, Calculator)
- Pressing keyboard shortcuts (Ctrl+C, Ctrl+V, etc.)
It was introduced in Java version 1.3 and can be easily integrated into any Selenium automation framework.
๐ Why Do We Use Robot Class in Selenium?
Robot Class in Selenium WebDriver: Selenium WebDriver is powerful for browser automation but has limitations when handling OS-level windows like:
- File Download dialogs
- Security pop-ups
- Native apps outside the browser
This is where the Robot Class comes into play. It: โ
Provides low-level input control over keyboard and mouse
โ
Helps simulate user interactions outside the DOM
โ
Complements Selenium by filling in the gaps for native OS controls
๐ Table of Contents
- What is Robot Class?
- Why Use Robot Class in Selenium?
- How to Access Robot Class Documentation
- Key Methods in Robot Class
- Real-World Use Cases
- Example Code in Java
- Summary
๐Robot Class Documentation (Java AWT)
Robot Class in Selenium WebDriver: The Robot class is part of the java.awt package and comes bundled with the JDK. You can explore the documentation on the or generate it locally using the steps below:
Steps to Generate Robot Class Documentation Locally:
- Go to your JDK installation folder and locate the src.zip file.
- Extract src.zip to any local directory (e.g., D:\JavaDocs).
- Navigate to: D:\JavaDocs\src\java\awt
- Open the Command Prompt in that folder.
Run the following command:
nginx
javadoc *.java
- After processing, you will see a series of .html files generated in the folder.
- Open index.html to access the complete documentation.
- Click on the Robot class in the navigation panel to view all its methods and usage details.
๐Key Methods in Robot Class
Here are some commonly used methods:
Method | Description |
mouseMove(x, y) | Moves the mouse to a specific screen coordinate |
mousePress(InputEvent.BUTTON1_DOWN_MASK) | Simulates mouse button press |
mouseRelease(InputEvent.BUTTON1_DOWN_MASK) | Simulates mouse button release |
keyPress(KeyEvent.VK_ENTER) | Simulates a key press |
keyRelease(KeyEvent.VK_ENTER) | Simulates a key release |
delay(ms) | Adds a delay in milliseconds |
createScreenCapture(Rectangle screenRect) | Captures a screenshot of a specific screen area |
๐ Real-World Use Cases for Robot Class
๐ธ Handling Windows authentication pop-ups
๐ธ Automating file uploads/downloads where <input type=”file”> is not available
๐ธ Simulating copy-paste (Ctrl+C / Ctrl+V) in web apps
๐ธ Testing desktop applications alongside browser apps
๐ธ Taking screenshots of native windows
๐ Robot Class Example in Selenium (Java)
java
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotDemo {
public static void main(String[] args) {
try {
// Create Robot class object
Robot robot = new Robot();
// Wait 2 seconds before starting
robot.delay(2000);
// Simulate Ctrl + S (Save)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
// Add delay
robot.delay(1000);
// Simulate Enter key (to confirm save)
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (Exception e) {
e.printStackTrace();
}
}
}
โ
This script will simulate Ctrl + S and Enter after a 2-second delay.
โ
Useful for automating browser download pop-ups or native save dialogs.
๐Summary
The Robot Class in Selenium WebDriver is a powerful utility for:
- Automating native system inputs
- Handling keyboard and mouse events
- Interacting with non-browser elements
It is essential for scenarios where Selenium WebDriver alone is insufficient, especially in dealing with OS-level interactions. While not suited for all types of testing, Robot Class is a valuable addition to any Java-based automation framework.
official Oracle Java Docs
Headless Browser Testing