1

I have this:

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.no_proxies_on", "localhost");
profile.setPreference("javascript.enabled", true);

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

FirefoxOptions options = new FirefoxOptions();
options.setLogLevel(Level.FINEST);
options.addPreference("browser.link.open_newwindow", 3);
options.addPreference("browser.link.open_newwindow.restriction", 0);

Now I have two different constructors:

WebDriver driver = new FirefoxDriver(capabilities);

and

WebDriver driver = new FirefoxDriver(options);

How can I pass them both (capabilities and options) into the driver? By the way, the IDE is telling me that FirefoxDriver(capabilities) is deprecated.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
assembler
  • 2,077
  • 7
  • 24
  • 44

2 Answers2

2

You were almost there. You need to use the method merge() from MutableCapabilities Class to merge the DesiredCapabilities type of object into FirefoxOptions type object and initiate the WebDriver and WebClient instance by passing the FirefoxOptions object as follows :

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.no_proxies_on", "localhost");
profile.setPreference("javascript.enabled", true);

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

FirefoxOptions options = new FirefoxOptions();
options.merge(capabilities);
options.setLogLevel(Level.FINEST);
options.addPreference("browser.link.open_newwindow", 3);
options.addPreference("browser.link.open_newwindow.restriction", 0);

WebDriver driver = new FirefoxDriver(options);

References

You can find a couple of relevant discussions in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • it yells me `java.lang.IllegalArgumentException: Preference browser.link.open_newwindow may not be overridden: frozen value=2, requested value=3`... what can I do? – assembler Aug 14 '19 at 13:20
  • @assembler That error is related to the _Key_ / _Value_ pair which you are passing as `browser.link.open_newwindow.restriction` / `0`. But this answer is to cater to your need to _pass the capabilities and options into Firefoxdriver_ – DebanjanB Aug 14 '19 at 13:26
0

you can pass capabilities into firefoxoptions constructor as below :

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

   FirefoxProfile profile = new FirefoxProfile();
   profile.setPreference("network.proxy.no_proxies_on", "localhost");
   profile.setPreference("javascript.enabled", true);

   DesiredCapabilities capabilities = DesiredCapabilities.firefox();
   capabilities.setCapability("marionette", true);

   FirefoxOptions options = new FirefoxOptions(capabilities);

set profile to firefox options
   options.setProfile(profile);
   options.setLogLevel(Level.FINEST);
   options.addPreference("browser.link.open_newwindow", 3);
   options.addPreference("browser.link.open_newwindow.restriction", 0);
pass firefox options as parameter to create driver
   WebDriver driver = new FirefoxDriver(options);
ghazouan badr
  • 374
  • 3
  • 12