SetEnvironmentProperty to ChromeDriver programmati

2019-05-10 12:21发布

问题:

I am running tests with SE2 and firefoxDriver in a headless enviroment(Xvfb). With FirefoxDriver I can set the DISPLAY environment property very easily:

     FirefoxBinary firefox = new FirefoxBinary(); 
     firefox.setEnvironmentProperty("DISPLAY",":"+DISPLAY); 
     FirefoxProfile firefoxProfile = new ProfilesIni().getProfile(Config.webDriverFirefoxProfile); 
     this.webDriver = new FirefoxDriver(firefox,firefoxProfile); 

How can I do the above with ChromeDriver?

UPDATE: Seems that this is not possible to do yet! There is a similar question with mine here where explains the situation: ChromeDriver Headless

回答1:

looks like they've fixed this (at least for now)

service = new ChromeDriverService.Builder()
        .usingChromeDriverExecutable(new File("/path/to/chromedriver"))
        .usingAnyFreePort()
        .withEnvironment(ImmutableMap.of("DISPLAY",":20"))
        .build();

Here's the revision in the code which adds this method: http://code.google.com/p/selenium/source/detail?r=15232



回答2:

another non-programmatic approach, go to /etc/chromium/default (or /etc/chromium-browser/default or similar, depends on distribution) and set display there :

CHROMIUM_FLAGS="--display :99"