Robot Class – Handle Keyboard and Mouse events in Selenium

Robot Class in selenium web driver  is used to handle the keyboard and mouse events. In previous article of Action Class we have learnt the same to handle the keyboard and mouse events but in Action class we can handle the events on the web page only where as with Robot Class we can handle the keyboard and mouse events in windows popup also.

We can download and upload the file using robot class and can handle the windows popup like Print out popup in chrome and Mozilla using the Robot class and you can attend our online selenium training sessions also.


Interfaces of Robot Class

import java.awt.Robot;


Some Common Methods of Robot Class

1) keyPress(keycode) – It is used to press the any keyboard key. Example – robot.keyPress(KeyEvent.VK_SHIFT) It will press the shift key .

2) keyRelease(keycode) – It is used to release the pressed key of the . Example – robot.keyRelease(KeyEvent.VK_SHIFT) It will release the pressed shift key .

3) mousePress(buttons) – It is used to press any mouse button.Example – robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);  will press the left mouse click button.

4) mouseRelease(buttons) – It is used to release any mouse button.Example – robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);  will press the left mouse click button.

5) mouseMove(x, y) – It is used to move mouse pointer to given screen coordinates..Example – rb.mouseMove(100,100);  will move the mouse to particular x and y cordinates


Java Selenium Program using Robot Class handling Keyboard Events

1) First we will Launch the “facebook.com” control will initially at “First Name” Text box.
2) Then we we will press key  “SHIFT” key of keyboard using Robot Class.
3) Then we we will press keys  “A” ,”B” and “C” key of keyboard using Robot Class.
4) Then we we will release key  “SHIFT” key of keyboard using Robot Class.
5) Then we we will release keys  “A” ,”B” and “C” key of keyboard using Robot Class.
6) Then we we will press key  “TAB” key of keyboard using Robot Class to switch the control to “Surname” Text box.
7) Then we we will press key  “SHIFT” key of keyboard using Robot Class.
8) Then we we will press keys  “X” ,”Y” and “Z” key of keyboard using Robot Class.
9) Then we we will release key  “SHIFT” key of keyboard using Robot Class.
10) Then we we will release keys  “X” ,”Y” and “Z” key of keyboard using Robot Class.

Java Program To handle Keyboard events using Robot Class

package convert.Maven_Basics;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Robot_Tutorial_Keyboard {

	public static void main(String[] args) throws AWTException, InterruptedException {

		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//open browser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//open url
		driver.get("https://www.facebook.com/");
		
		// create robot class object
		Robot robot = new Robot();
		
		//Press "Shift" Key of keyboard	to capitalize letter	
		robot.keyPress(KeyEvent.VK_SHIFT);

		//press the Key "A" of keyboard		
		robot.keyPress(KeyEvent.VK_A);
		//Release the Key "A" of keyboard
		robot.keyRelease(KeyEvent.VK_A);

		//press the Key "B" of keyboard		
		robot.keyPress(KeyEvent.VK_B);
		//Release the Key "B" of keyboard
		robot.keyRelease(KeyEvent.VK_B);

		//press the Key "C" of keyboard		
		robot.keyPress(KeyEvent.VK_C);
		//Release the Key "C" of keyboard
		robot.keyRelease(KeyEvent.VK_C);
		
		//Release "Shift" Key of keyboard		
		robot.keyRelease(KeyEvent.VK_SHIFT);
		
		//press the "Tab" Key of keyboard		
		robot.keyPress(KeyEvent.VK_TAB);
		//Release the "Tab "Key of keyboard
		robot.keyRelease(KeyEvent.VK_TAB);

		//press the Key "X" of keyboard		
		robot.keyPress(KeyEvent.VK_X);
		//Release the Key "X" of keyboard
		robot.keyRelease(KeyEvent.VK_X);

		//press the Key "Y" of keyboard		
		robot.keyPress(KeyEvent.VK_Y);
		//Release the Key "Y" of keyboard
		robot.keyRelease(KeyEvent.VK_Y);
		
		//press the Key "Z" of keyboard		
		robot.keyPress(KeyEvent.VK_Z);
		//Release the Key "Z" of keyboard
		robot.keyRelease(KeyEvent.VK_Z);
		
	}

}

Highlighted Text Box are empty before execution

Robot-Class in selenium

Highlighted Text Box- Keys are sent using Robot class.

Robot Class


Handling Mouse Events using Robot Class

1) First we will Launch the “facebook.com“.
2) Then we we will press key  “SHIFT” key of keyboard using Robot Class.
3) Then we we will press keys  “A” ,”B” and “C” key of keyboard using Robot Class.
4) Then we we will release key  “SHIFT” key of keyboard using Robot Class.
5) Then we we will release keys  “A” ,”B” and “C” key of keyboard using Robot Class.
6) Move the Cursor position to (200 pixel – X axis , 200 pixel- Y direction).
7) Then we we will press “Right Click” Mouse button of Mouse using Robot Class.
8) Then we we will press “Left Click” Mouse button of Mouse using Robot Class.
9) Then we we will Release “Left Click” Mouse button of Mouse using Robot Class.
10) Then we we will press “Left Click” Mouse button of Mouse using Robot Class.
11) Then we we will Release “Left Click” Mouse button of Mouse using Robot Class.

Handling Mouse Events using Robot Class in selenium

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Robot_Tutorials_Mouse {


	public static void main(String[] args) throws AWTException, InterruptedException {

		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		//open browser
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		//open url
		driver.get("https://www.facebook.com/");

		// create robot class object
		Robot robot = new Robot();

		//press the Key "A" of keyboard		
		robot.keyPress(KeyEvent.VK_A);
		//Release the Key "A" of keyboard
		robot.keyRelease(KeyEvent.VK_A);
		//press the Key "B" of keyboard		
		robot.keyPress(KeyEvent.VK_B);
		//Release the Key "B" of keyboard
		robot.keyRelease(KeyEvent.VK_B);

		//press the Key "C" of keyboard		
		robot.keyPress(KeyEvent.VK_C);
		//Release the Key "C" of keyboard
		robot.keyRelease(KeyEvent.VK_C);

		// move the mouse to 200,200 pixel location
		robot.mouseMove(200, 200);

		// click the right click mouse button
		robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
		// Release the right click mouse button
		robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

		Thread.sleep(5000);

		// double click left mouse button
		// click the left click mouse button
		robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
		// Release the left click mouse button
		robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
		// click the left click mouse button
		robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
		// Release the left click mouse button
		robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

	}
}

Below Screen shot displaying Right CLick on (200,200) position

Robot-Class in selenium


Note :
1) Robot class works on the current window which is opened so if window is switched in between the robot class control will go to that window and start executing from new window only. Be cautious while using Robot class while working on multiple windows and don’t perform any activity with keyboard and mouse manually while using robot class function is executing.
2) Using mouseMove(x, y) will vary from screen to screen resolution and give different result on different screen resolution.
3) Make sure if you press any mouse click or keyboard button , release that click or button also , else the key will remain pressed even after the driver quits also and you have to restart your machine to release the key as Robot class don’t use driver it will remain press for windows also.


Interview Question Robot Class in Selenium

Question 1 ) How we can scroll down page using Robot class ?
Question 2 ) How we can download and upload files using Robot class ?
Question 3 ) What are the disadvantages of using Robot class ?


Hope you like this selenium tutorial using Robot class for handling keyboard events and mouse events. This Article covers all the topics with respect to the interview purpose also.

Leave a Comment

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