FirefoxOptions lets you set up Firefox browser settings before running tests. It helps control how Firefox behaves during automation.
0
0
FirefoxOptions configuration in Selenium Java
Introduction
When you want to run Firefox in headless mode (without opening the browser window).
When you need to set a custom Firefox profile with saved settings or extensions.
When you want to disable browser notifications during tests.
When you want to specify browser arguments like window size or private mode.
When you want to configure Firefox to accept insecure certificates for testing.
Syntax
Selenium Java
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-headless");
options.setAcceptInsecureCerts(true);
WebDriver driver = new FirefoxDriver(options);Use addArguments to add command line options to Firefox.
Pass the FirefoxOptions object to the FirefoxDriver constructor to apply settings.
Examples
This runs Firefox without opening the browser window, useful for faster tests.
Selenium Java
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-headless");
WebDriver driver = new FirefoxDriver(options);This allows Firefox to accept insecure SSL certificates during testing.
Selenium Java
FirefoxOptions options = new FirefoxOptions(); options.setAcceptInsecureCerts(true); WebDriver driver = new FirefoxDriver(options);
This disables browser notifications by setting a custom Firefox profile.
Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("dom.webnotifications.enabled", false);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);Sample Program
This test runs Firefox in headless mode with notifications disabled and accepts insecure certificates. It opens example.com and prints the page title.
Selenium Java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; public class FirefoxOptionsTest { public static void main(String[] args) { // Create Firefox profile to disable notifications FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("dom.webnotifications.enabled", false); // Set Firefox options FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); options.addArguments("-headless"); options.setAcceptInsecureCerts(true); // Start Firefox with options WebDriver driver = new FirefoxDriver(options); // Open a website driver.get("https://example.com"); // Print page title System.out.println("Title: " + driver.getTitle()); // Close browser driver.quit(); } }
OutputSuccess
Important Notes
Always make sure the FirefoxDriver executable is set up correctly in your system path or specified in your code.
Headless mode is great for running tests on servers without a display.
Use FirefoxProfile to customize browser settings beyond simple arguments.
Summary
FirefoxOptions lets you customize Firefox browser behavior for tests.
You can add arguments, set profiles, and accept insecure certificates.
Pass FirefoxOptions to FirefoxDriver to apply your settings.