Skip to main content
No Result Found

Testing file download

Learn how to test the file download functionality using Automate TurboScale.

BrowserStack enhances your selenium testing suite by offering features to streamline file download interactions within web applications. These features are especially useful when testing scenarios like invoice generation, PDF downloads, and other file-centric workflows.

This feature is supported only for tests running with the Selenium framework.

Download files on Browserstack remote instances

To download files on BrowserStack’s remote instances, integrate the custom browserstack_executor script into your Automate TurboScale sessions. The following sample code illustrates its use in selenium:

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 BROWSERSTACK_USERNAME = "YOUR_USERNAME";
  public static final String BROWSERSTACK_ACCESS_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + BROWSERSTACK_USERNAME + ":" + BROWSERSTACK_ACCESS_KEY + "@hub-cloud.browserstack-ats.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), caps);
    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

BrowserStack provides a range of JavaScript executors to help you work with downloaded files. Use the following tabs to learn about different Javascript executors:

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\"}"));

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

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