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.
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:
May be you can do it so:
Here was a similar answer how to disable chrome extension in selenium
Got a solution =)
(thanks to chromedriver-users google group):