Robot Class in Selenium WebDriver With Java

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 7, 2024

This Tutorial explains Uses, Examples & Functionalities of Robot Class In Java and its Integration with Selenium Framework:

Robot Java is a Robot class in the Java AWT package. It is generally used to simulate real-time keyboard and mouse operations which we do manually.

The main purpose of the Robot Class in Java is automation. The beauty of it is that it can be easily integrated with the current automation framework to increase its reach.

Selenium Integration with Java Robot Class

Robot Class In Java

In this tutorial, we will learn about Selenium integration with Robot Class in Java but in general, Java Robot Class can be used in any automation framework built on Java.

Simulating Functionalities Of Robot Class

There are many functionalities of Robot Class in Java, enlisted below are some noted methods associated with them:

#1) Keyboard Functionalities

The keyboard functionalities of the Robot class generally deals with the simulation of different keys available in the keyboards and their operations.

Public void keyPress( int keycode)          

This function presses the input parameter key. To complete the operation the key release method should be used.

Follow the below code snippet:

      r.keyPress(KeyEvent.VK_CONTROL);
   r.keyPress(KeyEvent.VK_V); 

Here, in the above code snippet, we are just pressing CTRL+V. The method KeyEvent is associated with other keys available to our keyboard. One can try all of them.

Public void keyRelease(int keycode)

This method releases the input parameter key. This is post-operation to the keypress method.

Follow the below code snippet:

         r.keyRelease(KeyEvent.VK_V);
	r.keyRelease(KeyEvent.VK_CONTROL);

Here in the above code snippet, we are just releasing CTRL+V. Similarly as stated above, the method KeyEvent is also associated with other keys available to our keyboard. One can try all of them.

#2) Mouse Functionalities

The mouse functionalities deal with simulating different operations of the mouse.

Public void mouseMove(int x, int y)

Here, the two parameters determine the coordinates of the screen. Once we give the coordinates the mouse move to the point of the given coordinates of the screen.

Follow the given code snippet:

robot.mouseMove(0, 900);

Here, the position of x parameter is 0, and the y parameter is 900. So the mouse will move to that point. One can do an error and trial method to move to the various positions of the screen.

Public void mousePress(int buttons)

One can imagine this functionality with keyPress functionalities to gain a clear conception. This method simulates mousePress functions.

Follow the below code snippet:

robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);  //associated with mouse left click
robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);  //associated with mouse middle click
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);  //associated with mouse right click

Public void mouseRelease(int buttons)

One can imagine this functionality with keyPress functionalities to gain a clear conception. This method simulates mousePress functions.

Follow the below code snippet:

robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);  //associated with mouse left click
robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK); //associated with mouse middle click
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); //associated with mouse right click

Note: There are several other methods available for exploration. But the above methods are the most frequently used. However to explore other methods one can search for Robot Java class in Oracle docs.

Use Of Robot Class In Java

When and where we can use Robot Class in Java to enhance the functionality effectively in an automation framework?

As we all know that Selenium can be used to automate web applications. These web applications in Selenium use the underlying web object (locators) of the web app to determine the position on the web page and operate accordingly. Now in certain cases, we see that we need to handle windows authentication pop-up or any other windows pop-up while automating on the Windows operating system.

Selenium cannot automate Windows actions but we can use Robot Class in Java to accomplish the task.

Java Robot Class Examples

Example 1: Using Robot Class to Automate some Windows functionalities

Sometimes any small part/module of the entire web application may be designed in flash. We can use the Robot class to handle that part. Since web objects remain hidden for the part designed in flash.

Another important feature that can be automated using Robot Class in Java is Rich Text Editors, which sometimes remain embedded in web applications. There are several operational functions in RTF editors which is quite easy to automate using this class.

Example 2:Using Robot Class for automating RTF fields

Note: There are other open-source tools available in the market like Auto IT (specially designed to automate windows GUI), Sikuli (image processing tool), etc. to accomplish similar tasks.

This is true but Robot Class in Java comes with JDK. Thus, we do not require any other tool to install, this is a straight advantage if one is using a Java-based automation framework.

Automating RTF Fields

Sample Picture:

Automating RTF fields

Example Snippet:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class javainteraction 
{
  public void seleniumwithjava() throws AWTException, InterruptedException
  {
	  System.setProperty("webdriver.chrome.driver", "D://chromedriver//chromedriver.exe");
	   WebDriver driver =new ChromeDriver();
	   driver.get("https://www.tinymce.com" );
	   driver.manage().window().maximize();
	   driver.findElement(By.xpath("(//div[contains(@aria-label,'Bold')]//i[contains(@class,'mce-i-bold')])[1]")).click();
	   Robot r = new Robot();
	   String text = "Welcome to the world of automation.";
	   StringSelection stringSelection = new StringSelection(text);
	   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	   clipboard.setContents(stringSelection, stringSelection);
	   
	   r.keyPress(KeyEvent.VK_CONTROL);
	   r.keyPress(KeyEvent.VK_V);
	   r.keyRelease(KeyEvent.VK_V);
	   r.keyRelease(KeyEvent.VK_CONTROL);
	   
       Thread.sleep(1000);

	   String text1 = "Hope you enjoy This";
	   StringSelection stringSelection1 = new StringSelection(text1);
	   Clipboard clipboard1 = Toolkit.getDefaultToolkit().getSystemClipboard();
	   clipboard1.setContents(stringSelection1, stringSelection1);

	   r.keyPress(KeyEvent.VK_CONTROL);
	   r.keyPress(KeyEvent.VK_V);
	   r.keyRelease(KeyEvent.VK_V);
	   r.keyRelease(KeyEvent.VK_CONTROL);
	   driver.findElement(By.xpath("(//div[contains(@aria-label,'Italic')]//i[contains(@class,'mce-i-italic')])[1]")).click();
  }
  public static void main(String args[]) throws AWTException, InterruptedException
  {
	  javainteraction j1 = new javainteraction();
	  j1.seleniumwithjava();
	  
  }
}

Here, two functionalities of the RTF editors have been automated using bold and italics icon.

Automating Some Windows Functionalities

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class RobotJ {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Robot robot;
		try {
			robot = new Robot();
			robot.mouseMove(0, 900);
			robot.delay(1000);
			robot.mousePress(InputEvent.BUTTON1_MASK);
			robot.mouseRelease(InputEvent.BUTTON1_MASK);
			robot.delay(1000);
			robot.mouseMove(300, 490);
			robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
			robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
			robot.delay(1000);
			robot.mouseMove(400, 690);
			robot.delay(1000);
			robot.mousePress(InputEvent.BUTTON1_MASK);
			robot.mouseRelease(InputEvent.BUTTON1_MASK);
			robot.delay(1000);
			robot.mouseMove(1300, 150);

			
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

This code snippet generally deals with right starting from start icon to viewing the system configuration. Using a similar concept one can also handle windows pop-up generating while initiating or in between operation on a web page.

Tips To Incorporate Robot Java Into Existing Java+Selenium Framework

Now, we are going to see how to modify the above-demonstrated piece of codes so that we can accommodate the same into an existing Selenium+Java framework.

Please note this is a concept and there are many other ways to accomplish the same. We can further extend/modify this concept to meet the business needs which may vary from project to project.

First, create a class that will hold all the common or Robot class made available methods so that whenever it is required we can utilize those methods to any other classes where we are writing test cases.

Below code snippets demonstrate this:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class CommonRobotMethods
{
	Robot robot;
	int a= 0,b= 900; //This value can be exported from property file or excel sheet instead of direct input to the code
	public void LeftClickMouse() throws AWTException
	{
		robot = new Robot();
		robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
		robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
	}
	public void MiddleClickMouse() throws AWTException
	{
		robot = new Robot();
		robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
		robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
	}
	public void RightClickMouse() throws AWTException
	{
		robot = new Robot();
		robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
		robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
	}
	public void MouseMove(int x, int y) throws AWTException
	{
		robot = new Robot();
		robot.mouseMove(x,y);
	}
	public void KeyPressCTRL() throws AWTException
	{
		robot = new Robot();   
		robot.keyPress(KeyEvent.VK_CONTROL);
	}
	public void KeyReleaseCTRL() throws AWTException
	{
		robot = new Robot();   
		robot.keyRelease(KeyEvent.VK_CONTROL);
	}
	public void KeyPressV() throws AWTException
	{
		robot = new Robot();   
		robot.keyPress(KeyEvent.VK_V);
	}
	public void KeyReleaseV() throws AWTException
	{
		robot = new Robot();   
		robot.keyRelease(KeyEvent.VK_V);
	}

public String RobotJavaMethods(String utilityMethod)
  {
	switch(utilityMethod)
	  {
	  case "LeftClickMouse":
		  
		  try
		  { 
			  LeftClickMouse();
		   }
		  catch(AWTException e)
		  {
			  //Write any thing to track the error
		  }
		  break;
         case "MiddleClickMouse":
		  
		  try
		  { 
			  MiddleClickMouse();
		   }
		  catch(AWTException e)
		  {
			  //Write any thing to track the error
		  }
		  break;
         case "RightClickMouse":
   		  
   		  try
   		  { 
   			  RightClickMouse();
   		   }
   		  catch(AWTException e)
   		  {
			  //Write any thing to track the error
   		  }
   		  break;
         case "MouseMove":
      		  
      		  try
      		  { 
				MouseMove(a,b);
      		   }
      		  catch(AWTException e)
      		  {
   			  //Write any thing to track the error
      		  }
      		  break;
         case "KeyPressCTRL":
      		  
      		  try
      		  { 
      			KeyPressCTRL();
      		   }
      		  catch(AWTException e)
      		  {
   			  //Write any thing to track the error
      		  }
      		  break;
         case "KeyReleaseCTRL":
     		  
     		  try
     		  { 
     			 KeyReleaseCTRL();
     		   }
     		  catch(AWTException e)
     		  {
  			  //Write any thing to track the error
     		  }
     		  break;
         case "KeyPressV":
    		  
    		  try
    		  { 
    			  KeyPressV();
    		   }
    		  catch(AWTException e)
    		  {
 			  //Write any thing to track the error
    		  }
    		  break;
         case "KeyReleaseV":
    		  
    		  try
    		  { 
    			 KeyReleaseV();
    		   }
    		  catch(AWTException e)
    		  {
 			  //Write any thing to track the error
    		  }
    		  break;
   		  default:
   			  System.out.println("UnHandled error");
	  }
      return utilityMethod;
  }
}

Here we have taken a few methods, one can similarly add other methods according to their project needs.

Each method is attached to one switch case, so that the case keyword may be used when and wherever required.

Now let’s see how we can transform the code which we have written to automate RTF editor:

Refer the below code snippet to understand that:

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class seleniumFramework 
{
   public void actualOperationalMethod() throws InterruptedException
   {
	   CommonRobotMethods cm = new CommonRobotMethods();
	   System.setProperty("webdriver.chrome.driver", UtilityPathandLoc.Chromedriver_path);
	   WebDriver driver =new ChromeDriver();
	   driver.get(UtilityPathandLoc.APPURL);
	   driver.manage().window().maximize();
	   driver.findElement(By.xpath(UtilityPathandLoc.Bold_loc)).click();
	   String text = UtilityPathandLoc.First_input;
	   StringSelection stringSelection = new StringSelection(text);
	   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	   clipboard.setContents(stringSelection, stringSelection);
	   cm.RobotJavaMethods("KeyPressCTRL");
	   cm.RobotJavaMethods("KeyPressV");
	   cm.RobotJavaMethods("KeyReleaseV");
	   cm.RobotJavaMethods("KeyReleaseCTRL");

	   
       Thread.sleep(1000);

	   String text1 = UtilityPathandLoc.Second_input;
	   StringSelection stringSelection1 = new StringSelection(text1);
	   Clipboard clipboard1 = Toolkit.getDefaultToolkit().getSystemClipboard();
	   clipboard1.setContents(stringSelection1, stringSelection1);

	   cm.RobotJavaMethods("KeyPressCTRL");
	   cm.RobotJavaMethods("KeyPressV");
	   cm.RobotJavaMethods("KeyReleaseV");
	   cm.RobotJavaMethods("KeyReleaseCTRL");
	   
	   driver.findElement(By.xpath(UtilityPathandLoc.Italics_loc)).click();

   }
	public static void main(String args[])
   {
		seleniumFramework s1 = new seleniumFramework();
		try {
			s1.actualOperationalMethod();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
   }
}

We are creating the object of CommonRobotMethods class inside the seleniumFramework class and accessing the methods defined in CommonRobotMethods class via. Switch keywords defined in CommonRobotMethods class.

Here, we have created an interface to write down all the inputs which can be externalized. But one can divide and write in excel/properties or interface according to their comfort level or business needs.

public interface UtilityPathandLoc 
{
 String APPURL= "https://www.tinymce.com";
 String Chromedriver_path = "D://chromedriver//chromedriver.exe";
 String Bold_loc = "(//div[contains(@aria-label,'Bold')]//i[contains(@class,'mce-i-bold')])[1]";
 String Italics_loc = "(//div[contains(@aria-label,'Italic')]//i[contains(@class,'mce-i-italic')])[1]";
 String First_input = "Welcome to the world of automation.";
 String Second_input = "Hope you enjoy This";
}

Conclusion

Hopefully, how to utilize Robot Class within a Selenium + Java framework is clear now. We wish everybody finds this article interesting and helpful in learning the concept.

Was this helpful?

Thanks for your feedback!

Leave a Comment