Robot class is used to generate native system input events for the purpose of automation testing. It is used to perform mouse and keyboard events on windows applications or popups.
In test automation when the Actions class can’t handle the windows popups that require modifier keys like Alt, Shift, etc. then the Robot class comes into the picture.
For Example: When a user wants to upload a file during testing.
Then it will open the windows popup to select the file and click on Open.
Since Selenium can’t perform any actions on such pop ups then the Robot class comes into the picture.
No, the Robot class doesn’t belong to Selenium. It belongs to the AWT package of JDK (java.awt.Robot).
Scenario : The user wants to upload the file from some path on the computer.
Inspect the upload button locator and click.
driver.findElement(By.xpath(“//*[@id=’uploadfile’]”).click();
Create a string of file paths to locate the file and set it to the clipboard using Toolkit.
StringSelection pathString = new StringSelection(“C:Users/documents/test.doc”);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
We will use the keystrokes to paste the path and submit it.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
1. keyPress(): keyPress is used to press the keyboard keys.
Example: Method to press down arrow key of Keyboard.
robot.keyPress(KeyEvent.VK_DOWN)
2. mousePress(): mousePress is used to press mouse buttons.
Example: This method will press the right click of your mouse.
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK)
3. mouseMove(): mouseMove is used to move the mouse pointer to a specific location.
Example: This will move the mouse pointer to the specified X and Y coordinates.
robot.mouseMove(point.getX(), point.getY())
4. keyRelease(): keyRelsease used to release the keyboard key.
Example: This method with release down arrow key of Keyboard robot.keyRelease(KeyEvent.VK_DOWN)
5. mouseRelease(): mouseRelease is used to release mouse buttons.
Example: This method will release the right click of your mouse
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK)
1. It provides control over the Keyboard as well as Mouse events.
2. It helps to handle Windows pop-ups, which is not possible with Selenium Web Driver API.
3. Robot class is especially useful in managing file upload/download actions by interacting with OS pop-ups.
4. It is easy to consume in the Java Selenium scripts as this class is part of the Java package.
Limitations:
Robot class methods have following limitations while writing automation scripts:
1. Most of the methods like mouseMove are dependent on screen resolution, it may perform differently on different screens.
2. Robot class acts on windows in focus, so the behaviour differs when multiple windows open.
3. Switching between different frames or windows is difficult with Robot methods.
To conclude, the purpose of this tutorial is to give an introduction to the Robot class. If you’re still thirsty for more information, have questions, or simply would like to share your thoughts, then feel free to reach out in the comment section below.