Upload file in Selenium WebDriver using Robot class of Java

In the blogpost, we are going to automate one of the daunting functionality with Selenium WebDriver, and that is File Upload.

WebDriver API is not capable enough to automate file upload functionality single-handedly. We need to get our hands dirty with some of the concepts of Core Java.

We need to certain classes and/or interfaces like Robot, Toolkit, KeyEvent defined under AWT package in Java.

Apart from that, we need to make use of Robot and StringSelection classes as well.

Let us have a look at the code snippet to see the code in action.

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.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UploadDemo {

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

public static void main(String[] args) {

             // location of file

              String filepath = “C:\\Users\\User\\Desktop\\Testing Text.txt”;
             

              setClipboardData(filepath);

              System.setProperty(“webdriver.chrome.driver”,
“chromeDriverPath”);

               WebDriver driver = new FirefoxDriver();
               driver.manage().window().maximize();
               driver.get(“https://www.websupergoo.com/file-upload-1.htm”);

               WebElement fileInput = driver.findElement(By.name(“fileField”));
               fileInput.click();
               System.out.println(“Choose file button clicked”);

              try {
                         Thread.sleep(3000);

                        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);
                       

                        Thread.sleep(3000);

                       driver.findElement(By.xpath(“//form[@action=’file-upload-1.htm’]//input[@name=’submit’]”)).click();
                   

                       System.out.println(” File uploaded… “);
                       Thread.sleep(6000);

                       Alert alert = driver.switchTo().alert();
                      alert.accept();

               } catch (AWTException e) {
                       e.printStackTrace();
               } catch (Exception e) {
                      e.printStackTrace();
               }
               

                       driver.quit();
          }
}

Hope it works for you!!

Cheers 🙂

Advertisements

#automate-file-upload, #robot-class, #upload-file-using-selenium-webdriver