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 to automate native Operating System applications like Calculator, Skype, etc. Selenium WebDriver cannot handle these OS pop-ups or applications directly, but using the Robot Class, we can effectively manage OS pop-ups or applications.
Mastering these advanced techniques is an essential part of a Selenium WebDriver course, equipping you with the skills to handle such scenarios in automation testing seamlessly.
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. In scenarios where inspecting the properties of elements, such as system-level pop-ups or native OS windows, is not possible, traditional Selenium WebDriver commands fall short.
To address this, the Robot Class provides a powerful solution by simulating keyboard and mouse actions, enabling automation of tasks like handling file upload dialogs, print pop-ups, or even native applications like calculators. This makes Robot Class an essential tool for extending the capabilities of your Selenium scripts to handle complex, non-browser interactions effectively
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 AutoITtool, 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 the 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.
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("[email protected]");
driver.findElement(By.id("email")).sendKeys("[email protected]");//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 the AWT package, the Robot class plays a vital role in generating keyboard and mouse events to interact with OS windows and native applications. This functionality becomes especially useful when dealing with elements that Selenium WebDriver cannot directly control, such as system-level pop-ups, file download alerts, or native applications like the Calculator or Skype.
By simulating human-like interactions, the Robot Class effectively bridges the gap between automation scripts and system-level components, allowing for a seamless integration of operations that extend beyond the capabilities of traditional Selenium commands.
This includes managing tasks such as interacting with file upload dialogs, handling system pop-ups, or automating native applications like calculators or media players. By mimicking real keyboard and mouse events, the Robot Class ensures that automation scripts can tackle complex scenarios with precision and reliability. This ability to control system-level interactions makes it an indispensable tool for testers striving to create robust and comprehensive test suites.
The primary purpose of the Robot class is to enhance Selenium automated test projects built on the Java platform. It empowers testers to handle real-world challenges, such as managing unexpected pop-ups or automating workflows that involve native applications.
By integrating Robot into your automation framework, you can ensure greater reliability and coverage in your test cases. This makes the Robot class an indispensable tool for achieving robust and comprehensive test automation in any Selenium project.
Call to action
Are you looking to take your Selenium automation skills to the next level? The Robot Class in Selenium WebDriver is a powerful tool for handling system-level pop-ups, OS windows, and native applications like calculators or file dialogs. At H2K Infosys, our expert-led training covers these advanced features, helping you build robust and versatile automation frameworks.
Through hands-on projects and real-world examples, you’ll learn how to integrate the Robot Class effectively into your Selenium scripts, ensuring smooth and efficient test execution.
Join H2K Infosys to Boost Your Automation Career
Don’t let system-level challenges slow down your automation progress! H2K Infosys offers comprehensive Selenium training that includes practical applications of the Robot Class to tackle pop-ups, native apps, and other OS-level interactions.
With industry-expert instructors, live projects, and 24/7 support, we ensure you’re equipped to handle even the most complex testing scenarios. Enroll today to gain a competitive edge in the job market and become a sought-after automation testing professional!
One Response