1

I'm on selenium 2.53.0 (I don't want to update to 3.0 because I need to work on Firefox and Selenium did not implement actions for FF).

My problem is: I try to open FF in private mode (I don't want to keep the cache because I ran a lot of FF instances)

For that, I use a FF profile and I have a certificate, so I accept it.

My code is :

        FirefoxDriverManager.getInstance().setup();

        capabilities.setBrowserName("firefox");
        capabilities.setVersion("46");

        ProfilesIni profile = new ProfilesIni();
        FirefoxProfile ffProfile = profile.getProfile("default");

        ffProfile.setPreference("browser.private.browsing.autostart", true); 
        ffProfile.setPreference("browser.privatebrowsing.autostart", true);

        //accept the certificate
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);
        capabilities.setCapability(FirefoxDriver.PROFILE, ffProfile);
        WebDriver webDriver = new FirefoxDriver();

I tried "browser.private.browsing.autostart" and "browser.privatebrowsing.autostart" because when I did about:config in firefox, i found this two.

I did not received any error, firefox run my test but not in private. Do you have any idea? I found this post but it haven't have answer.

Community
  • 1
  • 1
Bob
  • 529
  • 1
  • 7
  • 28

2 Answers2

6
FirefoxOptions opts = new FirefoxOptions();
opts.addArguments("-private");
FirefoxDrive f = new FirefoxDriver(opts);

working currently with FF v54 and selenium 3.4.0

spy
  • 3,199
  • 1
  • 18
  • 26
4

On your code sample above you are trying twice to set the profile. Try removing one of the arguments? AS long as

 ffProfile.setPreference("browser.privatebrowsing.autostart", true);

is not working for you, there is an alternative workaround solution; so you can work until you figure this out.

Find any element at the page you are visiting where you want the private window to be opened. For example:

Driver.FindElement(By.ByXpath(//div[@id="loginButton"])).sendKeys(Keys.chord(Keys.CONTROL, Keys.SHIFT, "P"));

And then send a Ctrl+Shift+p to it..Then you can use the new private window for your test. Perhaps not the best solution but, this will work 100%, just tested on my machine, please comment below if you have trouble getting this right.

Best of luck!

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
Xwris Stoixeia
  • 1,831
  • 21
  • 22
  • it's "Working", It absolutly stop my test :) by printing an information about privacy but yes, my page is in private mode. Do you know why my preference is not working? I need to change my code for the workaround. – Bob Jan 11 '17 at 10:32
  • 1
    Probably it stop your test because you need to getWindowHandles() and then switch to the newly opened window. In the meantime, I'll have a play see if I can launch Firefox from the preferences for you. – Xwris Stoixeia Jan 11 '17 at 13:08
  • When I type about:config in firefox, I can see that the Boolean variable browser.privatebrowsing is set to true (yet firefox still opens in a normal not private window). But according to this official link below (always use private browsing) https://support.mozilla.org/en-US/kb/private-browsing-use-firefox-without-history#w_can-i-set-firefox-to-always-use-private-browsing you won't see a purple ribbon when firefox starts. But in essence because it does not remember the history it is in private mode. And another link. http://ccm.net/faq/15012-how-to-start-firefox-in-private-mode-by-default – Xwris Stoixeia Jan 11 '17 at 14:50
  • I also tried playing with Firefox profiles but to no avail http://toolsqa.com/selenium-webdriver/custom-firefox-profile/ – Xwris Stoixeia Jan 11 '17 at 14:52
  • 1
    When I type about:config, I cannot see the Boolean variable browser.privatebrowsing set to true, it always set to false. So if it true (and not in purple) it's ok? Because when I use your workaround it's in purple. Thanks for your help! – Bob Jan 11 '17 at 14:58
  • In about:config, do you have default as status? or user set? it's always default... that should be user set and true when I set it with selenium, right? – Bob Jan 11 '17 at 15:01
  • I cannot do it manually, because I will run my test with a grid on a AWS machine (without distant access) I need to do everything with the code – Bob Jan 11 '17 at 15:02
  • 1
    Mine is user set and true; however despite that still no purple private window – Xwris Stoixeia Jan 12 '17 at 13:44