1

I want to merge ChromeDriverService with chromeOptions or with DesiredCapabilities for running browser in xvfb.

Below is part of code ChromeDriverService I previously used without selenium grid.

String NodeChromeIncognito = "http://localhost:5558/wd/hub"

         ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
         .usingDriverExecutable(new File("driver_linux/chromedriver"))
         .usingAnyFreePort()
         .withEnvironment(ImmutableMap.of("DISPLAY", ":1")).build();
     chromeDriverService.start();
    // commented because using RemoteWebDriver
    // driver = new ChromeDriver(chromeDriverService);

and below is part code of RemoteWebDriver which I will merge with ChromeDriverService.

DesiredCapabilities cap = null;
String NodeChromeIncognito = "http://localhost:5558/wd/hub";
String NodeChrome = "http://localhost:5557/wd/hub";
String NodeFirefox = "http://localhost:5556/wd/hub";

    if (browserName.equals("chrome")) {
        cap = DesiredCapabilities.chrome();
        cap.setBrowserName("chrome");
        driver = new RemoteWebDriver(new URL(NodeChrome), cap);
    } else if (browserName.equals("firefox")) {
        System.setProperty("webdriver.gecko.driver", "driver_linux/geckodriver");
        cap = DesiredCapabilities.firefox();
        cap.setCapability("marionette", true);
        driver = new RemoteWebDriver(new URL(NodeFirefox), cap);
    }else if (browserName.equals("chromeIncognito")) {
        ChromeOptions option = new ChromeOptions();
        option.addArguments("--incognito");

        cap = DesiredCapabilities.chrome();
        cap.setCapability(ChromeOptions.CAPABILITY, option);
        cap.setPlatform(Platform.WIN10);
        cap.setBrowserName("chrome incognito");
        driver = new RemoteWebDriver(new URL(NodeChromeIncognito), cap);
    }

I know I could use addArguments("--headless") for chrome, but it does not work well with the my webApp. And also I used DesiredCapabilities.merge and error.

How to merge code/configuration ChromeDriverService with ChromeOptions or DesiredCapabilites ?

Mysound Magic
  • 13
  • 1
  • 4
  • 1
    Hey@Mysound Magic welcome to stack overflow please keep in mind question posting should be in proper format and easily understandable. – Dilip Dec 06 '17 at 10:42

1 Answers1

4

As you mentioned you want to merge ChromeDriverService with ChromeOptions or with DesiredCapabilities both can be achieved. But as of current Selenium Java Client releases the following Constructors are Deprecated :

ChromeDriver(Capabilities capabilities)
//and
ChromeDriver(ChromeDriverService service, Capabilities capabilities)

Hence we have to use either of the following options :

  • Option A : Use only ChromeOptions :

    private static ChromeDriverService service;
    private WebDriver driver;
    //code block
    service = new ChromeDriverService.Builder()
     .usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
     .usingAnyFreePort()
     .build();
    chromeDriverService.start();
    
    ChromeOptions option = new ChromeOptions();
    option.addArguments("--incognito");
    driver = new RemoteWebDriver(service.getUrl(), options);
    
  • Option B : Use DesiredCapabilities and then use merge() from MutableCapabilities to merge within ChromeOptions :

    private static ChromeDriverService service;
    private WebDriver driver;
    //code block
    service = new ChromeDriverService.Builder()
     .usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
     .usingAnyFreePort()
     .build();
    chromeDriverService.start();        
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("...", true);
    ChromeOptions option = new ChromeOptions();
    option.addArguments("--incognito");
    option.merge(capabilities);
    driver = new RemoteWebDriver(service.getUrl(), options);
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • thank you so much for your answe. I have try your code, it's work for running browser in xvfb, but not work with the selenium grid. i think because of driver = new RemoteWebDriver(service.getUrl(), options) url not go to node. because in my code i use NodeChromeIncognito. which is url of node. String NodeChromeIncognito = "http://localhost:5558/wd/hub" – Mysound Magic Dec 07 '17 at 04:59
  • @DebanjanB option.merge show me an error The Method Merge(DesiredCapabilities ) is undefined for type of chromeOptions – Dhru 'soni Feb 22 '19 at 07:11