Share

Why we use Robot Class in Automation Testing?

Author: neptune | 23rd-Aug-2022 | views: 91
#Selenium

Introduction to Robot Class?

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.


Need for Robot Class?

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.


Does Robot Class belong to Selenium?

No, the Robot class doesn’t belong to Selenium. It belongs to the AWT package of JDK (java.awt.Robot).

Use of Robot Class with Example

Scenario : The user wants to upload the file from some path on the computer.

1. Click on Upload/Choose file.

Inspect the upload button locator and click.

driver.findElement(By.xpath(“//*[@id=’uploadfile’]”).click();

2. Copy your file's absolute path to the clipboard.

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);

3. Paste the path in the file name of the upload dialog box.

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);

Methods in Robot Class.

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)

Advantages and limitations of Robot Class.

Advantages:

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.

Conclusion

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.





anonymous | Aug. 13, 2022, 4:15 p.m.

Add more such tutorial's



Related Blogs
Automate Ticket Booking in SpiceJet from Delhi to Bengaluru using Selenium and Cucumber.
Author: neptune | 17th-May-2022 | views: 4369
#Selenium #Cucumber
We are going to automate a ticket booking using Selenium WebDriver and Cucumber BDD...
Selenium, Cucumber, JUnit, TestNG dependencies for Selenium project.
Author: neptune | 25th-May-2022 | views: 1576
#Selenium #Testing #Cucumber #Projects
We guide you how to update the pom.xml file for Selenium Maven project...
How to use wait commands in Selenium WebDriver in Java ?
Author: neptune | 22nd-Feb-2022 | views: 889
#Selenium #Testing #Java
We are going to explore different types of waits in Selenium WebDriver. Implicit wait, Explicit wait, and Fluent wait with examples...
Top 50+ Selenium Interviews Questions 2022 based on Years of Experience
Author: neptune | 31st-May-2022 | views: 825
#Selenium #Testing #Interview
Every interview difficulty is based on how many years of experience you have in that field. For the Selenium Automation Tester I have divided the question on the number of years of experience...
Top 10 Selenium Interview Questions with answers (2021).
Author: neptune | 23rd-Feb-2022 | views: 724
#Selenium
In this article I will cover top 10 Selenium interview questions...
Mostly asked Cucumber interview questions in selenium automation interviews.
Author: neptune | 28th-May-2022 | views: 632
#Selenium #Testing #Cucumber
We are going to explore widely asked Cucumber interview questions in selenium automation interviews...
How to create Selenium Cucumber Project in Eclipse.
Author: neptune | 25th-May-2022 | views: 362
#Selenium #Cucumber
First, divide the process into the various steps to understand working in brief of a project. Steps in brief: We’ll start with initializing the browser driver and then log in to the web page...
Getting started with Selenium WebDriver.
Author: neptune | 15th-Apr-2022 | views: 263
#Selenium #Testing
In this blog I will give you an overview of Selenium WebDriver and Also discuss about advantages and disadvantages of Selenium WebDriver...
How to create a Selenium, Cucumber Automation project in Eclipse ?
Author: neptune | 19th-Apr-2022 | views: 244
#Selenium #Testing #Cucumber
Problem Statement : Let’s consider this particular project where we will try automating the process of booking a flight ticket. Let’s get started and see how it’s done using Selenium...
Top 10 Selenium Interview Questions.
Author: neptune | 03rd-Jun-2022 | views: 155
#Selenium #Interview
Locator is a command that tells Selenium IDE which GUI elements (like Text Box, Buttons, Check Boxes etc) we are going to use or perform some automation task...
View More