Robot API

During Selenium automation testing, sometimes we need to handle keyboard or mouse to interact with pop-up windows, print pop-up, alerts or native OS applications like Calculator, Notepad, etc. Selenium web driver cannot handle these window pop-ups although it can handle with the help of Robot API with Selenium.

What are the benefits of Robot API

  • Robot API simulates a keyboard or mouse.
  • Robot API will be useful for uploading and downloading of files while using selenium web driver.
  • It can be easily integrated with the current testing framework(like data-driven, keyword or hybrid frameworks).

Documentation of Robot API

Regarding Robot API and documentation, we should know of basic definition, syntax and the usage of all functions/methods. We can be seen this either in the Oracle official website or we can create the documentation on your machine. We can create documentation of Robot API through the following steps:

1. Navigate to JDK folder in System and copy and extract the src.zip file.
2. After extracting the file either on the same location or somewhere else in other drive and navigate to src/java/awt folder.
3. After copying the awt current location open the command prompt.
4. Change the current directory location in cmd to awt folder and type “ javadoc*.java.” and wait for few moments and when the process is completed, we will able to see few HTML files in the folder.
5. Search index.html in the folder and open it. Here we can see the full documentation of awt package and click on the Robot link.
6. Now we can check all Robot API Methods, Constructor summary, and details.

Robot API is basically used to interact with mouse and keyboard events when the browser is automated. Few of the commonly used methods are as follows:

  • keyPress()
  • mousePress()
  • mouseMove()
  • mouseWheel()
  • keyRelease()
  • mouseRelease()
  • delay()

Robot class Constructor and description

1. Robot()
Constructs a Robot object in the coordinate system of the primary screen.
2. Robot(GraphicsDevice screen)
Creates a Robot for the given screen device.

Commonly used Robot class methods

1. public void mouseMove(int x, int y)
Moves mouse pointer to given screen coordinates.

2. public void mousePress(int buttons)
Presses one or more mouse buttons. The mouse buttons should be released using the mouseRelease(int) method.

3. public void mouseRelease(int buttons)
Releases one or more mouse buttons.

4. public void mouseWheel(int wheelAmt)
Rotates the scroll wheel on wheel-equipped mice.

5. public void keyPress(int keycode)
Presses a given key. The key should be released using the keyRelease method.

6. public void keyRelease(int keycode)
Releases a given key.

7. public void delay(int ms)
Sleeps for the specified time. To catch any InterruptedExceptions that occur, Thread.sleep() may be used instead.

Selenium with Java code to automate a simple scenario using Robot class