speaktovasanthi

Mar 8, 20234 min

File Upload Using Robot Class - A Step by Step Guide

Updated: Mar 9, 2023

Contents

Introduction

When we Need Robot Class?

Use Of Robot Class In Selenium

Methods to implement the Robot class

How to implement the Robot class in Selenium?

Keyboard Actions

Mouse Actions

Program for File Upload using Robot Class

Conclusion

Introduction

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)
 
.getAutoDelay()
 
.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)
 
.notify()
 
.setAutoDelay(int ms)
 
.toString()
 
.wait()

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
 
robot.keyPress(KeyEvent.VK_DOWN);
 
//To press Enter key
 
robot.keyPress(KeyEvent.VK_ENTER);
 
//To press the TAB key of keyboard
 
robot.keyPress(KeyEvent.VK_TAB);
 
//To press CAPS Lock key
 
robot.keyPress(keyEvent.VK_CAPS_LOCK);
 
//To press Up key
 
robot.keyPress(keyEvent.VK_UP);

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

//The below code is used to release the Shift Key
 
robot.keyRelease(KeyEvent.VK_SHIFT);

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
 
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK)

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

//This will releases right mouse button
 
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

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);
 
robot.mouseWheel(100);

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

UploadFile.java

package com.vas.keymouse;
 
import org.openqa.selenium.By;
 
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.chrome.ChromeDriver;
 
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.navigate().to("https://blueimp.github.io/jQuery-File-Upload/");
 
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
 
driver.manage().window().maximize();
 
Thread.sleep(5000);
 
driver.findElement(By.cssSelector(".btn.btn-success.fileinput-button")).click();
 

 
Thread.sleep(2000);
 
//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
 
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath,null);
 

 
//Press Control&V to paste the above path
 
robot.keyPress(KeyEvent.VK_CONTROL);
 
robot.keyPress(KeyEvent.VK_V);
 
Thread.sleep(1000);
 

 
//Release Control & V buttons
 
robot.keyRelease(KeyEvent.VK_V);
 
robot.keyRelease(KeyEvent.VK_CONTROL);
 

 
//Press Enter
 
robot.keyPress(KeyEvent.VK_ENTER);
 
//Release Enter
 
robot.keyRelease(KeyEvent.VK_ENTER);
 

 
//wait for the file to get uploaded
 
robot.delay(2000);
 
// Thread.sleep(10000);
 
System.out.println("File Uploaded Successfully");
 
driver.close();
 
}
 
}

When we execute the above java class as above, it navigates to the url “https://blueimp.github.io/jQuery-File-Upload/" 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:

Conclusion

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!!!

    12921
    0