Sameera De Silva
2 min readJan 3, 2020

How to upload a file using Javatoolkit ? because sendkeys on the field is not working and I don’t want to use AutoIT

If you want to upload a file in a field where driver.sendkeys(“filepath”) is not working , you can use Javatoolkit with Java Robot keyEvents

Free video tutorial- https://www.youtube.com/watch?v=WdtqrVeN1Mk&t=19s

Please see the below code -

package com.webdriver.blogs;import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class FileUploadUsingJavaToolKitExample {WebDriver driver;@BeforeClass()
public void setUp() {
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\Jar_files\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//To start Chrome in Maximized browser window
options.addArguments("start-maximized");
//To remove Chrome is being controlled by automated test software
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

driver.manage().window().maximize();
}

@Test(priority=1)
public void navigateToWebSiteAndUploadFile() throws UnsupportedFlavorException, IOException, InterruptedException, AWTException {

// Create a file object
File f = new File("resources\\DemoUpload.txt");
// Get the absolute path of file f
String absoluteFilePath = f.getAbsolutePath();
//Copy the file path to clipboard
StringSelection autoCopiedFilePath = new StringSelection(absoluteFilePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(autoCopiedFilePath, null);

//Navigate to the URL
driver.get("https://codepen.io/rcass/pen/MmYwEp");
driver.switchTo().frame("result"); //switching the frame by name
//Click on a button which opens the popup
driver.findElement(By.xpath("//input[@id='fileToUpload']")).click();
Thread.sleep(2000);
//This will paste the file path and name in the file explorer by pressing Ctrl +V combination.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Pause should be used here to perform the action properly and release the Ctrl +V keys
Thread.sleep(2000);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(8000);
//press enter key after giving the file path.
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(5000);
}
@AfterSuite()
public void shutDown() {
driver.close();
driver.quit();
try {
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("***************End of testing shutdown happend**************************");
}
}

No responses yet