How do I add arguments to ChromeDriver while also

2019-07-09 02:20发布

I'm using ChromeDriver with my JUnit/Selenium tests. I have tests for both desktop and mobile.

For my desktop tests I specify several Arguments to ensure test results are consistent, as so:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions desktopOptions = new ChromeOptions();

desktopOptions.addArguments("start-maximized", "disable-extensions",
    "test-type", "no-default-browser-check", "ignore-certificate-errors");

capabilities.setCapability(ChromeOptions.CAPABILITY, desktopOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);

For my mobile tests I currently use:

Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");

Map<String, Object> mobileOptions = new HashMap<String, Object>();
mobileOptions.put("mobileEmulation", mobileEmulation);

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, mobileOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);

I can't see a way of including the Arguments (such as "disable-extensions") for my mobile tests.

Is there any way I can refactor the mobile method to allow for arguments to be specified?

EDIT: To be a little clearer - I'd like to do something like the below:

        Map<String, String> mobileEmulation = new HashMap<String, String>();
        mobileEmulation.put("deviceName", deviceType);

        Map<String, Object> mobileOptions = new HashMap<String, Object>();
        mobileOptions.put("mobileEmulation", mobileEmulation);

        ChromeOptions chromeArgs = new ChromeOptions();
        chromeArgs.addArguments("disable-extensions",
                "test-type", "no-default-browser-check", "ignore-certificate-errors");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, mobileOptions);
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeArgs);
        driver = new RemoteWebDriver(service.getUrl(), capabilities);

But the capabilities overwrite each other.

3条回答
聊天终结者
2楼-- · 2019-07-09 02:34

Use the setExperimentalOption() method for ChromeOptions

ChromeOptions provides an method setExperimentalOption(String name, Object value). This allows you to set a number of options, including the mobileEmulation parameters you have set.

Putting your code together, you can use this:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions chromeOptions= new ChromeOptions();

chromeOptions.addArguments("start-maximized", "disable-extensions",
"test-type", "no-default-browser-check", "ignore-certificate-errors");

Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);
查看更多
beautiful°
3楼-- · 2019-07-09 02:38

May be you can do it so:

capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));

Here was a similar answer how to disable chrome extension in selenium

查看更多
Explosion°爆炸
4楼-- · 2019-07-09 02:43

Got a solution =)

(thanks to chromedriver-users google group):

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Apple iPhone 6");
Map<String, Object> mobileOptions = new HashMap<String, Object>();
mobileOptions.put("mobileEmulation", mobileEmulation);

mobileOptions.put("args", Arrays.asList("disable-extensions",
"test-type", "no-default-browser-check", "ignore-certificate-errors"));

capabilities.setCapability(ChromeOptions.CAPABILITY, mobileOptions);
driver = new RemoteWebDriver(service.getUrl(), capabilities);
查看更多
登录 后发表回答