How to stop Selenium from creating temporary Firefox Profiles using Web Driver?

JavaSeleniumFirefoxWebdriverProfile

Java Problem Overview


I am using Selenium Web Driver API with Java. Every time I want to debug my test cases, a temporary profile for Firefox is created in the temporary files directory. This is a headache in two ways.

  1. It definitely is taking unnecessary time to create a profile and is taking unnecessary space.
  2. I cannot install any addons that will be available next time I launch my test cases.

How do I get around this?

Java Solutions


Solution 1 - Java

You can control how the Firefox driver chooses the profile. Set the webdriver.firefox.profile property to the name of the profile you want to use. Most folks think this is a bad idea, because you'll inherit all the cookies, cache contents, etc. of the previous uses of the profile, but it's allowed if you really want to do it.

For example:

System.setProperty("webdriver.firefox.profile", "MySeleniumProfile");
WebDriver driver = new FirefoxDriver(...);

UPDATE - From Ranhiru

How I handled it for Java

FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));    				
WebDriver driver = new FirefoxDriver(profile);

Then I changed settings in Firefox to clear all cookies and cache when exiting. Look here on how to do it.

Solution 2 - Java

Be sure you call

driver.quit();

instead of

driver.close();

close() will not delete temporary files

Solution 3 - Java

The answer was actually pretty easy after I went through this question where I found the documentation. I found the FirefoxProfile class and the constructor took the path to the Firefox Profile.

WebDriver driver = null;
FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\Ranhiru\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\qp1nizdo.Selenium"));
driver = new FirefoxDriver(profile);

I created a new profile by running "Firefox.exe" with the -p flag.

Firefox.exe -p

Installed the extensions I needed to this profile and I was good to go! :)

Update

It does not matter whether you assign a profile or not, the temporary profile in the temp folder is created nevertheless. By creating and assigning a profile you get the chance to use firebug/xpather/your favorite extension when testing.

Solution 4 - Java

You can load the FirefoxWebDriver with the desired plugins by calling addExtension(File) method in FirefoxProfile class.

Example:

try {
    File firebug = new File("C:\\FFPlugins\\firebug-1.7.3.xpi");
    File xpathChecker = new File("C:\\FFPlugins\\xpath_checker-0.4.4-fx.xpi");
    profile.addExtension(firebug);
    profile.setPreference("extensions.firebug.currentVersion", "1.7.3");
    profile.addExtension(xpathChecker);
    profile.setPreference("extensions.xpath_checker.currentVersion", "0.4.4");
} catch(IOException e) {
    e.printStackTrace();
}
driver = new FirefoxDriver(profile);

Solution 5 - Java

I have create a custom profile by running the command:

firefox -profileManager 

(then adding any exceptions etc I require - as due to selenium opening clean profile/instance each time the exceptions are lost)

I was then directly creating my Firefox with this profile using the following:

private static String profilePath = "C:\\fitnesse\\Selenesse\\FFProfiles";
private static FirefoxProfile ffprofile = new FirefoxProfile(profilePath); 
private static IWebDriver driver = new FirefoxDriver(ffprofile);

Solution 6 - Java

You can not stop Selenium from creating temporary files even you explicitly specified one. But you can clear it after tests completed.

TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles()

Tested under MacOS and Ubuntu.

Solution 7 - Java

  1. Start firefox with: firefox -P

  2. Create new profile (e.g. webdriver1), add necessary plugins etc.

  3. Add to your test case:

    > DesiredCapabilities desiredCapabilities = > new DesiredCapabilities("firefox", "", Platform.ANY); > FirefoxProfile profile = new ProfilesIni().getProfile("webdriver1"); > desiredCapabilities.setCapability("firefox_profile", profile); > WebDriver webdriver = new RemoteWebDriver(desiredCapabilities);

Solution 8 - Java

You can use always the profile named default, which is the one used by default by the user. There you will have all your cookies, plugins, etc. and you will be able to use it with no complications, using

System.setProperty("webdriver.firefox.profile", "default");

before creating the WebDriver

WebDriver driver = new FirefoxDriver();

for creating some new profile you can execute in the shell firefox -p which will show you

Add new profile to Firefox

and get a new profile besides the already existing default. That will give you all the freedom you want.

Solution 9 - Java

You can tell Selenium directly to use specific profile. Here is one of examples: http://automationtricks.blogspot.com/2010/05/how-to-run-test-cases-in-specified.html

Solution 10 - Java

you can specify different location for temporary files before the program begins, so that your program may not stop due to "lack of memory space"

if(!new File("c:/temp_data").isDirectory())  //to check dir exist
        
    FileUtils.forceMkdir(new File("c:/temp_data")); //create dir if not exists    
    TemporaryFilesystem.setTemporaryDirectory(new File("c:/temp_data")); //set new dir as temp file path for selenium    
    FirefoxProfile profile = new FirefoxProfile();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRanhiru Jude CoorayView Question on Stackoverflow
Solution 1 - JavaRoss PattersonView Answer on Stackoverflow
Solution 2 - JavaSzabolcs FilepView Answer on Stackoverflow
Solution 3 - JavaRanhiru Jude CoorayView Answer on Stackoverflow
Solution 4 - JavaKanishka DilshanView Answer on Stackoverflow
Solution 5 - JavaWoodman81View Answer on Stackoverflow
Solution 6 - JavaMaxView Answer on Stackoverflow
Solution 7 - JavajnrView Answer on Stackoverflow
Solution 8 - JavaEliuXView Answer on Stackoverflow
Solution 9 - JavaPavel JanicekView Answer on Stackoverflow
Solution 10 - JavavickisysView Answer on Stackoverflow