What is Robot Class and How to use it In Selenium Webdriver?

Reading Time: 3 minutes

Hello People, As you know Robots help in managing all the activities like performing the task within the specified time, handling the mouse functions and the keyboard functions, and many more.

Automation Minds: Type Text Using Java Robot Class

What is Robot Class?

To perform any action on a web element, we need a locator for the element. But Windows pop-ups don’t have any locators, as they are not part of the webpage, they are native OS pop-ups. To handle such pop-ups we need the Robot class. The primary purpose of this class is to facilitate automated testing for Java platform implementations. For instance, This class is basically an inbuilt class in java itself and we don’t need to download any third-party APIs. It is used to simulate manual operations of a real-time keyboard and mouse. In simple terms, I would say this class provides control over the mouse and keyboard devices.

 For instance, if you are trying to download an Email Attachment, a Windows pop-up, ‘Save Attachment’ prompts to specify Download Location, appears. It is nothing but a native OS pop-up.

Methods for Implementing Robot Class

Robot Framework - Working With Dropdown
  • KeyPress(): Uses to press any key.
    Ex: robot.keyPress(keyEvent.VK_UP).
  • KeyRelease(): Use to release the pressed key on the keyboard. For instance, the pressed caps lock key.
    Ex: robot.keyRelease(keyEvent.VK_CAPS_LOCK);
  • MouseMove(): Calls when you want to move the mouse pointer in the X and Y coordinates
    Ex: robot.mouseMove(coordinates.get.X(),coordinates.get.Y());
  • MousePress(): Use press the left button of the mouse.
    Ex: robot.mousePress(InputEvent.BUTTON1_MASK);
  • MouseRelease(): Helps in releasing the pressed button of the mouse.
    Ex: robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK) 

 

How to implement the Robot class in Selenium

Firstly, invoke the chrome browser and import all the packages required. Link the respective browser driver to the ChromeDriver and specify the path.

CV
// Launch Chrome
WebDriver driver = new ChromeDriver();

Let’s hit the URL of the corresponding webpage.

driver.get("https://www.knoldus.com/home");

After this Search for the element on the web page using the element locators. We use the Robot class to handle pop-ups, using this we create an instance of the Class in the code say Robot robot = new Robot(). Robot class is present in the AWT package of JDK.

 

Let us see the Code Snippet:-

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.awt.*;
import java.awt.event.KeyEvent;
public class robotClass {
    public static void main(String[] args) throws AWTException, InterruptedException {
        System.setProperty("webdriver.chrome.driver", "/home/knoldus/Downloads/Ajax handling/browser driver/chromedriver_linux64/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.knoldus.com/home"); // sample url
        driver.findElement(By.linkText("Services")).click();
        Robot robot = new Robot();  // Robot class throws AWT Exception
        Thread.sleep(4000);
        robot.keyPress(KeyEvent.VK_DOWN);// moving the keyboard key down.
        Thread.sleep(4000);
        robot.keyPress(KeyEvent.VK_TAB);
        Thread.sleep(4000);
        System.out.println("a");
        robot.keyPress(KeyEvent.VK_TAB);//pressing the tab key
        Thread.sleep(4000);
        System.out.println("b");
        robot.keyPress(KeyEvent.VK_TAB);
        Thread.sleep(4000);
        System.out.println("c");
        robot.mouseMove(30, 100);//moving the mouse.
        Thread.sleep(4000);
        System.out.println("d");
    }
}

How to Upload a file using Robot Class?

When we start automating web applications then we might get different scenarios where we need to upload a single file or multiple files to continue the flow.

If we click on file upload then the filer uploader model will appear which will ask us to upload a file. The file uploader is windows control which Selenium can’t handle so we need to take the help of another library or approach.

Upload a file in Selenium using sendKeys(), AutoIT tool and Robot Class -  Tutorials Hut

The below Code snippet demonstrate this:-

package basicsOfSelenium;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FileUploadInSelenium_UsingRobotClass {
 public static void main(String[] args) throws AWTException {
  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("URL of the webpage");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  //open upload window
     driver.findElement(By.xpath("//*[@id='uploadfile_0']")).click();
     //put path to your image in a clipboard
     StringSelection ss = new StringSelection("home/knoldus/Pictures");
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
     //imitate mouse events like ENTER, CTRL+C, CTRL+V
     Robot robot = new Robot();
     robot.delay(250);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
     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.delay(90);
     robot.keyRelease(KeyEvent.VK_ENTER);
 }
}

Hopefully, how to utilize Robot Class within a Selenium + Java framework is clear. I wish everybody finds this article interesting and helpful in learning the concept.

For more references visit:

https://www.selenium.dev/documentation/webdriver/


Knoldus-blog-footer-image

 

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading