What is Robot Class in Selenium WebDriver: Step-by-Step Guide

Last updated on Oct 07,2024 36K Views

What is Robot Class in Selenium WebDriver: Step-by-Step Guide

edureka.co

Robots help in managing all the activities like performing the task within the specified time, handling the mouse functions and the keyboard functions and many more. This article on Robot class in Selenium deals with certain functions that help in controlling the mouse and the keyboard while testing an application using Selenium. You can learn more with the Selenium Certification

“Robots will play an important role in providing physical assistance and even companionship for the elderly” – BIll Gates

 

So, let’s start with our first topic, What is Robot class?

What is a Robot class?

Time plays a major role while testing an application and we need to make sure we complete the desired task within the specific time.

A Robot class in Selenium is used to generate native system input events for test automation, self-running demos, and other applications where you need control over the mouse and keyboard. WebDriver cannot handle the OS popups, so in Java 1.3, Robot class was introduced.

The primary purpose of this Robot class is to facilitate automated testing for Java platform implementations. In simple terms, I would say this class provides control over the mouse and keyboard devices.

This is very easy to implement as it can be easily integrated with current automation framework

Find out our Selenium Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUS
HyderabadUK
PuneCanada
ChennaiAustralia
MumbaiSingapore
KolkataEdinburgh

Now, let us understand the importance of this class

Importance of Robot class

Let’s move on to our next topic and understand what are the different methods that are used to implement the Robot class.

Methods to implement the Robot class

To implement the Robot class, we require a few methods that help in easy execution of test scripts. We have: 

  1. KeyPress()
  2. KeyRelease()
  3. MouseMove()
  4. MousePress()
  5. MouseRelease()

We also use the KeyEvent which is a separate class that is responsible for the keyStroke

Let’s understand each of them in detail

Ex: robot.keyPress(keyEvent.VK_UP);

This will press the UP key on the keyboard

Ex: robot.keyRelease(keyEvent.VK_CAPS_LOCK);

This will release the pressed capslock key in the keyboard

Ex: robot.mouseMove(coordinates.get.X(),coordinates.get.Y());

This method is called when you want to move the mouse over X and Y coordinates

Ex: robot.mousePress(InputEvent.BUTTON1_MASK);

Helps in pressing the mouse button

Ex: robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK) 

This method is used to release the right click of the mouse

Now, let’s try to implement this robot class

How to implement the Robot class in Selenium

We’ll try to automate the web page edureka.co to unleash their way of interaction

Step 1: Link the respective browser driver to the ChromeDriver and specify the path

Step 2: Get the URL of the corresponding webpage and the O.S pop-up appears when you navigate.

Step 3: Find the element on the web page using the element locators

Step 4: To handle the pop-ups we use Robot class, using this we create an instance of Robot Class in the code say Robot robot = new Robot(). Robot class is present in the AWT package of JDK.

To perform the mouse actions, we use

Step 1: MouseMove() method takes the x and y coordinates as its parameters robot.mouseMove(640, 360)where 640 indicates X-axis and 360 indicate Y-axis. So, this method will move the mouse pointer from the current position to the X and Y intersection point.

Step 2: We need to press the mouse button. We will use mousePress method robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) .

Step 3: After the key is pressed, the mouse button needs to be released. We will use MouseRelease() to do it. robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK) this helps in releasing the left click of the mouse.


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.chrome.ChromeDriver;

public class DemoClass {

public static void main(String[] args) throws AWTException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:UsersNeha_VaidyaDesktopchromedriver_win32chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.edureka.co");
driver.manage().window().maximize();
driver.findElement(By.linkText("Courses")).click();
Robot robot = new Robot();
Thread.sleep(4000);
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(4000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(4000);
System.out.println("a");
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(4000);
System.out.println("b");
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(4000);
System.out.println("c");
robot.mouseMove(30,100);
Thread.sleep(4000);
System.out.println("d");
driver.quit();

}

}

Now. let us have a look at the output of this program

It first gets the URL of the web page edureka.co 

Next, it navigates to the next page edureka.co/all-courses using the LinkText

Presses the TAB key on the keyboard

Once this is done, we’ll execute the actions performed on the mouse

Also, check out this video on Robot class where the experts have explained it in a detailed manner

Robot Class in Selenium WebDriver | Handle Keyboard Events in Selenium

This video will help you understand how to automate keyboard and mouse functions using simple methods.

Now, let’s see what are the limitations of this robot class

Limitations of the robot class

Related Learning: Desired Capabilities in Selenium

With this, we come to the end of “Robot class in Selenium” blog. I Hope you guys enjoyed this article and understood what is a Robot Class in Selenium. Now that you have understood how a Robot class can handle the mouse and keyboard functions, check out the Selenium Certification Course by Edureka, a trusted online learning company with a network of more than 650,000 satisfied learners spread across the globe. This course is designed to introduce you to the complete Selenium features and its importance in testing software. 

Got a question for us? Please mention it in the comments section we will get back to you.

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 23rd November,2024

23rd November

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 25th November,2024

25th November

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Selenium Framework Explained in 60 Minutes