Playwright Mobile Emulation
A costume lets an actor look like another character without becoming that person. Mobile emulation dresses a desktop browser as a phone or tablet. Playwright copies the screen size, touch support, and browser identity of real devices, so one computer can check how a website looks and behaves on many phones.
What Emulation Changes
Desktop Browser Emulated Phone
+----------------------+ +--------+
| width 1280 | | 390 |
| mouse pointer | --> | touch |
| desktop user agent | | mobile |
| full menu bar | | burger |
+----------------------+ +--------+
The emulated device carries a viewport, a user agent string, touch events, a pixel density, and a mobile flag. Websites read these values and switch to their phone layout.
The Device Catalog
Playwright ships a list of ready-made profiles called devices. Each profile bundles every setting for one model, such as iPhone 13, Pixel 7, or iPad Pro 11.
Emulating Devices in the Config
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
projects: [
{ name: 'Desktop Chrome', use: { ...devices['Desktop Chrome'] } },
{ name: 'Mobile Safari', use: { ...devices['iPhone 13'] } },
{ name: 'Mobile Chrome', use: { ...devices['Pixel 7'] } },
],
});
Each project runs the whole suite under its own device profile.
Emulating a Device in One File
import { test, expect, devices } from '@playwright/test';
test.use({ ...devices['iPhone 13'] });
test('menu collapses on a phone', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('button', { name: 'Menu' })).toBeVisible();
});
Touch Gestures
Phones respond to taps rather than clicks. The tap method works when the device profile enables touch.
await page.getByRole('button', { name: 'Menu' }).tap();
Custom Screen Sizes
A team may want a size outside the catalog. A manual viewport covers that need.
test.use({ viewport: { width: 360, height: 640 }, isMobile: true, hasTouch: true });
Rotating the Screen
Many profiles offer a landscape twin, such as iPhone 13 landscape. A second project with that profile checks the sideways layout.
Location and Permissions on Phones
Phone apps often ask for location. A context can supply a fake position and grant permission.
test.use({
geolocation: { latitude: 18.5204, longitude: 73.8567 },
permissions: ['geolocation'],
});
A Real-World Scenario
A restaurant chain finds that most orders arrive from phones. The desktop menu shows a wide navigation bar, and the phone version hides it behind a hamburger icon. A mobile test taps the icon, opens the menu, picks "Order Online", and confirms the order page loads. A layout bug that hides the icon on small screens appears in the first run.
Limits of Emulation
- Emulation copies screen and browser traits and leaves out real hardware.
- Battery, camera, and real network speed need physical devices.
- Browser engines differ, so WebKit projects approximate Safari behavior.
Quick Practice Task
Add an iPhone profile project to your config. Visit any responsive site, assert that a menu button appears on the phone profile, and assert that it stays hidden on the desktop profile.
Common Questions
Is emulation enough for release approval?
Emulation catches layout and logic problems. Add a short round of manual checks on real phones before a major release.
Why does tap fail on a desktop profile?
The desktop profile disables touch. Choose a device profile or set hasTouch: true in the context options.
Can a test change orientation mid-run?
Yes. Call page.setViewportSize with swapped width and height to mimic a rotation.
Key Takeaways
- Emulation copies the screen, touch, and identity of real devices.
- The
devicescatalog supplies ready profiles. - Projects run one suite across many screens.
- Real devices still matter for hardware-specific checks.
