Defining FirefoxProfile preferences using Selenium WebDriver with Java

Sometimes, you might need to set the preferences of Firefox browser. We can do change the preferences of Firefox browser using FirefoxProfile class of WebDriver API.

What is Firefox profile?

Firefox saves your personal information such as bookmarks, passwords, and user preferences in a set of files called your profile, which is stored in a separate location from the Firefox program files. You can have multiple Firefox profiles, each containing a separate set of user information. The Profile Manager allows you to create, remove, rename, and switch profiles.

For more information, visit Firefox Profile.

Now, when we open the Firefox browser through automation script, it opens the default firefox profile.

So, using FirefoxProfile class and its setPreference() method, we can set the preferences first for the browser and then open the browser with already set preferences.

Syntax :

// creating an instance of FirefoxProfile class to set preferences for FF browser

FirefoxProfile profile = new FirefoxProfile();

 

Methods of FirefoxProfile class :

  1. public void setAcceptUntrustedCertificates(boolean acceptUntrustedSsl): The method sets whether the Firefox should accept the untrusted SSL certificates or not.
    The default value is set to TRUE.
  2. public void setPreference(String key, int value) : The method sets the preference for the current profile. It sets the property passed as a first parameter(key) and sets the value with integer value passed as a second parameter.
  3. public void setPreference(String key, boolean value) : The method sets the preference for the current profile. It sets the property passed as a first parameter(key) and sets the value with boolean value passed as a second parameter.
  4. public void setPreference(String key, String value) : The method sets the preference for the current profile. It sets the property passed as a first parameter(key) and sets the value with String value passed as a second parameter.

setPreference is the important method which has 3 overloaded versions each accepting different type of parameter as a property value (integer, boolean, string).

Now, lets dig into the practical scenarios where we can make use of FirefoxProfile.

// creating an instance of FirefoxProfile

FirefoxProfile profile = new FirefoxProfile();

// setting home page of browser when startup
profile.setPreference(“browser.startup.homepage”, “http://google.com”);

// setting download directory of firefox browser
profile.setPreference(“browser.download.folderList”, 2);

// setting download directory path
profile.setPreference(“browser.download.dir”, “pathToDirectory”);

// setting download manager startup status
profile.setPreference(“browser.download.manager.showWhenStarting”,
false);

// instantiating firefox driver with defined FirefoxProfile

WebDriver driver = new FirefoxDriver(profile);

 

 

How to get Preferences List that we can set using FirefoxProfile?

  1. Open Firefox browser and type about:config in address bar.

    firefoxprofile-preferences-list-using-selenium-webdriver.png

 

2. Click on I’ll be careful, I promise! button. And you will get the complete list of Preferences with its Name, Status, Type and Value.

firefoxprofile-using-selenium-webdriver_1.png

Now, since you have got the list of preferences of firefox browser, you can set any preference through setPreference method as shown in code snippet

Hope it works for you!

Cheers 🙂

Advertisements

#firefoxprofile, #setpreference, #webdriver