What is Robot Class in Selenium and How to Use it?

 

Difference between Robot class and action class in Selenium

The difference between Robot class and Actions class in selenium is related to how they work.

  1. Robot class performs or controls the mouse and keyboard events using system native events but in Actions class in selenium uses Webdriver API to send commands to the browser and perform operations (through JSON wire protocol)
  2. Robot class defined into java.awt package wheras actions class is defined into Oopenqa.selenium.interactions package.

Hello Everyone, Welcome to Learn Automation. In this post, we will learn what is Robot class in Selenium, it’s methods and how to use it, how to upload files using robot class, how to click element in Robot class, how to perform click operation using actions class, difference between Robot and Actions class etc.

Let’s discuss Robot class in Selenium.

What is Robot Class in Selenium?

Robot class is a part of Java. It is a java class which is used in Selenium automation to make automation easy to performing different operation. It is widely used to simulate keyboard and mouse events in selenium. It is easy to implement into Selenium automation framework.

Selenium also doesn’t handle OS popups, so in java 1.3, it was implemented.

Usage of Robot class in Selenium:

To perform keyboard and mouse operation

Handle windows popups

Upload and download the files

Methods of Robot Class:

Keyboard methods

keyPress()

keyRelease()

Mouse methods-

mousePress()

mouseRelease()

mouseMove()

 

Let’s discuss one scenario which covers keyboard methods.

Scenario:-

1. Open Facebook login page.

2. Enter username and using robot class press SHIFT and move on password text field.

3. Press Enter using robot class.

package datapkg;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

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

public class RobotClass {
	

    static WebDriver driver = null;
     public static void main(String[] args) throws InterruptedException, AWTException {
		
	    
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.get("https://www.facebook.com/");
		
		//Enter username 
		driver.findElement(By.name("email")).sendKeys("[email protected]");
		
		//create object of Robot class
		Robot r = new Robot();
		r.keyPress(KeyEvent.VK_SHIFT);	
		
		//Enter password
		driver.findElement(By.name("pass")).sendKeys("Selenium123");
		
		//Hit Enter of keyboard using Robot Class
	     r.keyPress(KeyEvent.VK_ENTER);
		
		//Release Enter
	     r.keyRelease(KeyEvent.VK_ENTER);
		
		

		}

}

Robot Class to Upload File in Selenium:

Robot class in Selenium
                                                                       To Upload file using Robot Class

Now we will upload the file using Robot class.

Step 1- We have to copy file location in system clipboard.

Scenario:

Open naukri.com

Click on user type

Click on Upload Resume

Selenium Script – To upload file in Selenium using Robot class

package datapkg;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

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

public class FileUploadRobotClass {


	static WebDriver driver = null;
    public static void main(String[] args) throws InterruptedException, AWTException {
		
	    
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		
		
		
		 // Specify the file location
		 StringSelection sel = new StringSelection("E:\\Resume.docx");
		 
		 // Copy to clipboard
		 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
		 System.out.println("selection" +sel);
		 
		 
		// Open naukri.com
		driver.get("https://www.naukri.com/account/register/basicdetails");
		
		//Click on upload resume button
		driver.findElement(By.name("userType")).click();
		Thread.sleep(3000);
		//Click on upload resume button
		driver.findElement(By.name("uploadCV")).click();
		 Thread.sleep(1000);
		
		// Create object of Robot class
		 Robot robot = new Robot();
		 Thread.sleep(1000);
		      
		  // Press Enter
		 robot.keyPress(KeyEvent.VK_ENTER);
		 
		// Release Enter
		 robot.keyRelease(KeyEvent.VK_ENTER);
		 
		  // Press CTRL+V
		 robot.keyPress(KeyEvent.VK_CONTROL);
		 robot.keyPress(KeyEvent.VK_V);
		 
		// Release CTRL+V
		 robot.keyRelease(KeyEvent.VK_CONTROL);
		 robot.keyRelease(KeyEvent.VK_V);
		 Thread.sleep(1000);
		        
		 //Press Enter 
		 robot.keyPress(KeyEvent.VK_ENTER);
		 robot.keyRelease(KeyEvent.VK_ENTER);
		 
		
    }
}

 

How to click using Robot class in selenium:

To perform the mouse operation we have to use mouse event methods and following steps:

Step 1: mouseMove(int x, int y) – This method move the mouse pointer and takes x and y coordinates . Example – mouseMove(100,200)  will move mouse pointer to 100 in x coordinate and 200 in y coordinate.

Step 2: mousePress(int buttons) – After mouse move on element we have to press mouse. We will use mousePress method

robot.mousePress(InputEvent.Button1_DOWN_MASK) – method is used left click mouse button.

Step 3: mouseRelease(int buttons) – After mouse move on element we have to release mouse. We will use mouseRelease method.

robot.mouseRelease(InputEvent.Button1_DOWN_MASK) – method is used left click mouse button.

Selenium Script –

package datapkg;

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 RobotMouseEvent {
	
	public static void main(String[] args) throws AWTException, InterruptedException {
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://automationtestings.com/");
		driver.manage().window().maximize();
		driver.navigate().to("https://automationtestings.com/category/interview-questions/");
		Robot robot = new Robot();
		Thread.sleep(4000);
		robot.keyPress(KeyEvent.VK_DOWN);
		Thread.sleep(2000);

		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(100,100);
		Thread.sleep(4000);
		System.out.println("d");
		driver.quit();
		 
		}

}

Are you enjoying this post? Then please do subscribe and share this post.

How to enter text using Robot class in Selenium:

Scenario:

  1. Navigate to https://www.facebook.com/
  2. Move on ‘Your Name’ text field using robot class
  3.  Type Name.

Selenium Script –

package datapkg;

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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class EnterTextUsingRbtClass {
	
	public static void main(String[] args) throws AWTException, InterruptedException {
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.facebook.com");
		driver.manage().window().maximize();
		
		WebElement ele = driver.findElement(By.name("email"));
		
		int xaxis = ele.getLocation().x;
		int yaxis = ele.getLocation().y;
		
	    int width = ele.getSize().width;
	    int height = ele.getSize().height;
		
		
		Robot r = new Robot();
		
		r.mouseMove(xaxis+width/2, yaxis+height/2);
		
		r.mousePress(KeyEvent.BUTTON1_DOWN_MASK);   // Mouse left button press
		r.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);  // mOUSE left button release
		
		r.keyPress(KeyEvent.VK_CAPS_LOCK);
		r.keyRelease(KeyEvent.VK_CAPS_LOCK);
		Thread.sleep(2000);
		
		ele.sendKeys("Ajeet Maurya"); // type name
		
		
		
		
		

}
	
}


Now let’s discuss limitations of Robot class.

Limitations of Robot Class:

  • Keyboard and mouse event will only work with current instance of the window.
  • Sometimes keyboard and mouse event may not work if screen resolution changed.
  • While code execution if window focus change to another window, mouse or keyword event will still remain on previous window so it will not work.
  • It is difficult to move to different frame or window in between execution of code.

How to handle Actions class in Selenium?

How to handle Actions class in Selenium
Action Class in Selenium

Actions class is a collection of individual action. Actions class is used to perform keyboard and mouse events. It is mainly used to perform drag and drop operation, right click operation, clicking on multiple elements with control key, double click and many more. Action class performed these operations using advance user interaction API in Selenium.

Syntax of Action class:

Actions a = new Actions(driver);
a.moveToElement(element).click().perform();

Methods of Actions class:

Actions class is used for mainly keyboard and mouse event. In order to perform these events Selenium provides different methods.

Methods Of Actions Class

Mouse Actions:

clickAndHold() – click on element without releasing mouse.

contextClick()- To perform right click on element.

doubleClick() – Double click on element.

dragAndDrop(source, target)- Click and hold element and move it to the target location and release the element.

moveToElement(webElement) – Move the mouse to the webElement.

Keyboard Actions:

keyDown() – It is used to press the modifier keys. Example – (Keys.ALT, Keys.CONTROL, Keys.SHIFT)

keyUP() – It is used to release the modifier keys. Example – (Keys.ALT, Keys.CONTROL, Keys.SHIFT)

release – It is used to release mouse left button at current location.

SendKeys()- Send series of keys to the element.

Example of Action Class in Selenium:

Perform Click operation using Action class in Selenium:

Test Scenario:

Visit the https://automationtestings.com/

Mouse hover on Interview Questions on header.

Click on this.

Selenium Code:

package datapkg;

import java.awt.AWTException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionClassExp {
	
	
	public static void main(String[] args) throws AWTException, InterruptedException {
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://automationtestings.com/");
		driver.manage().window().maximize();
		
		WebElement element = driver.findElement(By.xpath("//a[@href='https://automationtestings.com/category/interview-questions/']"));
		
		Actions a = new Actions(driver);
		a.moveToElement(element).click().perform();
		

}
}


Robot vs action class in Selenium:

Robot vs action class in Selenium
                                                                                Robot Class Vs Actions Class

Many times we faced whether we use Action class or Robot class while automating web application.

Maximum times we use Actions class to perform mouse and keyboard event but sometimes Actions class does not work. In that case we have to use Robot class.

The difference between two classes lies in terms of how they work.

Actions class uses the WebDriver API to send the commands to the browser to perform the actions. But in Robot class it uses the native system event to perform mouse and keyboard events.

Actions class is defined in  org.openqa.selenium.interactions package whereas Robot class is defined in java.awt package.

Ideally while doing automation we never ever use Robot class since the functionally provided by selenium Action class is sufficient.


Difference between Robot class and action class in Selenium

The difference between Robot class and Actions class in selenium is related to how they work.

  1. Robot class performs or controls the mouse and keyboard events using system native events but in Actions class in selenium uses Webdriver API to send commands to the browser and perform operations (through JSON wire protocol)
  2. Robot class defined into java.awt package wheras actions class is defined into openqa.selenium.interactions package.

Summary:

In this post, we have covered ‘What is Robot class in Selenium and it’s methods and how to use it.

We have also covered – how to upload files using robot class, how to click element in Robot class, how to perform click operation using Actions class.

I am sure this content added some additional value in your skills and also helpful to preparation of your interviews.

Final word, Bookmark this post “What is Robot class in Selenium”  for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

Leave a Comment