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.
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?
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;
Following code is the required packages to handle Java AWTException.
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;
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;
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is to select the defined string.
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
String filepath = "path of the file to upload";
setClipboardData(filepath);
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");
driver.navigate().to("http://www.helloselenium.com/2015/03/how-to-upload-file-using-java-robot.html");
WebElement fileInput = driver.findElement(By.name("uploadFileInput"));
fileInput.click();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
driver.quit();
driver.close();
1 Comments
ReplyDeleteRobot class in Selenium
What would you like to add in my list? I look forward to reading your comments below.