Yair
Yair

Reputation: 115

Chrome mobile emulator for Selenium testing- can't use all devices

I'm doing Selenium JAVA tests for mobile using the Chrome driver with its emulator. The problem is that I can't use the most advanced mobile devices like iPhone 7,8 etc even-though its in my drop-down list when manually testing in the devtools. This is the driver init. that works perfectly with many mobile devices:

if(browser.equalsIgnoreCase("mobileIPhone6")){
          Map<String, String> mobileEmulation = new HashMap<String, String>();
          mobileEmulation.put("deviceName", "iPhone 6");
          String exePathChromeDriver = Consts.chromeDriverPath;
          System.setProperty("webdriver.chrome.driver", exePathChromeDriver);
          PropertyLoader.loadCapabilities();
          ChromeOptions chromeOptions = new ChromeOptions();
          chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
          driver = new ChromeDriver(chromeOptions);

BUT, When I change line 3 to "iPhone 7" I'm getting this error:

2018-03-16 21:25:49 INFO  LogLog4j:210 - org.openqa.selenium.WebDriverException: unknown error: cannot parse capability: chromeOptions
from unknown error: cannot parse mobileEmulation
from unknown error: 'iPhone 7' must be a valid device
from unknown error: must be a valid device

Any idea why? many thanks

Upvotes: 2

Views: 3870

Answers (4)

Rohan Ghosh
Rohan Ghosh

Reputation: 1

You can use all the devices mentioned in the 'Emulated Devices' list within Chrome devtools > Devices. If the desired device is not present within the list you can 'Add custom device' (screenshot)- providing basic information, and use the deviceName within your code. In another way, you can directly provide your desired device's basic information (width, height, pixelRatio) within your automation code to use that device for testing. You can get the details code with explanation here.

Upvotes: 0

Pushparaj
Pushparaj

Reputation: 120

This should work:

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

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);

    driver = new ChromeDriver(chromeOptions);
    driver.get("https://www.google.co.uk");

Upvotes: 0

Yair
Yair

Reputation: 115

I've found the solution and it is now working properly. I had to set the Device Metrics object first:

...else if(browser.equalsIgnoreCase("iPhone678")){
      String exePathChromeDriver = Consts.chromeDriverPath;
      System.setProperty("webdriver.chrome.driver", exePathChromeDriver);

      Map<String, Object> deviceMetrics = new HashMap<>();
      deviceMetrics.put("width", 375);
      deviceMetrics.put("height", 667);
      deviceMetrics.put("pixelRatio", 2.0);

      Map<String, Object> mobileEmulation = new HashMap<>();
      mobileEmulation.put("deviceMetrics", deviceMetrics);
      mobileEmulation.put("userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1");

      ChromeOptions chromeOptions = new ChromeOptions();
      chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
      driver = new ChromeDriver(chromeOptions); }

Upvotes: 2

user9413725
user9413725

Reputation: 1

Why not use Dimension class?

Define an enum EmulatedDevices and use following style:

driver.manage().window().setSize(new Dimension(EmulatedDevices.IPHONE7.getWidth(),EmulatedDevices.IPHONE7.getHeight()));

Upvotes: 0

Related Questions