Skip to main content

Test File Downloads

TestingBot provides custom Selenium WebDriver commands to verify if a file was downloaded during a test automation session.
Below you'll find examples on how to:

All functions below can be used with a fileName argument, or without an argument.
If you do not specify a specific filename, TestingBot will automatically select the most recent downloaded file.

Download files on remote desktop instances during your test

As part of your test automation flow, you might want to test download functionality of your webapp.
In the example below, we'll first show you how to instruct the remote TestingBot browser to download a file on the TestingBot virtual machine.

ChromeOptions options = new ChromeOptions();
options.setPlatformName("WIN11");
options.setBrowserVersion("latest");

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

WebDriver driver = new RemoteWebDriver(new URL("https://hub.testingbot.com/wd/hub"), options);
driver.get("https://testingbot.com/security");

driver.findElement(By.cssSelector("body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a")).click();

Thread.sleep(1000);
driver.quit();
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

options = Options()
options.browser_version = 'latest'
options.platform_name = 'WIN11'
options.set_capability('tb:options', {
  'key': 'API_KEY',
  'secret': 'API_SECRET',
  'name': 'My First Download Test'
})

driver = webdriver.Remote(
  command_executor='https://hub.testingbot.com/wd/hub',
  options=options
)
driver.get("https://testingbot.com/security")

driver.find_element(By.CSS_SELECTOR, "body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a").click()
time.sleep(2)

driver.quit()
const { Builder, By } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const fs = require('fs');

async function main() {
  let options = new chrome.Options();
  options.setBrowserVersion('latest');
  options.setPlatformName('WIN11');
  options.set('tb:options', {
    'key': 'API_KEY',
    'secret': 'API_SECRET',
    'name': 'My First Download Test'
  });

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

  await driver.get('https://testingbot.com/security');
  await driver.findElement(By.css("body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a")).click();
  await driver.sleep(2000);
  await driver.quit();
}
main();
#!/usr/bin/env ruby

require 'rubygems'
require 'selenium-webdriver'

options = Selenium::WebDriver::Chrome::Options.new
options.browser_version = 'latest'
options.platform_name = 'WIN11'
options.add_option('tb:options', {
  'key' => 'API_KEY',
  'secret' => 'API_SECRET',
  'name' => 'My First Download Test'
})

driver = Selenium::WebDriver.for(
  :remote,
  url: 'https://hub.testingbot.com/wd/hub',
  options: options
)
driver.navigate.to 'https://testingbot.com/security'

# click the download button
element = driver.find_element(:css, 'body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a')
element.click
sleep 6
driver.quit
require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\WebDriverBy;

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

$web_driver = RemoteWebDriver::create(
  'https://hub.testingbot.com/wd/hub',
  $capabilities
);
$web_driver->get('https://testingbot.com/security');
$web_driver->findElement(WebDriverBy::cssSelector('body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a'))->click();
$web_driver->quit();
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using System.IO;
using System.Threading;

namespace SeleniumTest {
    class FileDownload {
        static void Main(string[] args) {
            ChromeOptions options = new ChromeOptions();
            options.BrowserVersion = "latest";
            options.PlatformName = "WIN11";
            options.AddAdditionalOption("tb:options", new Dictionary<string, object>
            {
                ["key"] = "API_KEY",
                ["secret"] = "API_SECRET",
                ["name"] = "My First Download Test"
            });

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

            driver.FindElement(By.CssSelector("body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a")).Click();
            Thread.Sleep(2000);
            driver.Quit();
        }
    }
}

Verify that the file was downloaded successfully

Once you've downloaded a file during your test automation, you might want to check if it was downloaded correctly (does it exist on the disk?).
To perform this check, TestingBot has created a custom tb:fileExists command which can be used with the JavascriptExecutor provided by Selenium WebDriver.

Map<String, Object> fileMap = new HashMap<>();
fileMap.put("fileName", "...the file name...");
((JavascriptExecutor) driver).executeScript("tb:fileExists", fileMap);
driver.execute_script("tb:fileExists", {
  "fileName": "...the file name..."
})
await driver.executeScript('tb:fileExists', {
  "fileName": "...the file name..."
});
driver.execute_script('tb:fileExists', {
  'fileName' => '...the file name...'
})
$driver->executeScript('tb:fileExists', [
  'fileName' => '...the file name...'
]);
((IJavaScriptExecutor)driver).ExecuteScript("tb:fileExists", "{\"fileName\":\"...the file name...\"}");

Retrieve (meta) properties from the downloaded file

You can check the integrity of the downloaded file during your test, with the tb:fileProperties command. This will return the following file (meta) data:

{
  "changed_time": 1649947630,
  "created_time": 1649947629,
  "modified_time": 1649947630,
  "size": 114123,
  "md5": "5d4f7a7d4335c99048c3cc7563ffa7bf"
}
  • changed_time: Indicates, in unix epoch time, when the file (permission or content) was changed.
  • created_time: Indicates, in unix epoch time, when the file was downloaded.
  • modified_time: Indicates, in unix epoch time, when the file content was changed.
  • size: The size of the downloaded file, in bytes.
  • md5: The MD5 checksum of the downloaded file. Useful to verify the integrity of the file.
Map<String, Object> fileMap = new HashMap<>();
fileMap.put("fileName", "...the file name...");
((JavascriptExecutor) driver).executeScript("tb:fileProperties", fileMap);
driver.execute_script("tb:fileProperties", {
  "fileName": "...the file name..."
})
await driver.executeScript('tb:fileProperties', {
  "fileName": "...the file name..."
});
driver.execute_script('tb:fileProperties', {
  'fileName' => '...the file name...'
})
$driver->executeScript('tb:fileProperties', [
  'fileName' => '...the file name...'
]);
((IJavaScriptExecutor)driver).ExecuteScript("tb:fileProperties", "{\"fileName\":\"...the file name...\"}");

Retrieve the contents of the downloaded file

It is possible to download the file, which was downloaded on the remote TestingBot machine, to your own computer.
TestingBot has created a custom command, tb:fileContent, which will return a Base64-encoded string of the file content.

Map<String, Object> fileMap = new HashMap<>();
fileMap.put("fileName", "...the file name...");
String base64EncodedFile = (String) ((JavascriptExecutor) driver).executeScript("tb:fileContent", fileMap);
byte[] data = Base64.getDecoder().decode(base64EncodedFile);
OutputStream stream = new FileOutputStream("filename.txt");
stream.write(data);
stream.close();
get_file_content = driver.execute_script('tb:fileContent', {
  'fileName': '...the file name...'
})
data = base64.b64decode(get_file_content)
f = open('filename.txt', 'wb')
f.write(data)
let base64data = await driver.executeScript('tb:fileContent', {
  'fileName': '...the file name...'
});
let data = Buffer.from(base64data, 'base64');
fs.writeFileSync('filename.txt', data);
console.log('File ready');
get_file_content = driver.execute_script('tb:fileContent', {
  'fileName' => '...the file name...'
})
decoded_content = Base64.decode64(get_file_content)
f = File.open('filename.txt', 'wb')
f.write(decoded_content)
f.close
$base64content = $driver->executeScript('tb:fileContent', [
  'fileName' => '...the file name...'
]);
$decoded = base64_decode($base64content);
file_put_contents('filename.txt', $decoded);
string base64encode = (string) ((IJavaScriptExecutor)driver).ExecuteScript("tb:fileContent", "{\"fileName\":\"...the file name...\"}");
byte[] b = Convert.FromBase64String(base64encode);
File.WriteAllBytes(@"filename.txt", b);
Was this page helpful?

Looking for More Help?

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