HOW TO UPLOAD FILE USING JAVA ROBOT CLASS AND SELENIUM WEBDRIVER?

In this blog, I am going to explain about "How to upload file using Java Robot Class and Selenium WebDriver?". To perform same I will use below HTML form which contains a file type input to choose a file from system.

File to upload:


Below is the example Java program to upload file using Java Robot Class and Selenium Webdriver. This example program is written for the above HTML form.
Read more about HOW TO OPEN EXECUTABLE (.EXE) APPLICATION USING JAVA?
Read more about WHAT IS ROBOT CLASS IN JAVA?
Read more about WHAT IS TOOLKIT CLASS IN JAVA?
Read more about HOW TO SET A STRING TO CLIPBOARD DATA USING JAVA?



Java Source code:

package com.helloselenium.selenium.test;

import java.awt.AWTException;
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;

public class UploadFileUsingJavaRobot{
 
 public static void setClipboardData(String string) {
    StringSelection stringSelection = new StringSelection(string);
    Toolkit.getDefaultToolkit().getSystemClipboard()
             .setContents(stringSelection, null);
 }

 public static void main(String[] args) { 
  
  String filepath = "path of the file to upload";  
  setClipboardData(filepath);

  WebDriver driver = new FirefoxDriver();

  driver.manage().window().maximize();        
  driver.get("http://www.helloselenium.com/2015/03/how-to-upload-file-using-java-robot.html");

  WebElement fileInput = driver.findElement(By.name("uploadFileInput"));
  fileInput.click();

  try {
 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 (AWTException e) {
        e.printStackTrace();
      }
  
  driver.quit();
 }

}

Below are the line by line code explanation for above Java program:




package com.helloselenium.selenium.test;
Above code is package structure for current program file.
Following code is the required packages to handle Java AWTException.
import java.awt.AWTException;
Following code is the required packages for Java AWT Robot class.
import java.awt.Robot;
Following code is the required packages for Java AWT Toolkit class.
import java.awt.Toolkit;
Following code is the required packages for string selection.
import java.awt.datatransfer.StringSelection;
Following code is the required packages to Java AWT KeyEvent class.
import java.awt.event.KeyEvent;
Following code is the required packages for Selenium Webdriver to define the element locator type.
import org.openqa.selenium.By;

Following code is the required packages for Selenium Webdriver.
import org.openqa.selenium.WebDriver;

Following code is the required packages for Selenium Webelement.
import org.openqa.selenium.WebElement;
Following code is the required packages for Firefox browser.
import org.openqa.selenium.firefox.FirefoxDriver;
Above both required packages are available in downloaded selenium server jar file.
Following code is to select the defined string.
StringSelection stringSelection = new StringSelection(string);
Following code is to set clipboard data for selected string.
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Following code is to set the filepath, you can replace this with your test filepath.
String filepath = "path of the file to upload"; 
Following code is to use the above created method.
setClipboardData(filepath); 
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();




Following code is to open the hello selenium blog page in browser.
driver.get("http://www.helloselenium.com/2015/03/how-to-upload-file-using-java-robot.html");
You can also use the following code is to open the hello selenium blog page in browser.
driver.navigate().to("http://www.helloselenium.com/2015/03/how-to-upload-file-using-java-robot.html");
Following code is to find the file input type webelement on the page.
WebElement fileInput = driver.findElement(By.name("uploadFileInput"));
Following code is to click on the file input.
fileInput.click();
Following code is to define new robot object.
Robot robot = new Robot();
Following code is to press CTRL key.
robot.keyPress(KeyEvent.VK_CONTROL);
Following code is to press V key.
robot.keyPress(KeyEvent.VK_V); 
Following code is to release CTRL key.
robot.keyRelease(KeyEvent.VK_V);
Following code is to release V key.
robot.keyRelease(KeyEvent.VK_CONTROL); 
Following code is to quit the webdriver instance.
driver.quit();
You can also use the following code is to close the webdriver instance.
driver.close();


Post a Comment

1 Comments


What would you like to add in my list? I look forward to reading your comments below.