Selenium File Upload

Selenium 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.

The "ie.fileUploadDialogTimeout" capability is supported by Gridlastic from selenium version 2.48.2.

Use the setFileDetector method in the below example to load files from your local machine or Jenkins/build box. This way you can check in your test upload files and use them regardless if you are using a Gridlastic selenium grid or a local setup.


// NOTE: replace USERNAME:ACCESS_KEY@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.WIN8_1);
        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("http://USERNAME:ACCESS_KEY@SUBDOMAIN.gridlastic.com:80/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.findElement(By.id("qbi")).click();
		driver.findElement(By.linkText("Upload an image")).click();
		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());		 
 }

    public void tearDown() throws Exception {
        driver.quit();
    }
}
NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.
It is important to ensure that "driver.quit()" is always called for proper test execution and creation of video recordings of failed tests.