Introduction to Robot Class in Selenium

Table of Contents

Robot class is used in Selenium scripts to automate browser and desktop pop-up windows, but the interesting thing is that this class is not included in the Web Driver API’s org.openqa.selenium package.

So where did this class originate from?

It is a component of the Java API’s awt package and not the Web Driver API.

We have already seen a variety of techniques for managing keyboard and mouse events. Now, the Actions class takes care of situations that web driver instructions can’t. One would therefore wonder why we need this package when it isn’t even a part of the WebDriver API. The following situations provide the solution:

  • When a webpage’s alert pop-ups need to be handled by the user or when the user needs to utilise a combination of modifier keys, like Alt and Shift, to enter text into the pop-ups.
  • Instead of pop-ups from websites, they are Windows pop-ups or alerts.

We are aware that a web element’s location is necessary before we can take any action on it. However, as Windows pop-ups are local to the OS and not a webpage, they lack locators. We need the Robot class to deal with these pop-ups.

For instance, when you attempt to download an email attachment, a Windows pop-up asking you to choose a download location will display. All that it is is a native OS pop-up.

To handle keyboard and mouse events on pop-up desktop windows, one cannot use Action class methods. Due to the fact that actions performed by methods of the Actions class require WebElement objects. However, there is no location for desktop windows that come up, and this can be confirmed using browser developer tools. Therefore, the Robot class is utilised to handle such cases.

Check out the Selenium training course to learn more about Robot Class.

What is a Robot class?

This class creates native system input events, as stated in the class description. The mouse and keyboard in this class are controlled by native system events.

It varies from Selenium, which invokes commands to a browser to carry out activities and uses the WebDriver API.

How to use Robot class methods?

The user enters the file path to be uploaded by first clicking the Choose File button. A desktop Windows pop-up asking you to choose a file appears here. Let’s enter the file path using the methods of the Robot class.

  1. Import the package before using the Robot class.

import Robot from Java Awt;

  1. Create an object of the robot class in order to call its methods. Let’s create an instance of the Robot class.

robot Robot = new Robot();

  1. Call method: At this point, call the necessary method on the robot object.

robot.<required_method>();

The Robot class offers a number of ways to handle keyboard and mouse events. We would require a way to enter text in order to enter the file location. KeyPress(int keycode), a method that presses a specified key, can therefore be utilised in this situation.

robot.keyPress(keycode);

Methods in Robot Class

The Java.awt.Robot class offers a variety of mouse and keyboard control methods.

However, we will simply go through a few of the most popular techniques for automating browser tests.

The following are some of the techniques frequently used in automated browser testing:

Keyboard Methods

  • keyPress(int keycode): Presses the specified key. As an illustration, the keyPress(KeyEvent.VK_SHIFT) method hits the key “SHIFT.”
  • The method keyRelease(int keycode) releases the specified key. For instance, the method keyRelease(KeyEvent.VK_SHIFT) releases the “SHIFT” key.
Robot Class

Mouse Methods

  • mousePress(int buttons): This process involves pressing a mouse button or buttons. Use the left mouse button to press the mousePress(InputEvent.BUTTON1_DOWN_MASK) method as an example.
  • button(int) mouseRelease: This technique releases a mouse button or buttons. To release the left mouse button, use the mouseRelease(InputEvent.BUTTON1_DOWN_MASK) method, for instance.
  • click mouseMoving (int x, int y): With the help of x and y variables, this function moves the mouse pointer to specific screen locations. As an illustration, mouseMove(100, 50) will move the mouse pointer to the screen’s x and y coordinates of 100 and 50, respectively.

To learn more methods, consult the Java documentation for the Robot class.

Advantages

Here are a few advantages:

  • It allows control over both mouse and keyboard events.
  • It provides a means of managing interactions with operating system pop-ups, which Selenium Web Driver API is unable to support.
  • By interacting with OS pop-ups, the robot class is particularly helpful in managing file upload/download operations.
  • Given that this class is a component of the Java package, consuming it in Java Selenium scripts is simple.

Limitations 

However, the methods discussed above to control the keyboard and mouse also have restrictions. When writing automation programs, take into account some of the following restrictions:

Introduction to Robot Class in Selenium
  • The majority of techniques, like mouseMove, are dependent on the screen resolution, therefore they may function differently on various displays.
  • This class only affects windows, so the behaviour might be different when multiple windows open.
  • Switching between different frames or windows is hard with Robot methods.

Conclusion 

Robot Class is very important in Selenium testing. You can check out the Selenium interview questions and answers to get more in-depth information

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class