top of page

File Upload Using Robot Class - A Step by Step Guide



Robot Class in Selenium is used to enable automated testing for implementations of Java platform. It generates input events in native systems for test automation, self-running demos and other applications where users need control over mouse and keyboard.

Selenium scripts use Robot class for automating the browser and desktop pop-ups.

The Robot class provides methods that can be used to simulate keyboard and mouse actions, e.g., simulation on OS popups/alerts and even on OS applications like Calculator, Notepad, etc.

Robot class is easy to implement and it can be easily integrated with an automated framework. It is present in the AWT package of JDK.

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations

When we Need Robot Class?

· When the user needs to handle alert pop-ups on a webpage, or

· User needs to enter text on the pop-ups with a combination of modifier keys such as Alt, Shift, etc.

.User wants to capture screenshot of webpage

.User wants to upload/download file

Use Of Robot Class In Selenium

When and where we can use Robot Class in Java to enhance the functionality effectively in an automation framework?

As we all know that Selenium can be used to automate web applications. These web applications in Selenium use the underlying web object (locators) of the web app to determine the position on the web page and operate accordingly. Now in certain cases, we see that we need to handle windows authentication pop-up or any other windows pop-up while automating on the Windows operating system.

Selenium cannot automate Windows actions but we can use Robot Class in Java to accomplish the task.

Methods to implement the Robot class

The below are some of the commonly used methods in Robot Class:

//commonly used methods in Robot Class:
.createMultiResolutionScreenCapture(Rectangle screenRect)
.createScreenCapture(Rectangle screenRect)
.delay(int ms)
.equals(Object obj)
.getPixelColor(int x,int y)
.keyPress(int keycode)
.keyRelease(int keycode)
.mouseMove(int x,int y)
.mousePress(int buttons)
.mouseRelease(int buttons)
.mouseWheel(int wheelAmt)
.setAutoDelay(int ms)

How to implement the Robot class in Selenium?

To handle the pop-ups we use Robot class, using this we create an instance of Robot Class in the code say Robot robot = new Robot().

Keyboard Actions

1) KeyPress(): This method is called when you want to press any key

//To press down arrow key on the Keyboard
//To press Enter key
//To press the TAB key of keyboard
//To press CAPS Lock key
//To press Up key

2) KeyRelease(): This method is used to release the pressed key on the keyboard

//The below code is used to release the Shift Key

Mouse Actions

  1. MouseMove(): This method is used to move the mouse pointer from the current position to the X and Y intersection point. It takes the x and y coordinates as its parameters

//This moves to mouse to the x and y position
robot.mouseMove(coordinates.getX(), coordinates.getY());
robot.mouseMove(640, 360);

2) MousePress(): This method is used to press the mouse button.

//// This will press Left mouse button

3) MouseRelease(): This method is used to release the mouse button.

//This will releases right mouse button

4) MouseWheel(): This method rotates the scroll wheel of the mouse on wheel-equipped mode

//This will move the mouse scroll
robot.mouseWheel(int wheelAmt);

Program for File Upload using Robot Class

When we want to do file upload in automation, we can implement it using Robot Class methods.

Let us see an example to upload file using Robot Class using below steps:

Step 1:Create an Object for the respective browser using WebDriver

Step 2: Navigate to the URL of the corresponding webpage

Step 3: Find the element on the web page using the element locators

Step 4: To handle the pop-ups we use Robot class, using this we create an instance of Robot Class in the code say Robot robot = new Robot().

Step 5: Store the path of the file to be uploaded using StringSelection Class Object

Step 6: Copy the above path to Clipboard

Step 7: Press Control&V to paste the above path and Release Control & V buttons

Step 8: Press Enter key and Release the same

Step 9: Wait for the file to get uploaded

package com.vas.keymouse;
import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import java.awt.*;
 import java.awt.datatransfer.StringSelection;
 import java.awt.event.KeyEvent;
 import java.util.concurrent.TimeUnit;
public class UploadFile {
 public static void main(String[] args) throws AWTException, InterruptedException {
  WebDriver driver = new ChromeDriver();
  driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
  //Create object for Robot class
  Robot robot=new Robot();
  //Store the path of the file to be uploaded using StringSelection Class Object
  StringSelection filepath=new StringSelection("D:\\profile.jpg");
  //Copy above path to Clipboard
  //Press Control&V to paste the above path
  //Release Control & V buttons
  //Press Enter
  //Release Enter
  //wait for the file to get uploaded
 // Thread.sleep(10000);
  System.out.println("File Uploaded Successfully");

When we execute the above java class as above, it navigates to the url “" and clicks “Add files” button

And pastes the file location as below:

Now, we can see the file “profile.jpg” is uploaded in the website as intended:

The console output screen is below:


From this blog, you would have got an understanding of how to do file upload and handle Keyboard and Mouse Actions using Robot Class. We will see how to capture screenshot using Robot Class in my other blog "How-to-capture-screenshots-using-robot-class". Happy Testing!!!

1,420 views1 comment

1 comentário

Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
02 de mai. de 2023

I have a question, If i try with robot method, will it work on pipeline as well? if yes, how to pass the path of the file?

bottom of page