Features
< Back to Blog Overview

Uploading with WebDriver

2015-12-11
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

Since a lot of websites have upload functionality, it's important to know that this can be tested via Selenium.

Uploading a file during a test

With Selenium WebDriver it's possible to upload a file from your own computer. In your test, you indicate which file you would like to upload, after which WebDriver will send the file from your computer, base64-encoded, to the Selenium node.


When you want to upload a file during your test via the TestingBot.com selenium grid, you can use the LocalFileDetector feature in WebDriver to upload a file from the machine running your test to our test VMs. Since we guarantee a pristine virtual machine for every test, you can be sure that after every test your uploaded files are destroyed.


An example in Java:

Copy code
import junit.framework.*;
  
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*; 

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;

import java.net.URL;  
  
public class UploadTest extends TestCase {
  
  public static void testUpload() {  
    String apiKey = "key";
    String apiSecret = "secret";

    DesiredCapabilities caps = new DesiredCapabilities();  
    caps.setCapability("browserName", "IE");  
    caps.setCapability("version", "9");  
    caps.setCapability("name", "Test File Upload"); 
    caps.setCapability("platform", "WINDOWS");  
  
    try {
      RemoteWebDriver driver = new RemoteWebDriver(new URL("http://" + apiKey + ":" + apiSecret + "@hub.testingbot.com/wd/hub"), caps);  
      driver.setFileDetector(new LocalFileDetector());
      
      driver.get("http://sl-test.herokuapp.com/guinea_pig/file_upload");
      WebElement upload = driver.findElement(By.id("myfile"));
      upload.sendKeys("~/mypicture.jpg");
      driver.findElement(By.id("submit")).click();
      driver.findElement(By.tagName("img"));
      Assert.assertEquals("mypicture.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText());  
      driver.quit();
    }
    catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
  }  
}

Uploading a file before starting a test

In some cases, you might want to make sure a file is already uploaded on our test VMs before your test starts. With TestingBot, you can specify the location (URL) of the file and the path where you want the file to be placed on our VMs. You can put these details in your desired capabilities (the options you send at the start of the test). TestingBot will then first download the file and place it in the location you want.


The way to do this is by specifying in your desired capabilities:

Copy code

{
browserName: "firefox",
version: 42,
platform: "VISTA",
upload: "http://mywebsite.com/myfile.ext",
uploadFilepath: "C:\\myfile.ext"
}

This will download the file from mywebsite.com/myfile.ext and place it in C:\myfile.ext During your test, you can use the C:\myfile.ext file to test your upload.


The same functionality is also available on our MAC and Linux VMs, where you can store them in the "/tmp" folder for example.


You can find more options for your tests on our test options page.

TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

blank

To keep up with the ever-changing world of software/development and testing, we keep on working to improve TestingBot and add new features. These l...

Read more
blank

Today TestingBot has added a new feature that should help you debug issues you encounter when running automated tests. Suppose you're running a t...

Read more
blank

With Robot Framework you can build easy to read test cases, which can then be run via Selenium WebDriver on our Selenium Grid. It allows using ke...

Read more
blank

This week we've been adding many more browser versions to our list of browsers you can pick from. You can now instantly launch a Chrome browser ...

Read more
blank

Windows 10 is now available on TestingBot to use during both manual and automated testing (Selenium WebDriver). This is the latest OS version avai...

Read more
blank

With our TestingBot Tunnel you can easily and securely test your websites running on your local computer or internal network. When you download a...

Read more