Skip to main content

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 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 capabilities to indicate where TestingBot needs to put the file.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

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

  public static void main(String[] args) throws Exception {
    ChromeOptions options = new ChromeOptions();
    options.setPlatformName("WIN11");
    options.setBrowserVersion("latest");

    Map<String, Object> tbOptions = new HashMap<>();
    tbOptions.put("key", "API_KEY");
    tbOptions.put("secret", "API_SECRET");
    tbOptions.put("name", "Test File Upload");
    tbOptions.put("upload", "https://testingbot.com/assets/logo-head.png");
    tbOptions.put("uploadFilepath", "C:\\test\\logo.png");
    options.setCapability("tb:options", tbOptions);

    RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), options);
    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();
  }
}
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('platformName', 'WIN11')
options.set_capability('browserVersion', 'latest')
options.set_capability('tb:options', {
  'key': 'API_KEY',
  'secret': 'API_SECRET',
  'name': 'Test File Upload',
  'upload': 'https://testingbot.com/assets/logo-head.png',
  'uploadFilepath': 'C:\\test\\logo.png'
})

driver = webdriver.Remote(
    command_executor='https://hub.testingbot.com/wd/hub',
    options=options)
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()
const { Builder, By } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const remote = require('selenium-webdriver/remote');

async function runTest() {
  const options = new chrome.Options();
  options.set('platformName', 'WIN11');
  options.set('browserVersion', 'latest');
  options.set('tb:options', {
    'key': 'API_KEY',
    'secret': 'API_SECRET',
    'name': 'Test File Upload',
    'upload': 'https://testingbot.com/assets/logo-head.png',
    'uploadFilepath': 'C:\\test\\logo.png'
  });

  const driver = await new Builder()
    .usingServer('https://hub.testingbot.com/wd/hub')
    .setChromeOptions(options)
    .build();

  driver.setFileDetector(new remote.FileDetector());

  try {
    await driver.get('http://the-internet.herokuapp.com/upload');
    await driver.findElement(By.id('file-upload')).sendKeys('C:\\test\\logo.png');
    await driver.findElement(By.id('file-submit')).click();
    console.log(await driver.getTitle());
  } finally {
    await driver.quit();
  }
}

runTest();
require 'rubygems'
require 'test/unit'
require 'selenium-webdriver'

class UploadTest < Test::Unit::TestCase
  def setup
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_option('platformName', 'WIN11')
    options.add_option('browserVersion', 'latest')
    options.add_option('tb:options', {
      'key' => 'API_KEY',
      'secret' => 'API_SECRET',
      'name' => 'Test File Upload',
      'upload' => 'https://testingbot.com/assets/logo-head.png',
      'uploadFilepath' => 'C:\\test\\logo.png'
    })

    @driver = Selenium::WebDriver.for(
      :remote,
      url: 'https://hub.testingbot.com/wd/hub',
      options: options
    )
  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'
    @driver.find_element(:id, 'file-submit').click
    assert_equal 'logo.png', @driver.find_element(:id, 'uploaded-files').text
  end

  def teardown
    @driver.quit
  end
end
<?php

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

$options = new ChromeOptions();
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability('platformName', 'WIN11');
$capabilities->setCapability('browserVersion', 'latest');
$capabilities->setCapability('tb:options', [
  'key' => 'API_KEY',
  'secret' => 'API_SECRET',
  'name' => 'Test File Upload',
  'upload' => 'https://testingbot.com/assets/logo-head.png',
  'uploadFilepath' => 'C:\\test\\logo.png'
]);
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = RemoteWebDriver::create(
  'https://hub.testingbot.com/wd/hub',
  $capabilities
);
$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();
?>
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ChromeOptions options = new ChromeOptions();
            options.PlatformName = "WIN11";
            options.BrowserVersion = "latest";

            var tbOptions = new Dictionary<string, object>
            {
                ["key"] = "API_KEY",
                ["secret"] = "API_SECRET",
                ["name"] = "Test File Upload",
                ["upload"] = "https://testingbot.com/assets/logo-head.png",
                ["uploadFilepath"] = "C:\\test\\logo.png"
            };
            options.AddAdditionalOption("tb:options", tbOptions);

            IWebDriver driver = new RemoteWebDriver(
                new Uri("https://hub.testingbot.com/wd/hub"), options);

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

            driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/upload");
            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.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

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

  public static void main(String[] args) throws Exception {
    ChromeOptions options = new ChromeOptions();
    options.setPlatformName("WIN11");
    options.setBrowserVersion("latest");

    Map<String, Object> tbOptions = new HashMap<>();
    tbOptions.put("key", "API_KEY");
    tbOptions.put("secret", "API_SECRET");
    tbOptions.put("name", "Test File Upload");
    options.setCapability("tb:options", tbOptions);

    RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), options);
    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();
  }
}
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('platformName', 'WIN11')
options.set_capability('browserVersion', 'latest')
options.set_capability('tb:options', {
  'key': 'API_KEY',
  'secret': 'API_SECRET',
  'name': 'Test File Upload'
})

driver = webdriver.Remote(
    command_executor='https://hub.testingbot.com/wd/hub',
    options=options)
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()
const { Builder, By } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const remote = require('selenium-webdriver/remote');

async function runTest() {
  const options = new chrome.Options();
  options.set('platformName', 'WIN11');
  options.set('browserVersion', 'latest');
  options.set('tb:options', {
    'key': 'API_KEY',
    'secret': 'API_SECRET',
    'name': 'Test File Upload'
  });

  const driver = await new Builder()
    .usingServer('https://hub.testingbot.com/wd/hub')
    .setChromeOptions(options)
    .build();

  driver.setFileDetector(new remote.FileDetector());

  try {
    await driver.get('http://the-internet.herokuapp.com/upload');
    await driver.findElement(By.id('file-upload')).sendKeys('/Users/test/logo.png');
    await driver.findElement(By.id('file-submit')).click();
    console.log(await driver.getTitle());
  } finally {
    await driver.quit();
  }
}

runTest();
require 'rubygems'
require 'test/unit'
require 'selenium-webdriver'

class UploadTest < Test::Unit::TestCase
  def setup
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_option('platformName', 'WIN11')
    options.add_option('browserVersion', 'latest')
    options.add_option('tb:options', {
      'key' => 'API_KEY',
      'secret' => 'API_SECRET',
      'name' => 'Test File Upload'
    })

    @driver = Selenium::WebDriver.for(
      :remote,
      url: 'https://hub.testingbot.com/wd/hub',
      options: options
    )
  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_equal 'logo.png', @driver.find_element(:id, 'uploaded-files').text
  end

  def teardown
    @driver.quit
  end
end
<?php

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

$options = new ChromeOptions();
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability('platformName', 'WIN11');
$capabilities->setCapability('browserVersion', 'latest');
$capabilities->setCapability('tb:options', [
  'key' => 'API_KEY',
  'secret' => 'API_SECRET',
  'name' => 'Test File Upload'
]);
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

$driver = RemoteWebDriver::create(
  'https://hub.testingbot.com/wd/hub',
  $capabilities
);
$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();
?>
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ChromeOptions options = new ChromeOptions();
            options.PlatformName = "WIN11";
            options.BrowserVersion = "latest";

            var tbOptions = new Dictionary<string, object>
            {
                ["key"] = "API_KEY",
                ["secret"] = "API_SECRET",
                ["name"] = "Test File Upload"
            };
            options.AddAdditionalOption("tb:options", tbOptions);

            IWebDriver driver = new RemoteWebDriver(
                new Uri("https://hub.testingbot.com/wd/hub"), options);

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

            driver.Navigate().GoToUrl("http://the-internet.herokuapp.com/upload");
            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
    options = Selenium::WebDriver::Firefox::Options.new
    options.add_option('platformName', 'WIN11')
    options.add_option('browserVersion', 'latest')
    options.add_option('tb:options', {
      'key' => 'API_KEY',
      'secret' => 'API_SECRET',
      'name' => 'Test File Upload',
      'upload' => 'tb://.....',
      'uploadFilepath' => 'C:\\test\\file.ext'
    })

    @driver = Selenium::WebDriver.for(
      :remote,
      url: 'https://hub.testingbot.com/wd/hub',
      options: options
    )
  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'
    @driver.find_element(:id, 'file-submit').click
    assert_equal 'logo.png', @driver.find_element(:id, 'uploaded-files').text
  end

  def teardown
    @driver.quit
  end
end
Was this page helpful?

Looking for More Help?

Have questions or need more information?
You can reach us via the following channels: