How to emulate mobile devices with Playwright
Playwright controls headless desktop browsers that can also emulate mobile devices. And while device emulation can’t replace testing on mobile devices entirely, it’s a practical and quick-to-setup approach to testing mobile scenarios.
Device emulation is well suited to test if your site behaves correctly across multiple viewport sizes and correctly handles user-agent
strings. But if your site relies on device-specific browser features, an iPhone emulation running in a Chromium browser might lead to false positives.
This guide explains how to define viewport sizes, device pixel ratio and user-agent
strings using Playwright.
Defining the user agent string
If your site parses the user agent string to serve a different experience to mobile users, define the userAgent
in your automation scripts.
Defining viewport size and pixel density
If your site follows responsive web design practices and renders elements depending on device viewport size, define a mobile viewport and pixel density.
Use built-in device registries
Playwright includes a built-in device registry to access mobile device characteristics quickly.
Leverage the pre-defined devices to emulate mobile devices.