Robot Class In Selenium WebDriver

During automation activity Selenium often find difficulty to find an object and perform actions which is related to mouse or keyboard type of activity. It is not easy to perform keyboard or mouse events using selenium commands. There are many specific scenarios where any selenium command is launched but actual event did not fired. This result that execution of test is stopped and it reports some error.

Here Java Robot Class come in picture. Using Robot Class we can simulate keyboard and mouse events in Selenium. This class is very easy to use with automation process. It can be easily integrate with current automation framework.
Robot Class is available under java.awt.package.
Methods in Robot Class can be effectively used to do the interaction with popups in Web Applications. Selenium does not provide support to handle browser pop-ups or the native operating system pop-ups. To handle these kind of pop-up, we need help of Robot Class. This is also used while we need to handle file upload and download activity using selenium webDriver.
Some of the popular methods under Robot Class are:
  • .keyPress();
  • .mousePress();
  • .mouseMove();
  • .keyRelease();
  • .mouseRelease();
First of all create the object of the Robot Class as following:

Robot robot=new Robot();

1. .keyPress()


This will press Escape key on keyboard.

2. .keyRelease()


This will release the CAPS_LOCK key.

3. .mousePress()


This will press Left mouse button.

4. .mouseRelease()


This will release Left mouse button.

5. .mouseMove()

robot.mouseMove(coordinates.getX(), coordinates.getY());

This will move the mouse pointer to X and Y co-ordinates.

Code Example of Using Robot Class:
package com.esteyaqueSelenium.SeleProg;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class RobotClass {

public void robotandselenium() throws InterruptedException
WebDriver driver=new FirefoxDriver();
Robot robot=null;
try {
robot=new Robot();
} catch (AWTException e) {
//Keyboard Activity Using Robot Class


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s