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

Set Firefox profile

Set a custom Firefox profile or use an existing profile for your tests that run on Firefox browsers in BrowserStack Automate.

Firefox profiles include custom preferences that you would like to simulate an environment for your test script. For example, you might want to create a profile that sets preferences to handle the download popup programmatically during your test run.

The following sections include sample code snippets to create a profile or use an existing profile with preferences that handle the download popup and successfully downloads the file.

Check out the available preferences at the MozillaZine Knowledge Base.

In this guide, you will learn about:

Create a custom Firefox profile

Use the following sample code snippets to create a custom profile.

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;

public class JavaSample {

  public static final String URL = "https://YOUR_USERNAME:YOUR_ACCESS_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");
    
    FirefoxOptions capabilities = new FirefoxOptions();
    capabilities.setCapability("browserName", "Firefox");
    capabilities.setCapability("browserVersion", "95.0");
    HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
    browserstackOptions.put("os", "OS X");
    browserstackOptions.put("osVersion", "Monterey");
    browserstackOptions.put("buildName", "Selenium Java Firefox Profile");
    browserstackOptions.put("sessionName", "Selenium Java Firefox Profile");
    browserstackOptions.put("userName", "YOUR_USERNAME");
    browserstackOptions.put("accessKey", "YOUR_ACCESS_KEY");
    capabilities.setCapability("bstack:options", browserstackOptions);
    capabilities.setProfile(profile);

    WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
    driver.get("https://www.browserstack.com/test-on-the-right-mobile-devices");
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    
    jse.executeScript("arguments[0].scrollIntoView();", driver.findElement(By.className("icon-csv")));
    jse.executeScript("window.scrollBy(0,-100)");
    Thread.sleep(2000);

    WebElement element = driver.findElement(By.className("icon-csv"));
    element.click();
    Thread.sleep(2000);

    jse.executeScript("browserstack_executor: {\"action\": \"fileExists\"}");
    jse.executeScript("browserstack_executor: {\"action\": \"getFileProperties\"}");
    driver.quit();
  }
}

Use an existing Firefox profile

Using an existing profile from your local machine involves the following steps:

  1. Creating the Firefox profile locally
  2. Setting the Firefox profile in test scripts

Create the Firefox profile locally

  1. In the Firefox browser, enter about:profiles in the address bar.
  2. Create a new profile and click Launch profile in new browser.
  3. Enter about:config in the address bar
  4. Search for the following preferences in the search bar. If a preference is not autopopulated, you can add it manually.
    • browser.download.showWhenStarting to false
    • browser.download.folderList to 1
    • browser.download.manager.focusWhenStarting to false
    • browser.download.useDownloadDir to true
    • browser.helperApps.alwaysAsk.force to false
    • browser.download.manager.alertOnEXEOpen to false
    • browser.download.manager.closeWhenDone to true
    • browser.download.manager.showAlertOnComplete to false
    • browser.download.manager.useWindow to false
    • browser.helperApps.neverAsk.saveToDisk to application/octet-stream

Check out the official Mozilla documentation for detailed information.

Setting the Firefox profile in test scripts

Use the following sample code snippets to use an existing Firefox profile.

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.HashMap;

public class JavaSample {

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

  public static void main(String[] args) throws Exception {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("profile", "/Users/test/Library/Application Support/Firefox/Profiles/x0w613zr.firefox_profile");
    
    FirefoxOptions capabilities = new FirefoxOptions();
    capabilities.setCapability("browserName", "Firefox");
    capabilities.setCapability("browserVersion", "95.0");
    HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
    browserstackOptions.put("os", "OS X");
    browserstackOptions.put("osVersion", "Monterey");
    browserstackOptions.put("buildName", "Selenium Java Firefox Profile");
    browserstackOptions.put("sessionName", "Selenium Java Firefox Profile");      
    capabilities.setCapability("bstack:options", browserstackOptions);
    capabilities.setProfile(profile);

    WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
    driver.get("https://www.browserstack.com/test-on-the-right-mobile-devices");
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    
    jse.executeScript("arguments[0].scrollIntoView();", driver.findElement(By.className("icon-csv")));
    jse.executeScript("window.scrollBy(0,-100)");
    Thread.sleep(2000);

    WebElement element = driver.findElement(By.className("icon-csv"));
    element.click();
    Thread.sleep(2000);

    jse.executeScript("browserstack_executor: {\"action\": \"fileExists\"}");
    jse.executeScript("browserstack_executor: {\"action\": \"getFileProperties\"}");
    driver.quit();
  }
}

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