Robot Class in Selenium

Robot Class in Selenium :

Robot Class is available under java.awt.package, Robot class is a Java class which helps the Java developer to emulate the Mouse and Keyboard like actual user, also robot class helps to take screenshot of the desktop

Some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects.

So, we need to use either AutoIT or Robot class in Selenium Webdriver to interact the Windows Dialog boxes like upload and download.

 

Use of Robot Class in Selenium:

  • Robot class emulates real user in front of the computer
  • Using Robot Class we can handle keyboard and mouse events in Selenium.
  • Robot Class can help in uploading / downloading files using selenium web driver.
  • Robot class helps to take screenshots of the desktop
  • Robot class is pure Java code, so we can use it with any java technology

 

Method which are useful in for Selenium :

Methods in Robot Class can be effectively used to do the interaction with popups in Web Applications. Selenium does not provide support to handle browser pop-ups or the native operating system pop-ups. To handle these kind of pop-up, we need help of Robot Class.

Robot class provide a lot of methods to simulate keyboard or mouse actions. Below is a list of major methods that commonly used.

  • keyPress() : Presses a given key. The key should be released using the keyRelease method.¬† Example: robotObj.keyPress(KeyEvent.VK_TAB) : Press down Keyboard tab key.
  • keyRelease() : Simulate keyboard key release. Example: robotObj.keyRelease(KeyEvent.VK_DOWN) : Release up Keyboard down arrow key.
  • mousePress() : Simulate¬† mouse key press. Example : robotObj.mousePress(InputEvent.BUTTON1_DOWN_MASK) : Press down left key of your mouse.
  • mouseRelease() : Simulate mouse key release . Example: robotObj.mouseRelease(InputEvent.BUTTON3_DOWN_MASK) : Release up right key of your mouse.
  • mouseMove() : Moves mouse pointer to given screen coordinates. Example: robotObj.mouseMove(point.getX(), point.getY()) : Move mouse pointer to a point located by X and Y coordinates.

 

Handle Download popUp using Robot class in selenium webdriver:

We can handle the download pop up using Robot class, Just we have to press couple of TABS and ENTER keys.

Avoid using the mouse with robot class when you cannot find the exact location of the buttons. Location of button or the elements may change due to size of the screen we have for testing.

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://www.seleniumhq.org/download/");
	driver.findElement(By.linkText("32 bit Windows IE")).click()
        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);
}

 

Roll Mouse Wheel to scroll a webpage:

public static void main(String[] args) {
	System.setProperty("webdriver.gecko.driver","D:\\PATH\\geckodriver.exe");
	WebDriver driver = new FirefoxDriver();
	driver.get("https://javaseleniumworld.com/robot-class/");
	// Create object of Robot class
	Robot robot = new Robot();
	// Scroll MouseWheel
	robot.mouseWheel(5);
}

 

Capturing full screen:

To capture a screenshot of the whole screen, we need to know the screen size, we can use defaultToolkit class to get the size of the desktop.

To take screenshot you application must be visible on desktop, basically robot class takes screenshot of whatever application is visible in desktop.

public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            String format = "png";
            String fileName = "FullScreenshot." + format;
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
            ImageIO.write(screenFullImage, format, new File(fileName));
            System.out.println("Captured full screenshot");
        } catch (AWTException | IOException ex) {
            System.err.println(ex);
        }
    }

 

Capture Screenshot of Specific Area:

To capture screenshot of a portion of the screen, we need to specify a rectangle region to be captured.

public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            String format = "png";
            String fileName = "PartialScreenshot." + format;
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 3, screenSize.height / 3);
            BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
            ImageIO.write(screenFullImage, format, new File(fileName));
            System.out.println("Specific screenshot saved!");
        } catch (AWTException | IOException ex) {
            System.err.println(ex);
        }
    }

Dis-Advantages of Robot Class :

  • When you use Robot class in your code then you have make sure that no other window is open while execution of the code. Because Robot class will work on active window. Your eclipse should be in the background to the active window
  • When you use keyPress event of this class then you should use keyRelease event also to perform the short keys on to the browser. e.g you want to click Ctrl+V if you forget to add keyRelease event then it will remain pressed and consume memory in the background. Like this
  • You need to pay attention on the order of key release. Consider this example. And please try to understand that what I am trying to say
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

 

Leave a Reply

Your email address will not be published. Required fields are marked *