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:
- Download files on remote desktop instances during your test
- Verify that the file was downloaded successfully
- Retrieve (meta) properties from the downloaded file
- Retrieve the contents of the downloaded file
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/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/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/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/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");
const 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/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/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.
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.
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();