Features

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.


#!/usr/bin/env ruby

require 'rubygems'
require 'selenium-webdriver'

caps = Selenium::WebDriver::Remote::Capabilities.new
caps["browserName"] = "chrome"
caps["version"] = "latest"
caps["platform"] = :WINDOWS
caps["name"] = "My First Download Test"

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120

driver = Selenium::WebDriver.for(
  :remote,
  :url => "https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub",
  :desired_capabilities => caps,
  :http_client => client)
driver.navigate.to "https://testingbot.com/enterprise/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
String URL = "https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub";
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platform", "WIN10");
caps.setCapability("browserName", "Chrome");
caps.setCapability("version", "latest");
caps.setCapability("name", "My First Download Test");

WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("https://testingbot.com/enterprise/security");

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

Thread.sleep(1000);
driver.quit();
require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

$caps = array(
  "version" => "latest",
  "browserName" => "chrome",
  "platform" => "WIN10",
  "name" => "My First Download Test"
);
$web_driver = RemoteWebDriver::create(
  "https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub",
  $caps
);
$web_driver->get("https://testingbot.com/enterprise/security");
$web_driver->findElement(WebDriverBy::cssSelector("body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a")).click();
$web_driver->quit();
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Input capabilities
desired_cap = {
 'browserName': 'chrome',
 'version': 'latest',
 'platform': 'WIN10',
 'name': 'My First Download Test'
}

driver = webdriver.Remote(
  command_executor='https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub',
  desired_capabilities=desired_cap,
  options=options
)
driver.get("https://testingbot.com/enterprise/security")

driver.find_element_by_css("body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a").click()
time.sleep(2)

driver.quit()
const {Builder, By, Key, until} = require('selenium-webdriver');
const fs = require("fs");
var sleep = require('sleep');

let capabilities = {
    'browserName' : 'Chrome',
    'version' : 'latest',
    'platform': 'WIN10',
    'name': 'My First Download Test'
}

async function main() {
  let driver = await new webdriver.Builder().usingServer('https://API_KEY:API_SECRET@hub.testingbot.com/wd/hub')
      .withCapabilities(capabilities)
      .build()

  await driver.get('https://testingbot.com/enterprise/security')
  await driver.findElement(By.css("body > div.bigHeader.enterprise > div.hero.enterprise.feature > div > div > div > a")).click();
  sleep.sleep(2);
  await driver.quit()
}
main()
using System;
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) {
            IWebDriver driver;
            ChromeOptions capability = new ChromeOptions();
            capability.AddAdditionalCapability("browserName", "Chrome", true);
            capability.AddAdditionalCapability("version", "latest", true);
            capability.AddAdditionalCapability("platform", "WIN10", true);
            capability.AddAdditionalCapability("key", "your_testingbot_key", true);
            capability.AddAdditionalCapability("secret", "your_testingbot_secret", true);
            capability.AddAdditionalCapability("name", "My First Download Test", true);

            driver = new RemoteWebDriver(
                new Uri("http://hub.testingbot.com/wd/hub/"), capability
            );
            driver.Navigate().GoToUrl("https://testingbot.com/enterprise/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.


driver.execute_script("tb:fileExists", {
  "fileName": "...the file name...",
})
Map<String, Object> fileMap = new HashMap<>();
fileMap.put("fileName", "...the file name...");
((JavascriptExecutor) driver).executeScript("tb:fileExists", fileMap);
$driver->executeScript("tb:fileExists", [
  "fileName" => "...the file name..."
 ]
]);
driver.execute_script("tb:fileExists", {
  "fileName": "...the file name..."
})
browser.execute('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.

driver.execute_script("tb:fileProperties", {
  "fileName": "...the file name...",
})
Map<String, Object> fileMap = new HashMap<>();
fileMap.put("fileName", "...the file name...");
((JavascriptExecutor) driver).executeScript("tb:fileProperties", fileMap);
$driver->executeScript("tb:fileProperties", [
  "fileName" => "...the file name..."
 ]
]);
driver.execute_script("tb:fileProperties", {
  "fileName": "...the file name..."
})
browser.execute('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.


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
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();
$driver->executeScript("tb:fileContent", [
  "fileName" => "...the file name..."
 ]
]);
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 data = browser.execute('tb:fileContent', { 
  "fileName": "...the file name..."
})
let base64data = data.toString('base64');
await fs.writeFile('filename.txt', base64data, function (err) {
  if (err) throw err;
  console.log('File ready')
});
string base64encode = (string) ((IJavaScriptExecutor)driver).ExecuteScript("tb:fileContent", "{\"fileName\":\"...the file name...\"}}");
byte[] b = Convert.FromBase64String(base64encode);
File.WriteAllBytes(@"filename.txt", b);