Features

Uploading with Selenium

With Selenium WebDriver it's possible to test file uploads. You can either specify a file that needs to be uploaded at the start of your test, or upload the file during your test.

1. Upload a file before your test starts

With TestingBot you can specify a URL in your desired capabilities which links to the file you want to be present on our test VMs.
TestingBot will then first download the URL and save it on the test VM so your test can use the file.


You specify the download URL as upload and use uploadFilePath in your desired capabilities to indicate where TestingBot needs to put the file.


require 'rubygems'
require "test/unit"
require 'selenium-webdriver'
class UploadTest < Test::Unit::TestCase
   def setup
      caps = Selenium::WebDriver::Remote::Capabilities.chrome
      caps.version = "latest"
      caps.platform = "WIN10"
      caps[:name] = "Test File Upload"
      caps[:upload] = "https://testingbot.com/assets/logo-head.png"
      caps[:uploadFilepath] = "C:\\test\\logo.png" # use C:\test\filename.ext on Windows and /tmp/filename.ext on Linux/OSX
      @driver = Selenium::WebDriver.for(
        :remote,
        :url => "https://key:secret@hub.testingbot.com/wd/hub",
        :desired_capabilities => caps)
   end

   def test_upload
      @driver.navigate.to "http://the-internet.herokuapp.com/upload"
      element = @driver.find_element(:id, 'file-upload')
      element.send_keys   "C:\\test\\logo.png" # use C:\test on Windows and /tmp on Linux/OSX
      @driver.find_element(:id, "file-submit").click
      assert "logo.png" == @driver.find_element(:id, "uploaded-files").text
  end

  def teardown
      @driver.quit
  end

end
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {
  public static final String URL = "https://key:secret@hub.testingbot.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browserName", "chrome");
    caps.setCapability("platform", "WIN10");
    caps.setCapability("version", "latest");
    caps.setCapability("name", "Test File Upload");
    caps.setCapability("upload", "https://testingbot.com/assets/logo-head.png");
    caps.setCapability("uploadFilepath", "C:\\test\\logo.png");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.setFileDetector(new LocalFileDetector());
    driver.get("http://the-internet.herokuapp.com/upload");
    driver.findElement(By.id("file-upload")).sendKeys("C:\\test\\logo.png");
    driver.findElement(By.id("file-submit")).click();
    driver.quit();
  }
}
<?php

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;
  use Facebook\WebDriver\Remote\LocalFileDetector;

  $caps = array(
   'browserName': 'chrome',
   'version': 'latest',
   'platform': 'WIN10',
   'name': 'Test File Upload',
   'upload': 'https://testingbot.com/assets/logo-head.png',
   'uploadFilepath': 'C:\\test\\logo.png'
  );

  $driver = RemoteWebDriver::create(
    "https://key:secret@hub.testingbot.com/wd/hub",
    $caps
  );
  $driver->get("http://the-internet.herokuapp.com/upload");
  $file_input = $driver->findElement(WebDriverBy::id("file-upload"));
  $file_input->setFileDetector(new LocalFileDetector());
  $file_input->sendKeys("C:\\test\\logo.png");

  $driver->findElement(WebDriverBy::id("file-submit"))->click();
  sleep(5);
  $driver->quit();
?>
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_caps = {
 'browserName': 'chrome',
 'version': 'latest',
 'platform': 'WIN10',
 'name': 'Test File Upload',
 'upload': 'https://testingbot.com/assets/logo-head.png',
 'uploadFilepath': 'C:\\test\\logo.png'
}

driver = webdriver.Remote(
    command_executor='https://key:secret@hub.testingbot.com/wd/hub',
    desired_capabilities=desired_caps)
driver.get('http://the-internet.herokuapp.com/upload')
driver.find_element_by_id('file-upload').send_keys('C:\\test\\logo.png')
driver.find_element_by_id('file-submit').click()
driver.quit()
var webdriver = require('selenium-webdriver');
var remote = require('selenium-webdriver/remote');

var capabilities = {
  'browserName': 'chrome',
  'version': 'latest',
  'platform': 'WIN10',
  'name': 'Test File Upload',
  'upload': 'https://testingbot.com/assets/logo-head.png',
  'uploadFilepath': 'C:\\test\\logo.png',
  'client_key': 'key',
  'client_secret': 'secret'
};

var driver = new webdriver.Builder().
  usingServer('https://hub.testingbot.com/wd/hub').
  withCapabilities(capabilities).
  build();

var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
    return driver.takeScreenshot().then(function(data) {
        fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
            if(err) throw err;
        });
    })
};

driver.setFileDetector(new remote.FileDetector);

driver.get('http://the-internet.herokuapp.com/upload').then(function(){
  driver.findElement(webdriver.By.id('file-upload')).sendKeys('C:\\test\\logo.png').then(function(){
    driver.findElement(webdriver.By.id('file-submit')).click().then(function(){
      driver.getTitle().then(function(title) {
        console.log(title);
        driver.quit();
      });
    });
  });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Text;

namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver;
            ChromeOptions capability = new ChromeOptions();

            capability.AddAdditionalCapability("browserName", "Chrome", true);
            capability.AddAdditionalCapability("version", "latest", true);
            capability.AddAdditionalCapability("platform", "WIN10", true);
            capability.AddAdditionalCapability("client_key", "key", true);
            capability.AddAdditionalCapability("client_secret", "secret", true);
            capability.AddAdditionalCapability("name", "Test File Upload", true);
            capability.AddAdditionalCapability("upload", "https://testingbot.com/assets/logo-head.png", true);
            capability.AddAdditionalCapability("uploadFilepath", "C:\\test\\logo.png", true);

            driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), capability);
            driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/upload");

            var allowsDetection = driver as IAllowsFileDetection;
            allowsDetection.FileDetector = new LocalFileDetector();

            IWebElement upload = driver.FindElement(By.Id("file-upload"));
            upload.SendKeys("C:\\test\\logo.png");

            driver.FindElement(By.Id("file-submit")).Click();
            driver.Quit();
        }
    }
}

2. Upload a file during your test

You can use LocalFileDetector to upload a file from your local computer to the remote VM/device during your test.


In the example below, we'll upload an image, residing on your own computer /Users/test/logo.png, via the remote VM/device in the TestingBot cloud to a website.


require 'rubygems'
require "test/unit"
require 'selenium-webdriver'
class UploadTest < Test::Unit::TestCase
   def setup
      caps = Selenium::WebDriver::Remote::Capabilities.chrome
      caps.version = "latest"
      caps.platform = "WIN10"
      caps[:name] = "Test File Upload"
      @driver = Selenium::WebDriver.for(
        :remote,
        :url => "https://key:secret@hub.testingbot.com/wd/hub",
        :desired_capabilities => caps)
   end

   def test_upload
      @driver.navigate.to "http://the-internet.herokuapp.com/upload"
      element = @driver.find_element(:id, 'file-upload')
      element.send_keys "/Users/test/logo.png"
      @driver.find_element(:id, "file-submit").click
      assert "logo.png" == @driver.find_element(:id, "uploaded-files").text
  end

  def teardown
      @driver.quit
  end

end
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {
  public static final String URL = "https://key:secret@hub.testingbot.com/wd/hub";

  public static void main(String[] args) throws Exception {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browserName", "chrome");
    caps.setCapability("platform", "WIN10");
    caps.setCapability("version", "latest");
    caps.setCapability("name", "Test File Upload");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.setFileDetector(new LocalFileDetector());
    driver.get("http://the-internet.herokuapp.com/upload");
    driver.findElement(By.id("file-upload")).sendKeys("/Users/test/logo.png");
    driver.findElement(By.id("file-submit")).click();
    driver.quit();
  }
}
<?php

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;
  use Facebook\WebDriver\Remote\LocalFileDetector;

  $caps = array(
   'browserName': 'chrome',
   'version': 'latest',
   'platform': 'WIN10',
   'name': 'Test File Upload'
  );

  $driver = RemoteWebDriver::create(
    "https://key:secret@hub.testingbot.com/wd/hub",
    $caps
  );
  $driver->get("http://the-internet.herokuapp.com/upload");
  $file_input = $driver->findElement(WebDriverBy::id("file-upload"));
  $file_input->setFileDetector(new LocalFileDetector());
  $file_input->sendKeys("/Users/test/logo.png");

  $driver->findElement(WebDriverBy::id("file-submit"))->click();
  sleep(5);
  $driver->quit();
?>
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_caps = {
 'browserName': 'chrome',
 'version': 'latest',
 'platform': 'WIN10',
 'name': 'Test File Upload'
}

driver = webdriver.Remote(
    command_executor='https://key:secret@hub.testingbot.com/wd/hub',
    desired_capabilities=desired_caps)
driver.get('http://the-internet.herokuapp.com/upload')
driver.find_element_by_id('file-upload').send_keys('/Users/test/logo.png')
driver.find_element_by_id('file-submit').click()
driver.quit()
var webdriver = require('selenium-webdriver');
var remote = require('selenium-webdriver/remote');

var capabilities = {
  'browserName': 'chrome',
  'version': 'latest',
  'platform': 'WIN10',
  'name': 'Test File Upload',
  'client_key': 'key',
  'client_secret': 'secret'
};

var driver = new webdriver.Builder().
  usingServer('https://hub.testingbot.com/wd/hub').
  withCapabilities(capabilities).
  build();

var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
    return driver.takeScreenshot().then(function(data) {
        fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
            if(err) throw err;
        });
    })
};

driver.setFileDetector(new remote.FileDetector);

driver.get('http://the-internet.herokuapp.com/upload').then(function(){
  driver.findElement(webdriver.By.id('file-upload')).sendKeys('/Users/test/logo.png').then(function(){
    driver.findElement(webdriver.By.id('file-submit')).click().then(function(){
      driver.getTitle().then(function(title) {
        console.log(title);
        driver.quit();
      });
    });
  });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Text;

namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver;
            ChromeOptions capability = new ChromeOptions();

            capability.AddAdditionalCapability("browserName", "Chrome", true);
            capability.AddAdditionalCapability("version", "latest", true);
            capability.AddAdditionalCapability("platform", "WIN10", true);
            capability.AddAdditionalCapability("client_key", "key", true);
            capability.AddAdditionalCapability("client_secret", "secret", true);
            capability.AddAdditionalCapability("name", "Test File Upload", true);

            driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), capability);
            driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/upload");

            var allowsDetection = driver as IAllowsFileDetection;
            allowsDetection.FileDetector = new LocalFileDetector();

            IWebElement upload = driver.FindElement(By.Id("file-upload"));
            upload.SendKeys("/Users/test/logo.png");

            driver.FindElement(By.Id("file-submit")).Click();
            driver.Quit();
        }
    }
}

TestingBot Storage

With TestingBot Storage, you can upload your files on our servers.
The advantage of this is that our test VMs can immediately download your file from our own network, which is much faster than downloading from the public internet.


To upload your file with TestingBot Storage, please see the API upload examples.


An example of using the uploaded file via TestingBot Storage:


require 'rubygems'
require "test/unit"
require 'selenium-webdriver'
class UploadTest < Test::Unit::TestCase
   def setup
      caps = Selenium::WebDriver::Remote::Capabilities.firefox
      caps.version = "latest"
      caps.platform = :WINDOWS
      caps[:name] = "Test File Upload"
      caps[:upload] = "tb://....." # the unique hash you got back from TestingBot Storage
      caps[:uploadFilepath] = "C:\\test\\file.ext" # use C:\test\filename.ext on Windows and /tmp/filename.ext on Linux/OSX
      @driver = Selenium::WebDriver.for(
        :remote,
        :url => "https://key:secret@hub.testingbot.com/wd/hub",
        :desired_capabilities => caps)
   end

   def test_upload
      @driver.navigate.to "http://the-internet.herokuapp.com/upload"
      element = @driver.find_element(:id, 'file-upload')
      element.send_keys   "C:\\test\\logo.png" # use C:\test on Windows and /tmp on Linux/OSX
      @driver.find_element(:id, "file-submit").click
      assert "logo.png" == @driver.find_element(:id, "uploaded-files").text
  end

  def teardown
      @driver.quit
  end

end