FirefoxOptions for running Webdriver Tests

Earlier the way how we used to construct a FirefoxDriver is by passing desired capabilities object like FirefoxDriver(Capabilities desiredCapabilities) which is now Deprecated. But from Selenium v3.6.0 it is recommended to construct a FirefoxDriver with FirefoxOptions, like below:

FirefoxOptions options = new FirefoxOptions()
WebDriver driver = new FirefoxDriver(options);

Earlier we have used RemoteWebDriver with DesiredCapabilities object to define which browser, version of the browser and platform (OS - Windows / LINUX etc) that we want to run our tests. But now with selenium version v3.6.0, we should start using like below :

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), capabilities);

So now with the new FirefoxOptions, it should looks like below : -

FirefoxOptions options = new FirefoxOptions();
driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), options);

When you start your Selenium Nodes, it displays a log information on using new FirefoxOptions preferred to 'DesiredCapabilities.firefox() along with all other browser options. Check below screenshot of Node console after starting it.

Selenium grid node console

Firefox Binary :-

When Firefox browser is not installed in the default location ("C:\Program Files (x86)\Mozilla Firefox\firefox.exe" OR "C:\Program Files\Mozilla Firefox\firefox.exe"), we have to add binary path to FirefoxOptions. If not, you may see an error like 'Cannot find firefox binary in PATH. Make sure firefox is installed'.

If your Firefox installation location like 'C:\Users\username\AppData\Local\Mozilla Firefox\firefox.exe', then make sure to add Firefox binary path to FirefoxOptions like below :-

FirefoxOptions options = new FirefoxOptions()
String strFFBinaryPath = "C:/Users/username/AppData/Local/Mozilla Firefox/firefox.exe";
options.setBinary(strFFBinaryPath)
driver = new FirefoxDriver(options)

You can use any of the below methods available from FirefoxOptions : -

setBinary(FirefoxBinary binary) 
setBinary(java.nio.file.Path path) 
setBinary(java.lang.String path) 

Firefox Profile :-

As we know every time when we launch browser through automation, selenium starts a new session with temp profile without any bookmarks, plug-ins attached to it. If you want to launch browser with some plug-ins / settings to the browser which is useful for your automation, you can use Firefox Profile class.

You can initializes a new instance of the FirefoxProfile or new instance of the FirefoxProfile using a specific profile directory and need to set preferences to FirefoxOptions.

Example:- If you want to enable Flash within Firefox, you can do that by setting the profile capability like below :-

FirefoxProfile profile = new FirefoxProfile();
//Set a preference for this particular profile.
profile.setPreference('dom.ipc.plugins.enabled.libflashplayer.so', 'false');

FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
//Sets whether Firefox should accept SSL certificates which have expired, signed by an unknown authority or are generally untrusted.
options.setAcceptUntrustedCertificates(true)
FirefoxDriver driver = new FirefoxDriver(options);

To launch browser with some predefined settings we can load the existing profile and set that with FirefoxOptions.

FirefoxOptions options = new FirefoxOptions();
options.setProfile(getProfile("autoprofile"));
FirefoxDriver driver = new FirefoxDriver(options);

private static FirefoxProfile getProfile(String profileName) {
if(profileName == null || profileName.trim().isEmpty()) {
    return new FirefoxProfile();
  }
    return new ProfilesIni().getProfile(profileName);
}
Selenium Tutorials: 

Comments

Hi,

First of all thanks for the useful article! nonetheless, I would appreciate if anyone could provide a list of arguments that can be passed to the `addArguments` method of the FireFoxOptions class, similar to what is done to ChromeOptions.

Thanks!

Add new comment