0

Am trying to use GeckoDriver for firefox in selenium-java 3.11. The firefox version is 59. Things are working fine and am able to invoke the firefox driver. The only challenge is I need to add DesiredCapabilities to my firefox instance.

The old method WebDriver driver = new FirefoxDriver(capabilities); in latest selenium version seems to be deprecated and am not able to figure out the new way to specify the capabilities.

Here is the code:

public static WebDriver createDriver(String userAgentKey, String PROXY) {


     File modifyHeaders = null;
     try {
     modifyHeaders =
     ResourceUtils.getFile("classpath:modify_headers-0.7.1.1-fx.xpi");  //am adding the extension using DesiredCapabilities
     } catch (FileNotFoundException e1) {

     e1.printStackTrace();
     }

    Proxy proxy = new Proxy();
    proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY);



    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("javascript.enabled", true);
    profile.setPreference("brower.link.open_newwindow", 1);


    profile.setPreference("modifyheaders.config.active", true);
    profile.setPreference("modifyheaders.config.alwaysOn", true);
    profile.setPreference("modifyheaders.headers.count", 1);
    profile.setPreference("modifyheaders.headers.action0", "Add");
    profile.setPreference("modifyheaders.headers.name0", "Proxy-Authorization");
    profile.setPreference("modifyheaders.headers.value0", "Basic dnNodWtsYUBleHBlZGlhLmNvbTpxZWxsYWZraw==");
    profile.setPreference("modifyheaders.headers.enabled0", true);

    profile.addExtension(modifyHeaders);

    DesiredCapabilities cap = DesiredCapabilities.firefox();
    cap.setCapability(FirefoxDriver.PROFILE, profile);
    cap.setCapability(CapabilityType.PROXY, proxy);
    System.setProperty("webdriver.gecko.driver","/Users/vshukla/Music/geckodriver");

    WebDriver driver = new FirefoxDriver(cap);  //this is deprecated


    return driver;

How can I add the extension to instance using DesiredCapabilities? What are other options to achieve this?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
roger_that
  • 8,273
  • 15
  • 56
  • 94

2 Answers2

0

Use firefox options :

    FirefoxOptions options = new FirefoxOptions();
    options.setCapability(FirefoxDriver.PROFILE, profile);
    options.setCapability(CapabilityType.PROXY, proxy);
cruisepandey
  • 6,860
  • 3
  • 9
  • 24
0

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 :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

System.setProperty("webdriver.gecko.driver","/Users/vshukla/Music/geckodriver");
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability(FirefoxDriver.PROFILE, profile);
cap.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(cap);
WebDriver driver = new FirefoxDriver(opt);

PS : As a reference you can have a look at the discussions in mutablecapabilities tag

DebanjanB
  • 118,661
  • 30
  • 168
  • 217