Overview
FirefoxProfile represents a Firefox browser profile that manages user preferences, extensions, and SSL certificate trust settings for Selenium WebDriver sessions.
Description
The FirefoxProfile class provides a programmatic interface for creating and configuring Firefox browser profiles used during automated testing. It encapsulates the management of browser preferences (stored in user.js), browser extensions, SSL/TLS certificate acceptance policies, and the Linux no-focus library setting. Profiles can be constructed from scratch or modeled after an existing profile directory on disk. The class supports serialization to and deserialization from a Base64-encoded ZIP representation via toJson() and fromJson(), enabling profile transfer across remote WebDriver connections. When layoutOnDisk() is called, the profile is materialized to a temporary directory that includes copied model files, installed extensions, updated user preferences, and cleaned lock/cache files.
Usage
Use FirefoxProfile when you need to customize Firefox behavior for test sessions, such as setting browser preferences, installing extensions, controlling SSL certificate acceptance, or reusing an existing profile directory as a template. It is typically passed into FirefoxOptions before creating a FirefoxDriver.
Code Reference
Source Location
Signature
public class FirefoxProfile {
public FirefoxProfile()
public FirefoxProfile(File profileDir)
public static FirefoxProfile fromJson(String json) throws IOException
public String getStringPreference(String key, String defaultValue)
public int getIntegerPreference(String key, int defaultValue)
public boolean getBooleanPreference(String key, boolean defaultValue)
public boolean containsWebDriverExtension()
public void addExtension(Class<?> loadResourcesUsing, String loadFrom)
public void addExtension(File extensionToInstall)
public void addExtension(String key, Extension extension)
public void setPreference(String key, Object value)
public void updateUserPrefs(File userPrefs)
public boolean shouldLoadNoFocusLib()
public void setAlwaysLoadNoFocusLib(boolean loadNoFocusLib)
public void setAcceptUntrustedCertificates(boolean acceptUntrustedSsl)
public void setAssumeUntrustedCertificateIssuer(boolean untrustedIssuer)
public File layoutOnDisk()
public void clean(File profileDir)
public void cleanTemporaryModel()
public void deleteExtensionsCacheIfItExists(File profileDir)
String toJson() throws IOException
}
Import
import org.openqa.selenium.firefox.FirefoxProfile;
I/O Contract
Constructor Parameters
| Parameter |
Type |
Description
|
profileDir |
File (nullable) |
An existing Firefox profile directory to use as a template model. If null, a blank profile is created.
|
Key Methods
| Method |
Input |
Output |
Description
|
setPreference |
String key, Object value |
void |
Sets a Firefox preference (String, Integer, or Boolean value).
|
getStringPreference |
String key, String defaultValue |
String |
Retrieves a string preference or returns the default.
|
getIntegerPreference |
String key, int defaultValue |
int |
Retrieves an integer preference or returns the default.
|
getBooleanPreference |
String key, boolean defaultValue |
boolean |
Retrieves a boolean preference or returns the default.
|
addExtension(File) |
File extensionToInstall |
void |
Adds a Firefox extension from a file on disk.
|
addExtension(Class, String) |
Class<?>, String loadFrom |
void |
Adds an extension from the classpath or file system.
|
layoutOnDisk |
none |
File |
Writes the profile to a temporary directory on disk and returns its path.
|
fromJson |
String json |
FirefoxProfile |
Deserializes a profile from a Base64-encoded ZIP string.
|
toJson |
none |
String |
Serializes the profile to a Base64-encoded ZIP string.
|
setAcceptUntrustedCertificates |
boolean |
void |
Controls whether untrusted SSL certificates are accepted (default: true).
|
setAssumeUntrustedCertificateIssuer |
boolean |
void |
Controls whether certificate issuers are assumed untrusted (default: true).
|
Exceptions
| Exception |
Condition
|
UnableToCreateProfileException |
The provided model profile directory does not exist or is not a directory.
|
WebDriverException |
A preference expected to be boolean is not, or user preferences cannot be deleted/written.
|
Usage Examples
// Create a blank profile and set preferences
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.startup.homepage", "https://example.com");
profile.setPreference("network.proxy.type", 0);
profile.setAcceptUntrustedCertificates(true);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
// Create a profile from an existing directory
File profileDir = new File("/path/to/existing/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.addExtension(new File("/path/to/extension.xpi"));
profile.setPreference("dom.webnotifications.enabled", false);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
// Deserialize a profile from JSON (e.g., for RemoteWebDriver)
FirefoxProfile profile = FirefoxProfile.fromJson(base64EncodedZip);
Related Pages