Robot class enables selenium to use an actual mouse, Actions class in selenium only simulates a mouse, which means Actions class does not move the mouse cursor.
Robot class is very easy to use with the automation process. It can be easily integrated with Java automation frameworks
Selenium does not provide support to handle window based pop-ups (like download popups, upload popups).
Useful Robot Class Methods for selenium
keyRelease method.
We can handle the download popup using
Avoid using the mouse with the robot class when you cannot find the exact location of the buttons. Location of the button or the elements may change due to the size of the screen we have for testing.
Example : Consider the Below download popup.
To Handle the above popUp, follow the below steps:
// PLEASE DO WRITE IMPORT STATEMENTS
public class DownloadUsingRobot {
public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
// open firefox
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
// open web page
driver.get("https://chercher.tech/files/minion.zip");
Thread.sleep(3000);
// create object to robot class
Robot robot = new Robot();
// press tab first time
robot.keyPress(KeyEvent.VK_TAB);
// press tab second time
robot.keyPress(KeyEvent.VK_TAB);
// press enter key
robot.keyPress(KeyEvent.VK_ENTER);
}
}
createScreenCapture takes a screenshot of the given rectangle shape. This method accepts a Rectangle type parameter.
Robot class takes a screenshot irrespective of application if the application is minimized robot class takes a screenshot of the desktop.
// PLEASE DO WRITE IMPORT STATEMENTS
public class RobotClass {
public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
// open firefox
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
// open webpage
driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
Robot robot = new Robot();
int x = 10;
int y = 10;
int width = 500;
int height = 200;
Rectangle area = new Rectangle(x, y, width, height);
BufferedImage bufferedImage = robot.createScreenCapture(area);
File imageFile = new File("D:Robot.png");
ImageIO.write(bufferedImage, "png", imageFile);
}
}
Output :
Full screenshot with Robot class in Selenium We can also take a full screenshot of the Desktop (any application which is active currently).
// PLEASE DO WRITE IMPORT STATEMENTS
public class FullScreenCaptureExample {
public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
// open firefox
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
// open webpage
driver.get("https://chercher.tech/java/practice-pop-ups-selenium-webdriver");
// create object to robt class
Robot robot = new Robot();
// create rectangle for full screenshot
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
// capture screen of the desktop
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
// save the screenshot to local system
ImageIO.write(screenFullImage, "png", new File("D:FullScreenshotRobot.png"));
System.out.println("Full Desktop screenshot saved!");
}
}
Output :
There are more than 40 exceptions present in selenium apart from awt exceptions
Though Robot class helps to handle window based popup, below drawbacks of robot class
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.