All IT Courses 50% Off
Selenium Tutorials

Robot Class in Selenium Webdriver

Why Robot Class?

In Selenium Automation Tests, sometimes there is a need to control mouse or keyboard events to interact with Operating System windows like Download Alerts, Pop-ups, print Pop-ups, etc. or can automate native Operating system applications like Calculator, Skype, etc. Selenium WebDriver cannot handle these OS pop-ups or applications directly but using Robot Class we can handle OS pop-ups or applications. The robot is used in a scenario where you can’t automate using Selenium Webdriver. Now the question comes why can’t we automate with selenium webdriver because in such scenarios you can find the properties of those elements i.e web elements or desktop elements. So as you can’t inspect the properties so you can’t automate them, so in such scenarios, we can use robot class methods.  

Benefits of Robot Class

  • It can simulate Keyboard and Mouse events
  • When using Selenium WebDriver it supports in download or upload of files
  • It will Automate Tier-1 Applications.
  • It can easily be integrated with an automation framework like Keyword, hybrid, and data-driven.

Disadvantages of Robot Class

  • The Keyword or Mouse event will only work on the current instance of Window. For example: While performing the code any robot class event, and during the code, execution user moved to some other screen then Keyword or Mouse event will occur on that screen. 

Understanding Robot Class internal methods and usage

To interact with keyboard/mouse events while performing automation robot class methods are used. Alternatively, we can use the AutoIT tool, but the drawback of it is that it generates an executable file (exe) which will only work on windows, which is not a good option to use.

Methods that are commonly used of Robot Class during web automation:

  • keyPress(): Example: r.keyPress(KeyEvent.VK_DOWN) : This method will press down arrow key of the Keyboard
  • mousePress() :This method will simulates mousePress functions. Example: robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) : This method will press the left click of your mouse. robot.mousePress(InputEvent.BUTTON2_DOWN_MASK) : This method will press the middle click of your mouse. robot.mousePress(InputEvent.BUTTON3_DOWN_MASK) : This method will press the mouse of right button.
  • mouseMove() : Example: robot.mouseMove(int X(), int Y()) : This method will move mouse pointer to the specified X and Y coordinates of the screen

Follow the given code snippet:

robot.mouseMove(0, 700);

Here, the x parameter position is 0, and the y parameter is 700. So to that point the mouse will move. To move to the various positions of the screen one can perform an error and trial method.

  • keyRelease() : Example: robot.keyRelease(KeyEvent.VK_DOWN) : The down key arrow of the keyboard is released in this method.
  • mouseRelease(): This method will simulates mousePress functions

Example: 

robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK) : This method will release the left click of your mouse.

robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK) : This method will release the middle click of your mouse.

All IT Courses 50% Off

robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK) : This method will release the mouse of the right click.

Let’s discuss the Scenario which covers enter key 

1- Open Facebook.

2- Enter the Username and password.

3- Using the robot class press the Enter button.

Program for Robot Class in Selenium Webdriver

package htmldriver;

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 TestLogin {

@Test
public void TestRobo() throws Exception{
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
     WebDriver driver = new FirefoxDriver();
     // Maximize the window
     driver.manage().window().maximize();
     // Open facebook
     driver.get("http://www.facebook.com");
     // Enter Username
     //driver.findElement(By.id("email")).sendKeys("Selenium@gmail.com");
     driver.findElement(By.id("email")).sendKeys("xxxxabc@gmail.com");//Enter your Email
     // Enter password
     driver.findElement(By.id("pass")).sendKeys("xxx@12345");//Enter your password
     // Create object of Robot class
     Robot r=new Robot();
     // Press Enter
     r.keyPress(KeyEvent.VK_ENTER);
     // Release Enter
     r.keyRelease(KeyEvent.VK_ENTER);


}
}

Mouse movement:

import java.awt.Robot;
 
public class MouseClass {
 public static void main(String[] args) throws Exception {
     Robot robot = new Robot();
 
     // SET THE MOUSE X Y POSITION
     robot.mouseMove(300, 650);
     }
}

Press the left/right button of the mouse:

package htmldriver;

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Robot_Mouse_Class {

public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
     WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com");

// Create object of Robot class
Robot robot = new Robot();

// Press Left button
robot.mousePress(InputEvent.BUTTON1_MASK);

// Release Left button
robot.mouseRelease(InputEvent.BUTTON1_MASK);

// Press Middle button
robot.mousePress(InputEvent.BUTTON2_MASK);

// Release Middle button
robot.mouseRelease(InputEvent.BUTTON2_MASK);

// Press Right button
robot.mousePress(InputEvent.BUTTON3_MASK);

// Release Right button
robot.mouseRelease(InputEvent.BUTTON3_MASK);



}

}

Example: Scroll Mouse Using Robots class: 

  • Scroll the Mouse using mouseWheel() Method.
import java.awt.Robot;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class Robot_Mouse_Class {
 
public static void main(String[] args) {
 
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");  
 
WebDriver driver = new FirefoxDriver();
 
driver.get("https://www.facebook.com");
 
// Create object of Robot class
Robot robot = new Robot();
 
// Scroll MouseWheel
robot.mouseWheel(5);
 
}
 
}

Conclusion

In AWT package Robot class is used to generate keyboard/mouse events to interact with the OS windows and the native apps.

The main purpose of Robot is to support selenium automated tests project to build in Java platform.

Facebook Comments

Related Articles

Back to top button