Selenium Grid File Upload

Selenium grid file upload example code is shown below. Watch out, the sendKeys method is working well for Chrome and Firefox but for Internet Explorer you need to use the capability ie.fileUploadDialogTimeout to give the dialog box extra time to load.

This example code is also available as a maven project on Github

Also see our selenium grid download files example.


// NOTE: replace USERNAME:ACCESS_KEY@HUB_SUBDOMAIN and VIDEO_URL with your credentials found in the Gridlastic dashboard

import junit.framework.TestCase;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class selenium_file_upload extends TestCase {
    private RemoteWebDriver driver;

    public void setUp() throws Exception {
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability("version", "11");
        capabilities.setCapability("platform", Platform.WIN10);
        capabilities.setCapability("platformName", "windows");
        capabilities.setCapability("video", "True");
        capabilities.setCapability("ie.fileUploadDialogTimeout", 10000); // First time open of the internet explorer file upload dialog box is slow.
        
        driver = new RemoteWebDriver(
           new URL("https://USERNAME:ACCESS_KEY@HUB_SUBDOMAIN.gridlastic.com/wd/hub"),
           capabilities);
        driver.setFileDetector(new LocalFileDetector()); // Files will be uploaded from local machine via the selenium grid.
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        System.out.println("GRIDLASTIC VIDEO URL: " + VIDEO_URL + ((RemoteWebDriver) driver).getSessionId()); 
    }

    public void test_google_image_search() throws Exception {
	driver.get("https://www.google.com/ncr");
	driver.findElement(By.linkText("Images")).click();
	driver.findElementByCssSelector("[aria-label='Search by image']").click();;
	driver.findElement(By.linkText("Upload an image")).click();
	Thread.sleep(3000); // slow down for demo purposes
	WebElement inputFilePath = driver.findElement(By.id("qbfile"));
	File uploadedFile = new File("C:/Users/homework/Desktop/googlelogo_color_272x92dp.png"); // local machine file location
	inputFilePath.sendKeys(uploadedFile.getAbsolutePath());	
	Thread.sleep(10000); // slow down for demo purposes
 }

    public void tearDown() throws Exception {
        driver.quit();
    }
}
It is important to ensure that "driver.quit()" is always called for proper test execution and creation of video recordings of failed tests.