Why we use Robot Class in Automation Testing?

Author: neptune | 23rd-Aug-2022
#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 | Nov. 2, 2022, 10:23 a.m.

Very well explained 👍


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 | 08th-Aug-2023
#Selenium
We are going to automate a ticket booking using Selenium WebDriver and Cucumber BDD...

Selenium, Cucumber, JUnit, TestNG dependencies for Selenium project.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
We guide you how to update the pom.xml file for Selenium Maven project...

5 Selenium Project Ideas & for Beginners in Automation Testing
Author: neptune | 30th-Mar-2023
#Selenium #Testing #Projects
In this article, we will discuss 5 interesting Selenium project ideas for beginners in automation testing...

How to use wait commands in Selenium WebDriver in Java ?
Author: neptune | 22nd-Feb-2022
#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 2023 based on Years of Experience
Author: neptune | 02nd-Apr-2023
#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 | 02nd-Apr-2023
#Selenium #Interview
In this article I will cover top 10 Selenium interview questions...

How to create Selenium Cucumber Project in Eclipse.
Author: neptune | 25th-May-2022
#Selenium
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...

Challenges faced in automating Web Applications using Selenium
Author: neptune | 02nd-Oct-2022
#Selenium #Testing
List of Challenges faced by testers using Selenium 1. Popup and Alert Handling, 2. Dynamic element Handling 3. Restricted to only Desktop Browsers Testing...

How to create a Selenium, Cucumber Automation project in Eclipse ?
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
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...

20 TestNG Interview Questions
Author: neptune | 17th-Dec-2022
#Selenium #Interview
There is always more than one test or method in the class. If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution...

Getting started with Selenium WebDriver.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
In this blog I will give you an overview of Selenium WebDriver and Also discuss about advantages and disadvantages of Selenium WebDriver...

Top 10 Selenium Interview Questions.
Author: neptune | 02nd-Apr-2023
#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...

How to send POST request using Rest Assured framework in Java ?
Author: neptune | 26th-Mar-2023
#Selenium #API
Rest Assured is a popular Java-based framework that is used for testing RESTful web services. It provides a simple and intuitive API for sending HTTP requests and validating the responses...

View More