top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

ROBOT Class in Selenium WebDriver

Introduction:

In Selenium Automation Tests, sometimes we neeed to control mouse or keyboard events to interact with Operating System windows like Download Alerts, Pop-ups, print Pop-ups, etc. or we need to automate native Operating system applications like Calculator, Skype, etc.

Selenium WebDriver cannot handle these OS pop-ups or applications directly. But by 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. Because in such scenarios you can't 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 can easily be integrated with an automation framework like Keyword, hybrid, and data-driven.


Disadvantages of Robot Class

Robot framwork has few disadvantages mentioned below:

  1. Keyword/mouse event will only works on current instance of Window. E.g. suppose a code is performing any robot class event, and during the code execution user has moved to some other screen then keyword/mouse event will occur on that screen.

  2. Most of the methods like mouseMove is screen resolution dependent so there might be a chance that code working on one machine might not work on other.

Understanding Robot Class internal methods and usage

To interact with keyboard/mouse events while performing automation, robot class methods are used.

lternatively, we can use the Auti IT 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

keyRelease() :

Example: robot.keyRelease(KeyEvent.VK_DOWN)

The down key arrow of the keyboard is released in this method.


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.

For Example:

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.



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.


robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK)

This method will release the mouse of the right click.


Let’s discuss an Example:

1- Open Facebook.

2- Enter the Username and password.

3- Using the robot class press the Enter button.

How to execute Robot Class code using TestNG


Since, now you aware of basic methods of Robot Class so let’s understand few more complex methods –

Suppose you do not want to use the click method for clicking at web element.

In such cases, you can use mouseMove method of the Robot class.


Running code using testNG:

Running code using Testng requires maven dependency of testNG or referenced library of TestNG jar file.

TestNG maven dependency:

<dependency>	
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>	
  <version>7.7.1</version>	
  <scope>test</scope>
</dependency>

Program for Robot Class in Selenium Webdriver:

Executing program using TESTNG:


package testSuite1; 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; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.annotations.Test; public class TestLogin { @Test public void testRobo() throws Exception{ System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver_win32\\chromedriver.exe"); ChromeOptions chromeOption = new ChromeOptions(); chromeOption.addArguments("--remote-allow-origins=*"); WebDriver driver = new ChromeDriver(chromeOption); // Maximize the window driver.manage().window().maximize(); // Open facebook driver.get("http://www.facebook.com"); // Enter Username driver.findElement(By.id("email")).sendKeys("xxxxxxxx@gmail.com"); //Enter your Email

// Enter password driver.findElement(By.id("pass")).sendKeys("xxxxxxxx");//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); } }

testng.xml


<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="TestSuite1"> <test parallel="false" thread-count="5" name="Sanity"> <classes> <class name = "testSuite1.TestLogin"> <methods> <include name="testRobo"></include> </methods> </class> </classes> </test> <!-- Sanity --> </suite> <!-- TestSuite1 -->


Output:



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:


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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Robot_Mouse_Class {

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

System.setProperty("webdriver.chrome.driver", 	"C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
		ChromeOptions chromeOption = new ChromeOptions();
		chromeOption.addArguments("--remote-allow-origins=*");
		WebDriver driver = new ChromeDriver(chromeOption);
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.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
public class Robot_Mouse_Class {
 
public static void main(String[] args) {
 
System.setProperty("webdriver.chrome.driver", 	"C:\\Drivers\\chromedriver_win32\\chromedriver.exe");
		ChromeOptions chromeOption = new ChromeOptions();
		chromeOption.addArguments("--remote-allow-origins=*");
		WebDriver driver = new ChromeDriver(chromeOption);
 
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.




112 views0 comments

Recent Posts

See All
bottom of page