0

I am trying to automate the mobile emulator that chrome provides in selenium, however, the issues that I am facing at the moment are that the browser opens up with the wrong device metrics, I am trying to set the height and width specifically but without any luck here is my code:

Map<String, Object> deviceMetrics = new HashMap<String, Object>();
            deviceMetrics.put("width", 360);
            deviceMetrics.put("height", 640);
            Map<String, Object> mobileEmulation = new HashMap<String, Object>();
            mobileEmulation.put("deviceMetrics", deviceMetrics);
            mobileEmulation.put("deviceName", "Nexus 5");
            Map<String, Object> chromeOptions = new HashMap<String, Object>();
            chromeOptions.put("mobileEmulation", mobileEmulation);
            DesiredCapabilities capabilityMob = DesiredCapabilities.chrome();
            capabilityMob.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
            this.driver = new RemoteWebDriver(_url, capabilityMob);

However I am getting the following error:

org.openqa.selenium.WebDriverException: unknown error: cannot parse capability: chromeOptions from unknown error: cannot parse mobileEmulation from unknown error: 'deviceName' must be used alone

Vahid Boreiri
  • 3,418
  • 1
  • 19
  • 34
Ali Hamadi
  • 673
  • 3
  • 11
  • 26

1 Answers1

0

You don't need to use both a pre-installed device and custom device-parameters. Try similarly:

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 768);
deviceMetrics.put("height", 1024);
deviceMetrics.put("pixelRatio", 2);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 
Safari/9537.53");
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
Hamster
  • 129
  • 3
  • 12