[Latest]How to Create a Firefox Profile to use in Selenium scripts?

When WebDriver launches browser then a new session gets created and you don’t see extensions, plugins or settings which are available on your browser. Sometimes few settings become mandatory that we even want to use them with the browser opened by WebDriver. I am pretty sure that you would be searching for the solution to carry forward your personal settings with test execution browser. This article will solve your problem in Mozilla Firefox; thus, Firefox profile is the concept to some personal settings in test execution.

Firstly, we will discuss a way to create a Firefox profile in Windows OS then further we will look at coding implementation to use profile in Selenium WebDriver.

Recommended: Technique to handle AJAX Call in Selenium WebDriver

What is Firefox Profile?

A Firefox profile is a personalized setting of the user which consists of personal bookmarks, extensions, plugins and saved passwords.

What is the location of the Firefox profile in my system?

If you are using windows OS then follow the path below to find out the location where the profile is stored.

C:\Users\<username>\AppData\Roaming\Mozilla\Firefox\Profiles\6whirxma.SeleniumWebDriverProfile

If you are not able to find AppData Folder then simply type %AppData% in Cortana search bar.

How to create a Firefox Profile?

Follow the steps below to create a Firefox profile. This profile will be used in your Selenium scripts for testing purpose. Also, make sure that you don’t have opened Mozilla Firefox in your system.

Step# 1: Go to Firefox profile selection option

Open run dialogue (Win+R) and type firefox.exe -p, then click OK

Open run to create firefox profile

Step# 2: Create a New Profile

A new dialogue box will appear, then click on the Create Profile button.

Choose Firefox Profile

Click Next.

Create Firefox Profile Wizard

Step# 3: Finish profile creation process

Another dialogue box appears; Give a name to your custom profile, then click Finish.

Complete Create Firefox Profile WIzard

Now you have successfully created a profile. Just click on Start Firefox and customize your profile for further use in Selenium scripts.

Start Firefox

 

How to use Firefox profile in Selenium WebDriver?

This is the agenda for this article. We will use the newly created profile in our automation scripts.

By using ProfilesIni class

		ProfilesIni profile = new ProfilesIni();
		
		FirefoxProfile fFoxProfile = profile.getProfile("SeleniumWebDriverProfile");
		
		FirefoxOptions opt = new FirefoxOptions();
		
		opt.setCapability(FirefoxDriver.PROFILE, fFoxProfile);
			
		WebDriver driver = new FirefoxDriver(opt);

Code Explanation

In the above code, we use ProfilesIni class to instantiate the profiles and we further use getProfile() to get the Firefox profile which we created above. Since DesiredCapabilities() class is deprecated, hence, we used FirefoxOptions() here and passes the instance of FirefoxProfile in setCapability() method.

Following is the sample program which opens the Firefox browser with custom settings in the custom profile.

Sample Program

package Test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class FirefoxProfiles {

	public static void main(String[] args) throws InterruptedException {
		
		System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
		
		ProfilesIni profile = new ProfilesIni();
		
		FirefoxProfile fFoxProfile = profile.getProfile("SeleniumWebDriverProfile");
		
		FirefoxOptions opt = new FirefoxOptions();
		
		opt.setCapability(FirefoxDriver.PROFILE, fFoxProfile);
			
		WebDriver driver = new FirefoxDriver(opt);
		
		driver.get("https://www.inviul.com");
		
		Thread.sleep(3000);
		
		System.out.println(driver.getTitle());
		
		driver.close();
		
		driver.quit();
	}

}

This was all about Firefox profile handling in Selenium WebDriver. If you have any confusions then write your queries in the comment section below. Don’t miss to support us on social media for more updates on Selenium and Test Automation.

Join Inviul fb group

One Response

Leave a Reply