Ankit_Add

Wednesday, September 23, 2015

Use of Robot Class In Selenium WebDriver For Automation Purpose

During automation activity Selenium often find difficulty to find an object and perform actions which is related to mouse or keyboard type of activity. It is not easy to perform keyboard or mouse events using selenium commands. There are many specific scenarios where any selenium command is launched but actual event did not fired. This result that execution of test is stopped and it reports some error.


Here Java Robot Class come in picture. Using Robot Class we can simulate keyboard and mouse events in Selenium. This class is very easy to use with automation process. It can be easily integrate with current automation framework.

Robot Class is available under java.awt.package.


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. This is also used while we need to handle file upload and download activity using selenium webDriver.


The activity of file upload can also be handled using AutoIT. AutoIT is a tool that can automate the windows GUI. It generates an '.exe' file which is used by selenium script for file upload or download activity.


Some of the popular methods under Robot Class are:

.keyPress();

.mousePress();

.mouseMove();

.keyRelease();

.mouseRelease();




Here is an example to learn more about Robot Class


import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class UploadFileRobot {

 String URL = "application URL";
@Test
public void testUpload() throws InterruptedException
 {
 WebDriver  driver = new FirefoxDriver();
driver.get(URL);
WebElement element = driver.findElement(By.name("uploadfile"));
element.click();
uploadFile("path to the file");
Thread.sleep(2000);
 }

/**      * This method will set any parameter string to the system's clipboard.      */
public static void setClipboardData(String string) {
//StringSelection is a class that can be used for copy and paste operations.
    StringSelection stringSelection = new StringSelection(string);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
 }

public static void uploadFile(String fileLocation) {
        try {
        //Setting clipboard with file location
            setClipboardData(fileLocation);
            //native key strokes for CTRL, V and ENTER keys
            Robot robot = new Robot();

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (Exception exp) {
        exp.printStackTrace();
        }
    }
}

1 comment:

  1. Nice Article, Please do post this kind of posts more, thanks.
    I wish you might like to have a look at my article http://javaseleniumworld.com/robot-class/

    ReplyDelete