Skip to main content
Experience faster, smarter testing with BrowserStack AI Agents. See what your workflow’s been missing. Explore now!
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Test the file download functionality

Learn how to test the file download functionality of your web app using Selenium on BrowserStack Automate.

File download functionality is often implemented in web applications to support scenarios, such as downloading invoices, PDFs, etc. Though Selenium provides basic commands to download any file from a website, BrowserStack provides custom executors to assert if the file is downloaded successfully, view file properties, and transfer files to local machine.

In this guide, you will learn how to:

Download files on BrowserStack remote instances

BrowserStack provides a custom browserstack_executor script that lets you check whether the file is downloaded successfully in an Automate session. The following sample code shows how to use Selenium to download a file on a BrowserStack remote instance in your Automate session:

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {

  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.folderList", 1);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.useDownloadDir", true);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.closeWhenDone", true);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    // You will need to find the content-type of your app and set it here.
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("browserName", "Firefox");
    capabilities.setCapability("browserVersion", "latest");
    HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
    browserstackOptions.put("os", "Windows");
    browserstackOptions.put("osVersion", "10");
    capabilities.setCapability("bstack:options", browserstackOptions);

    WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
    JavascriptExecutor jse = (JavascriptExecutor) driver;

    driver.get("https://www.browserstack.com/test-on-the-right-mobile-devices");
    Thread.sleep(2000);
    driver.findElement(By.id("accept-cookie-notification")).click();

    // Find element by class name and store in variable "Element"
    WebElement Element = driver.findElement(By.className("icon-csv"));

    // This will scroll the page till the element is found
    jse.executeScript("arguments[0].scrollIntoView();", Element);
    jse.executeScript("window.scrollBy(0,-100)");
    Thread.sleep(1000);

    // Click on the element to download the file
    Element.click();
    Thread.sleep(2000);

    driver.quit();
  }
}

Work with the downloaded file

Use the information in the following tabs to learn about different Javascript executors that BrowserStack provides to verify the downloaded file:

Protip: Any of the custom Javascript executors will work even if fileName argument is not passed. The results will be for the last downloaded file in the session.
System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"fileExists\", \"arguments\": {\"fileName\": \"<file name>\"}}"));

The fileExists function within the browserstack_executor script returns a boolean value as a response.

Use the above code snippets to verify whether a specific file was downloaded.

Protip: The following snippet verifies whether the last file was downloaded in the test session.
System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"fileExists\"}"));

Sample Code that uses the executors

The following sample code is a cumulative script that downloads a file, and then uses all the supported Javascript executors that BrowserStack provides:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.net.URL;

public class script {

  public static final String USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.folderList", 1);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.useDownloadDir", true);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.closeWhenDone", true);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    // You will need to find the content-type of your app and set it here.
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
    
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("browserName", "Firefox");
    capabilities.setCapability("browserVersion", "latest");
    HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
    browserstackOptions.put("os", "Windows");
    browserstackOptions.put("osVersion", "10");
    capabilities.setCapability("bstack:options", browserstackOptions);

    WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
    JavascriptExecutor jse = (JavascriptExecutor) driver;

    // Navigate to the link
    driver.get("https://www.browserstack.com/test-on-the-right-mobile-devices");
    Thread.sleep(2000);

    // Accept the cookie popup
    WebElement pop = driver.findElement(By.id("accept-cookie-notification"));
    pop.click();

    // Find element by class name and store in variable "Element"  
    WebElement Element = driver.findElement(By.className("icon-csv"));

    // This will scroll the page till the element is found       
    jse.executeScript("arguments[0].scrollIntoView();", Element);
    jse.executeScript("window.scrollBy(0,-100)");
    Thread.sleep(1000);

    // Click on the element to download the file
    Element.click();
    Thread.sleep(2000);

    // Check if file exists
    System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"fileExists\", \"arguments\": {\"fileName\": \"BrowserStack - List of devices to test on.csv\"}}"));

    // Get file properties
    System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"getFileProperties\", \"arguments\": {\"fileName\": \"BrowserStack - List of devices to test on.csv\"}}"));

    // Get file content. The content is Base64 encoded
    String base64EncodedFile = (String) jse.executeScript("browserstack_executor: {\"action\": \"getFileContent\", \"arguments\": {\"fileName\": \"BrowserStack - List of devices to test on.csv\"}}");

    // Decode the content to Base64 and write to a file
    byte[] data = Base64.getDecoder().decode(base64EncodedFile);
    OutputStream stream = new FileOutputStream("BrowserStack%20-%20List%20of%20devices%20to%20test%20on.csv");
    stream.write(data);
    stream.close();
    driver.quit();
  }
}

Check our list of various JavaScript Executors that BrowserStack offers.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Test web and mobile, from manual to automation, on real devices in one unified platform.

Contact Us

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle