Issue
I have to run some testcases in different mobile view. I'm trying test run in mobile emulation mode using Google Chrome. Testcases are working fine if a device from existing list selected but while try with creating a custom device from here :
href="https://i.stack.imgur.com/PkS3B.png" rel="nofollow noreferrer">
And running the test using below code :
@Test
public void responsive() {
String driverPath = "/Users/resources/drivers/chromedriver";
System.setProperty("webdriver.chrome.driver", driverPath);
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Pixel 3");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
}
Causes below exception:
org.openqa.selenium.InvalidArgumentException: invalid argument: entry 0 of 'firstMatch' is invalid from invalid argument: cannot parse capability: goog:chromeOptions from invalid argument: cannot parse mobileEmulation from invalid argument: 'Pixel 3' must be a valid device from unknown error: must be a valid device Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
Is there any additional thing which require to run test in custom device ?
Solution
Not sure whats wrong with the custom device in chrome. Even i print all devices using below code, It won't print created custom device.
try {
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(
new File("/Users/<username>/Library/Application Support/Google/Chrome/Default/Preferences"),
Map.class);
Map devTools = (Map) map.get("devtools");
Map preferences = (Map) devTools.get("preferences");
String standardEmulatedDeviceList = (String) preferences.get("standardEmulatedDeviceList");
List emulatorMap = mapper.readValue(standardEmulatedDeviceList, List.class);
System.out.println(emulatorMap.size());
for (Object object : emulatorMap) {
Map device = (Map) object;
System.out.println(device.get("title"));
}
} catch (IOException ex) {
System.out.println("Look there is an error..");
}
}
But there is an alternative to do that. A device matrix can be created with required device height, width and pixel ratio. Pass that device matrix in Chrome Option instead of device name. Most important thing, must pass device user agent in order to make it behave like a device screen. This is a very good site to get device specifications.
My working code is here -
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 412);
deviceMetrics.put("height", 797);
deviceMetrics.put("pixelRatio", 1.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 9; Pixel 3 XL Build/PD1A.180621.003) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
Answered By - NarendraR
Answer Checked By - Cary Denson (JavaFixing Admin)