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();
Example:
First of all create the object of the Robot Class as following:

Robot robot=new Robot();

1. .keyPress()

robot.keyPress(KeyEvent.VK_ESC);

This will press Escape key on keyboard.

2. .keyRelease()

robot.keyRelease(KeyEvent.VK_CAPS_LOCK);

This will release the CAPS_LOCK key.

3. .mousePress()

robot.mousePress(InputEvent.BUTTON1_MASK);

This will press Left mouse button.

4. .mouseRelease()

robot.mouseRelease(InputEvent.BUTTON1_MASK);

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 {

@Test
public void robotandselenium() throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
Robot robot=null;
driver.get(“http://www.makemytrip.com”);
driver.manage().window().maximize();
driver.findElement(By.xpath(“.//*[@id=’ssologinlink’]”)).click();
driver.findElement(By.xpath(“.//*[@id=’username’]”)).sendKeys(“username@gmail.com”);
driver.findElement(By.xpath(“.//*[@id=’password_text’]”)).sendKeys(“password”);
try {
robot=new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//Keyboard Activity Using Robot Class
robot.keyPress(KeyEvent.VK_ENTER);
}
}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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