Playwright · Reliable end-to-end tests
How do you emulate a mobile device?
Short answer
Spread a device descriptor into a project to set viewport, user agent, touch and device scale factor. Emulation is not a real device; use a device cloud when hardware behaviour matters.
Playwright ships descriptors for popular phones and tablets. Spreading one into a project sets the viewport, user agent, device scale factor, isMobile and hasTouch. That is enough to catch layout and touch-target problems without maintaining a device lab.
import { defineConfig, devices } from "@playwright/test";
export default defineConfig({
projects: [
{ name: "mobile-chrome", use: { ...devices["Pixel 7"] } },
{ name: "mobile-safari", use: { ...devices["iPhone 15"] } },
],
});import { test, devices } from "@playwright/test";
test.use({ ...devices["iPhone 15"] });
test("opens the mobile menu", async ({ page }) => {
await page.goto("/");
await page.getByRole("button", { name: "Menu" }).click();
await expect(page.getByRole("navigation")).toBeVisible();
});What emulation changes
- Viewport size and device scale factor
- User agent string
- Touch support through
hasTouch - Mobile viewport behaviour through
isMobile - Some geolocation, locale and permission defaults
Emulation does not reproduce real hardware. It cannot validate actual touch gestures, camera, sensors, battery behaviour or the exact Safari version on a user's phone. It also cannot tell you how a device GPU performs. Use it for responsive layout and interaction checks, and use a real-device cloud for hardware-specific risks.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What does a device descriptor set?
- 2How do you apply a device to one test file?
- 3What can emulation not reproduce?
- 4Is Playwright WebKit the same as Safari on a phone?
Say it in the interview
I spread a device descriptor from the devices list into a project or a per-file use block, which sets viewport, user agent, touch and scale factor. That catches responsive layout and touch-target issues without a device lab. It does not reproduce hardware sensors, camera, performance or the exact Safari build on a real phone. For those risks I use a real-device cloud rather than treating emulation as equivalent.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Playwright mock interview and get scored feedback on depth, clarity and confidence.