File upload using Robots

We have discussed uploading a file using using Webdriver Sendkeys method and Using AutoIT Tool. Now here we will look into an other way of doing file upload using Robot class and StringSelection class in java.

Robot class is used to (generate native system input events) take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through with java code.

There are different methods which robot class uses. Here in the below example we have used 'keyPress' and 'keyRelease' methods.

keyPress - takes keyCode as Parameter and Presses here a given key.
keyrelease - takes keyCode as Parameterand Releases a given key

Both the above methods Throws - IllegalArgumentException, if keycode is not a valid key.

We have defined two methods in the below example along with the test which is used to upload a file.

package com.easy.upload;

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();
        }
    }
}
Selenium Tutorials: 

Comments

Hi,

I am not able to upload image using this method and others also. It gets stuck at image open page.

I wonder if there is a way to do this for remote execution, for example, when using Selenium Grid.
AutoIt and Robot only works if the automation program executes on the browser machine.

It's very nice and helpful article. Thank a lot for help.

To upload file...click on the upload webelement and pass sendkeys through the same element

Hai.. Iam using Robot for uploading file but i got error i.e, VK_CONTROL cannot be resolved or is not a field.

can u people please tell me why this error occurs

use this
import java.awt.event.KeyEvent;

Worked like a magic. Jst to add one point abt which i have faced in my project. After pressing control v need to add wait for 1 or 2 secs so that enter is pressed correctly. If not..Sometimes the enter is not presses after ctrl+v

I need to upload multiple files whose links are stored in excel file and i am trying to make use of robot class to do so. Help

I need to click 'Command' key using robot , how to click?

im getting error as "the file name is not valid" , im using IE 11 Browser and i need to upload file in custom files format

if i select the file manually its uploading. by using robot it is not working

Please help.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.