Chrome allows users to emulate Chrome on a mobile device (e.g. a “Nexus 7” tablet, or an “iPhone 5”) from the desktop version of Chrome, by enabling the Mobile Emulation feature in Chrome DevTools. This feature speeds up web development, allows developers to quickly test how a website will render in a mobile device, without requiring a real device. ChromeDriver can also enable Mobile Emulation, via the “mobileEmulation” capability, specified with a dictionary value.
Just as in the DevTools Emulation panel, there are two ways in ChromeDriver to enable Mobile Emulation: by specifying a known device, or by specifying individual device attributes. The format of the “mobileEmulation” dictionary depends on which method is desired.
Specifying a Known Mobile Device
To enable Mobile Emulation with a specific device name, the “mobileEmulation” dictionary must contain a “deviceName.” Use a valid device name from the DevTools Emulation panel as the value for “deviceName.”
Note: ChromeDriver’s list of known devices is generated from those found in the DevTools Emulation panel. However, it is possible to use a version of ChromeDriver against a version of Chrome that has a newer or older list of devices. If you try to use a device name that ChromeDriver doesn’t recognize, you will see an error: “<your device name> must be a valid device.” To emulate a device that ChromeDriver doesn’t know of, enable Mobile Emulation using individual device metrics, as described below.
Specifying Individual Device Attributes
It is also possible to enable Mobile Emulation by specifying individual attributes. To enable Mobile Emulation this way, the “mobileEmulation” dictionary can contain a “deviceMetrics” dictionary and a “userAgent” string. The following device metrics must be specified in the “deviceMetrics” dictionary:
“width” - the width in pixels of the device’s screen
“height” - the height in pixels of the device’s screen
“pixelRatio” - the device’s pixel ratio
"touch" - whether to emulate touch events (defaults to true, usually does not need to be set)
The phones and tablets that are available under the Mobile Emulation panel can be found in the DevTools source code.
Differences between mobile emulation and real devices
Testing a mobile website on a desktop using mobile emulation can be useful, but testers should be aware that there are many subtle differences such as:
entirely different GPU, which may lead to big performance changes;
mobile UI is not emulated (in particular, the hiding url bar affects page height);
disambiguation popup (where you select one of a few touch targets) is not supported;
many hardware APIs (for example, orientationchange event) are unavailable.