Running Selenium Tests with a Custom Firefox Profile

When we run our Selenium based web tests we often run them with multiple threads. This results in multiple browser windows on your desktop. I was tasked with getting the browser windows to start up minimized.

The solution I came up with is described below.

Note: This doesn’t have to work with all browsers. Just Firefox is good enough.

The first step was to figure out how to start Firefox minimized from Selenium. Turns out there is a plugin for Firefox to do just that. It’s called “Minimize on Start and Close”. Just do a search for it in Firefox’s “Add Ons” menu and you’ll find it.

The next step was to create a custom profile for Firefox with this plugin installed. To do this you run Firefox from the command line like this:

firefox.exe -profilemanager

Choose “Create Profile” and give it a new name when prompted. Firefox then starts up with a new default profile which you can configure as needed. Using this profile I added the “Minimize on Start and Close” plugin and set it up to minimize on start. I also adjusted the profile to show a blank screen on start up, use a direct connection to the network (not proxied), turned off spell checking , turned off automatic updates as well as a few other options. I wanted this profile to be as bare bones as possible.

After adjusting the profile you close firefox and the profile is saved.

I then copied the created profile to a safe location and zipped it up for safekeeping. On Windows 7 Firefox profiles are found under AppData as shown in the following screen shot:

Now, in order to use this custom profile I had to adjust the code we use in our tests to support the use of custom profiles. Here’s the code snippet below:

private static SeleniumServer startSeleniumServer(int port) throws Exception {
  RemoteControlConfiguration rc = new RemoteControlConfiguration();
  rc.setPort(port);
  rc.setSingleWindow(true);
  rc.setTrustAllSSLCertificates(true);

  ...

  // Here's where we attempt to use the custom profile
  String customProfilePath = System.getProperty("selenium.custom.profile");
  if(StringUtils.isNotBlank(customProfilePath)) {
    File firefoxProfile = new File(customProfilePath);
    if(firefoxProfile.exists()) {
      LOGGER.warn("Using custom firefox profile: " + customProfilePath);
      rc.setFirefoxProfileTemplate(firefoxProfile);
      rc.setBrowserSideLogEnabled(false);
      rc.setDebugMode(false);
    }
  }
  SeleniumServer server = new SeleniumServer(rc);
  server.start();
  LOGGER.info("Selenium Server Started Successfully on Port:" + port);
  return server;
}

The last thing to do was to add the JVM argument to the JRE command line to point to the new custom profile. I added the following JVM argument:

-Dselenium.custom.profile="C:UsersPeter.DavisonAppDataRoamingMozillaFirefoxProfiles6qjh0zno.minimized"

One other note. I learned that you can specify exactly the version of Firefox to run with your tests by appending the path to the executable in your JVM argument:

-Dselenium.browser="*firefox3 c:UsersPeter.DavisonMozilla Firefox 3.6firefox.exe"
It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply