Devices, viewport and media
A profile in devices sets a consistent group of context options, not just a window size. These are viewport, deviceScaleFactor, isMobile, hasTouch, userAgent and the browser type, so a mobile run is a separate project, and tap() needs hasTouch. It is emulation in a desktop engine, not a real device: no virtual keyboard or real iOS Safari behaviour, so critical mobile flows also need real devices.
Playwright · Emulation
The menu does not open on a phone, yet the desktop tests are green. How do you catch it without a phone?
A user on an iPhone writes to support: the "Menu" button does nothing. The automated tests pass every day, because they run in a 1280×720 window where there is no burger at all: the navigation is laid out in a row. The problem is not the tests but that they have never seen the mobile version. This lesson is about how Playwright models a device, how a profile differs from simply resizing the window, and where the limit of that model lies.
How it works and why. devices is a plain object of browser context options. Playwright creates a separate context for each test, and a profile only presets in it what makes a mobile browser different from a desktop one:
viewportandscreen: the size in CSS pixels, on which media queries depend;deviceScaleFactor: pixel density (2 or 3 on a Retina screen);isMobile: the browser takes themeta viewporttag into account and behaves as a mobile one (not supported in Firefox);hasTouch: touch events are available, and without ittap()does not work;userAgent: read by servers and scripts that detect the device type;defaultBrowserType: WebKit in an iPhone profile, Chromium in a Pixel profile.
A profile is spread into a project's use, so a mobile run becomes an ordinary config project that the report, the --project filter and CI can see. That is why profiles are not written by hand field by field: the set of values is consistent, and a hand-made one easily forgets something.
Window size only or a full profile
viewport: { width: 390, height: 844 }
Width-based media queries fire, but touch is off, the userAgent stays desktop and the mobile viewport mode is not enabled. The test is green, while a real phone behaves differently.
...devices['iPhone 15']
Size, pixel density, userAgent, touch and the mobile mode are set together, and the default browser matches the device.
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
projects: [
{ name: 'desktop', use: { ...devices['Desktop Chrome'] } },
{ name: 'iphone', use: { ...devices['iPhone 15'] } },
{ name: 'pixel', use: { ...devices['Pixel 7'] } },
],
});
An override after ...devices[...] changes individual properties, for example colorScheme: 'dark'. Order matters: whatever comes later wins.
Example: the tap fails although "the size is set". The team decided to make do with a window size, and the test presses the menu with a tap:
// before: size only, no touch
export default defineConfig({
use: { viewport: { width: 390, height: 844 } },
});
test('the menu opens on tap', async ({ page }) => {
await page.goto('/');
await page.getByRole('button', { name: 'Menu' }).tap();
});
Error: locator.tap: hasTouch must be enabled on the browser context before using the touchscreen.
This is a shortened output. The error is direct: the context was created without touch. Here it helps, but in another test of the same "mobile" project the application's touch handlers simply will not fire, and the test passes having checked nothing. The fix: a profile instead of a hand-made size.
// after: a device profile in the project
test('the menu opens on tap', async ({ page, isMobile }) => {
test.skip(!isMobile, 'Mobile projects only');
await page.goto('/');
await page.getByRole('button', { name: 'Menu' }).tap();
await expect(page.getByRole('navigation')).toBeVisible();
});
The isMobile fixture lets one test file run in all projects: desktop ones skip mobile scenarios, and mobile ones execute them.
Media emulation. Besides a device, the browser can be given substitute media queries for the page. They are set in a project's use or during a test:
await page.emulateMedia({ colorScheme: 'dark' }); // dark theme
await page.emulateMedia({ reducedMotion: 'reduce' }); // fewer animations
await page.emulateMedia({ media: 'print' }); // @media print
reducedMotion: 'reduce'works only if the application readsprefers-reduced-motion; if it does, animations go off and the test is more stable.media: 'print'checks the printed version of a statement or a receipt.offline: trueorcontext.setOffline(true)in the middle of a test shows what the application does without a network.javaScriptEnabled: falseis useful for SSR pages.
test('message without a network', async ({ page, context }) => {
await page.goto('/transfers');
await context.setOffline(true);
await page.getByRole('button', { name: 'Refresh' }).click();
await expect(page.getByText('No connection')).toBeVisible();
});
Breakpoints without a profile. To check layout boundaries (375, 768, 1280) page.setViewportSize() is sometimes enough. But some applications read the width once at start, so resizing an already loaded page can differ from opening it at the right size. It is more reliable to move the width into a project or test.use({ viewport }).
Limits. This is emulation in a desktop engine, not a real device.
What emulation checks and what it does not
Catches
Responsive layout and media queries, the mobile menu, touch through tap(), the dark theme, print, offline, userAgent-based behaviour.
Does not catch
Bugs of the real iOS Safari, a virtual keyboard pushing a field, gestures, performance on a weak phone, memory limits.
The rest needs real devices or a cloud farm. For comparison: in Cypress cy.viewport() changes the window size, so the "mobile" mode there is also only a partial model; Playwright's set is wider, but the principle is the same.
Common mistakes.
- Believing that a green mobile project guarantees it works on an iPhone: the engine differs from real Safari, and there is no virtual keyboard.
- Building a "mobile" mode from
viewportalone: withouthasTouchandisMobilethe application behaves differently, and a tap fails. - Using
click()where the logic depends on touch events: a different event arrives, and the bug stays unseen. - Running all tests in all projects without
isMobile: mobile scenarios fail on desktop, desktop ones on mobile. - Sharing screenshots between devices: baselines must be separate for each project.
Check yourself
A project has use: { viewport: { width: 390, height: 844 } }, a test calls tap() and fails. What is wrong and what should change?
Answer
The context was created without hasTouch, so tap is not supported. The simplest fix is a device profile (...devices['iPhone 15']): it sets touch, userAgent and the mobile mode together. A window size alone does not change everything that makes a phone different from a laptop.
The mobile project is green, but iPhone users complain that the keyboard covers the "Amount" field. Why did the test not see it?
Answer
Emulation sets the size, touch and userAgent, but it does not reproduce the virtual keyboard or the specifics of the real iOS Safari: it is Playwright's WebKit build on a desktop. Such bugs are caught on a real device or in a cloud farm, while Playwright keeps what is reproducible: layout and logic.
devices, for example iPhone or Pixel: Playwright sets the size, userAgent and touch. Middle/Senior: I understand it is emulation: the engine is a desktop one, with no virtual keyboard, gestures or real performance, so I additionally check critical mobile flows on real devices. I run one set of tests in all projects and separate scenarios with isMobile, and I check the dark theme, print and offline through emulateMedia and setOffline.