Issue
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.
Solution
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);
Answered By - user6340746
Answer Checked By - Mary Flores (JavaFixing Volunteer)