0

I need to open the Preferences page in Firefox using WebDriver, in order to clear the cache in a browser-specific way.

While investigating the issue, I found a Python solution and tried to translate it in Java, but WebDriver misinterpretes the uri:

driver.get("about:preferences#privacy");

and sends "/about:preferences#privacy" instead which Firefox doesn't know how to open.

I also tried a JavaScript way:

driver.executeScript("window.location.replace('about:preferences#privacy');")
driver.executeScript("window.open('about:preferences#privacy');")

but these are just ignored by the browser.

I assume there MUST be a Java way because this had already been implemented with Python WebDriver (see the first link).

Update: this turned out to be a non-WebDriver issue. In fact this originates in the testing framework that we're using and which is built around Selenium - Quantum Perfecto. The described behavior doesn't happen in pure Selenium. I sent a request to the framework support team to solve the issue in their code.

Denis Abakumov
  • 355
  • 3
  • 11
  • 2
    Back up a moment: _why_ do you need to do this? Selenium/webdriver are for testing page interaction, not for configuring the browser (if you need to do that, make a testing profile for that browser, and then tell webdriver to use that profile instead of the default profile when it runs) – Mike 'Pomax' Kamermans Aug 08 '19 at 22:33
  • As to *why*: in our tests we prefer to clear browser cache through the browser's gui, along with standard javascript methods ("window.localStorage.clear();" and "window.sessionStorage.clear();") as this has proven to be the most reliable way. – Denis Abakumov Aug 08 '19 at 22:53
  • You're telling me trying to get Selenium to run _application_ rather than _page_ interaction is more efficient than just running it with a clean profile? Because that's plain false. Have a profile dir in a known location, make your selenium runner wipe that dir on start, and make it start with a clean account on each run. Done. No "needing to clear cache", because there won't _be_ any cache. This is basically an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): the real question is "how do we make sure Selenium sessions run without cache from prior runs". – Mike 'Pomax' Kamermans Aug 09 '19 at 16:07
  • You're 100% right, Mike, but from the update under my question you can see that I'm locked down in the Quantum automation framework that doesn't give me full control over driver creation. Is I've mentioned, the issue doesn't even exist in pure Selenium but is rarther related to Quantum wrapper code. – Denis Abakumov Aug 09 '19 at 17:09
  • 1
    every time you instantiate a webdriver, it starts with new profile containing an empty cache and localstorage.. there is never a need to "clean" anything, regardless of framework you are using. – Corey Goldberg Aug 11 '19 at 14:36
  • Good point, Corey. But in the way Quantum handles a test suite, all test cases are run in the same browser session, hence the necessity to clear the cache. – Denis Abakumov Aug 11 '19 at 17:02

2 Answers2

2

An introduction into Custom Firefox Profile for Selenium

You will use it like:

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("<your-profile-name>");
WebDriver driver = new FirefoxDriver(myprofile);

As Mike 'Pomax' Kamermans says, the best would be to create the profiles based on the requirements you have.

If you need to programatically to create the environments, use a configuration management (like Puppet/Chef/Ansible), or even use maven to generate multiple environment.properties that you can use with custom script to configure (or create from scratch) a Firefox profile, which is answered in this thread

azbarcea
  • 3,323
  • 1
  • 20
  • 25
0

You can navigate to "about:preferences#privacy" by using driver.navigate().to();

Tested on the FF-68 , Selenium 4.0.0-alpha-2 (Java Binding)

driver.navigate().to("about:preferences#privacy");
Rahul L
  • 4,249
  • 15
  • 18