58

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?

user861594
  • 5,733
  • 3
  • 29
  • 45
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • 1
    good question-im looking for an answer too. this is a problem with selenium 1 as well – rs79 Jul 22 '11 at 13:25
  • 1
    You will detail explanations on this thread.... http://stackoverflow.com/a/40521731/ – Rasel Nov 13 '16 at 07:38

10 Answers10

34

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.

Community
  • 1
  • 1
Ross Patterson
  • 9,527
  • 33
  • 48
  • 6
    `Then I changed settings in Firefox to clear all cookies and cache when exiting.` - I don't think that's necessary as Selenium will still create temporary profiles. It uses your specified one as a basis, i.e. it copies settings, so the original profile should not change. – Vsevolod Golovanov Feb 13 '13 at 13:59
  • 4
    This doesn't solve problem #1 in question. As previous answers say a temporary profile is created anyway, so time and space are consumed. – gamliela Oct 17 '13 at 15:31
  • @VsevolodGolovanov did you find any solution for this ? currently I am facing this issue. – Yaswanth Sep 13 '17 at 04:39
  • @abc, 1. It only bothered me because FF's own DevTools were inadequate back then, and I needed Firebug sometimes. The option to set a base profile was enough to fix this when necessary. 2. Today FF's DevTools are mature enough and Firebug is dead, so that's not an issue anymore. 3. I switched away from FF to Chrome for UI testing currently anyway, because FF's switch to incomplete Marionette broke some stuff. 4. Temp Profile perfomance hit was never a problem for me - it's negligible, if the tests themselves are fat. – Vsevolod Golovanov Sep 13 '17 at 07:19
  • @abc, for the record, a Chrome test currently takes 5 sec to start on my dev PC, don't remember how much it was with FF. – Vsevolod Golovanov Sep 13 '17 at 07:20
  • @VsevolodGolovanov, In my case we are running our apps in unix platform. We invoke firefox and read something from the websites, when it is started firefox will create temporary profile as rust_mozprofile under '/tmp' directory. So if the firefox has stopped abruptly then my rust_mozprofile which is under '/tmp' directory will not be deleted. So I just want to redirect the path from '/tmp' to '/home'. If you have knowledge on redirecting the path from '/tmp' to '/home'. please let me know. – Yaswanth Sep 13 '17 at 09:10
25

Be sure you call

driver.quit();

instead of

driver.close();

close() will not delete temporary files

Szabolcs Filep
  • 251
  • 3
  • 4
  • This should be a comment and not an answer. How does this answer the question? – stuxnetting Sep 23 '15 at 23:34
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Johannes Klauß Sep 24 '15 at 09:10
  • 7
    @JohannesKlauß This provide answer to exactly this part of the answer = " is taking unnecessary space". If you don't properly call driver.quit() you'll end with a tons of Gb of dummy profile data. – m3nda Oct 26 '15 at 13:55
  • .close() is also the solution for the nodejs implementation that can leave a temp folder behind with 5MB of files – David De Sloovere Apr 26 '16 at 13:55
  • Note that `close()` is not the same as `quit()` if there are more than one windows. – user202729 Jan 02 '21 at 03:18
3

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.

Community
  • 1
  • 1
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • 2
    I spent multiple hours figuring out, why it won't use the specified profile... The official doc says `webdriver.firefox.profile The name of the profile to use when starting firefox`. It doesn't mention that it still creates new anonymous profile, that it just uses the specified one as a source. Oh well, thanks for your update, now I get it. – Vsevolod Golovanov Jan 23 '13 at 14:45
2

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);
budi
  • 6,351
  • 10
  • 55
  • 80
Kanishka Dilshan
  • 724
  • 2
  • 10
  • 19
1

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);
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Woodman81
  • 53
  • 1
  • 7
0

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.

Max
  • 2,065
  • 24
  • 20
0
  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);
    
jnr
  • 790
  • 1
  • 7
  • 9
0

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.

EliuX
  • 11,389
  • 6
  • 45
  • 40
-1

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();
vickisys
  • 2,008
  • 2
  • 20
  • 27
-1

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

Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77