Table of Contents

In this tutorial, we will understand an interesting  Java class called the Robot class. The Selenium-based test automation frameworks widely use it for simulating the keyboard and mouse events. In this article, we are going to learn:-

  • What is a Robot class, its methods and how to use them?
  • Its Advantages and Limitations

What is an exciting thing about Robot class?

Selenium scripts use Robot class for automating the browser and desktop pop-ups, but the exciting thing is this class is not part of org.openqa.selenium package of Web Driver API.

Then from where does this class come?

It doesn’t reside in Web Driver API; it is part of the Java API awt package.

Need for Robot class

In the Actions class tutorials series, we have already seen various methods for handling keyboard and mouse events. Now, Actions class handles cases that web driver commands can't handle. Therefore, one may think, why do we need this package which is not even a part of WebDriver API? Answer to lies in the scenarios like below:

  • When the user needs to handle alert pop-ups on a webpage, or
  • User needs to enter text on the pop-ups with a combination of modifier keys such as Alt, Shift, etc.

Here, the pop-ups/alerts are Windows pop-ups instead of Webpage pop-ups.

We know that to perform any action on a web element, we need a locator for the element. But Windows pop-ups don't have any locators, as they are not part of the webpage, they are native OS pop-ups. To handle such pop-ups we need the Robot class.

For instance, if you are trying to download Email Attachment, Windows pop-up, 'Save Attachment' prompts to specify Download Location, appears. It is nothing but a native OS pop-up.

One can't use Action class methods to handle keyboard/mouse events on Desktop windows pop-up. The reason being, Actions class methods need WebElement objects to perform actions. Whereas for Desktop windows pop-up, no locator exists, and the same can be verified using browser developer tools. Therefore, to handle such scenarios, the Robot class is used.

What is a Robot class?

As per the class description, this class is used to generate native system input events. This class uses native system events to control the mouse and keyboard.

Robot Class

It differs from Selenium which uses the WebDriver API and invokes commands to a browser to perform actions.

How to use Robot class methods?

Let us understand the use of Robot class methods with the help of an example on ToolsQa's demo site https://demoqa.com/keyboard-events/. Consider the scenario where a user wants to upload a file.

DemoIntroduction

So, the user clicks on the Choose File button first to enter the file path to be uploaded. Here, pop-up to select file is Desktop Windows appears. Let's use the Robot class methods to enter the file path.

1.Import package: Robot class has to import first, to use.

import java.awt.Robot;

2. Instantiate: A robot class object is needed to invoke its methods. So, let’s instantiate the Robot class.

Robot robot = new Robot();

3. Invoke method: Now invoke the required method on robot object.

robot.<required_method>();

The Robot class provides various methods for handling mouse and keyboard events. For entering the file path, we would need a method to enter text. So, a method to be used here is keyPress(int keycode) which presses a given key keycode.

robot.keyPress(keycode);

Methods in Robot class:

As you can see java.awt.Robot class provides various methods needed for controlling mouse and keyboard.

robot methods

But, we will only cover a few of the commonly used methods for browser test automation.

Following are some of the methods commonly used in browser test automation:

Keyboard methods:

  • keyPress(int keycode): This method presses a given key. For Example, keyPress(KeyEvent.VK_SHIFT) method presses ''SHIFT' key
  • keyRelease(int keycode): This method releases a given key. For Example, keyRelease(KeyEvent.VK_SHIFT) method releases ''SHIFT" key

Mouse Methods:

  • mousePress(int buttons): This method presses one or more mouse buttons.For Example, mousePress(InputEvent.BUTTON1_DOWN_MASK) method is used left click mouse button
  • mouseRelease(int buttons): This method releases one or more mouse buttons. For Example, mouseRelease(InputEvent.BUTTON1_DOWN_MASK) method is used to release the left mouse button click
  • mouseMove(int x, int y): This method moves the mouse pointer to given screen coordinates specified by x and y values. For Example, mouseMove(100, 50) will move the mouse pointer to the x coordinate 100 and y coordinate 50 on the screen.

You can refer to Robot class java documents to explore more methods.

Advantages:

Here are some of the benefits:

  • 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.

Limitations:

But methods mentioned above to control Keyboard and Mouse have some limitations also. Consider some of those following limitations while writing automation scripts:

  • Most of the methods like mouseMove are dependent on screen resolution, so, the method may perform differently on different screens.
  • This class acts only on the window in focus, so the behavior may differ when multiple windows open.
  • Switching between different frames or windows is difficult with Robot methods.

Conclusion:

To conclude, the purpose of this tutorial is to get an introduction to the Robot class. We will be covering Keyboard Events and Mouse Events methods in more detail in our next articles.

Keyboard Events in Selenium Actions Class
Keyboard Events in Selenium Actions Class
Previous Article
Robot Class Keyboard Events
Robot Class Keyboard Events
Next Article

Similar Articles

Feedback