Playwright Mobile Emulation
Playwright Mobile Emulation
Mobile emulation makes a desktop browser behave like a phone.
Playwright sets the screen size, touch support and user agent for you.
The user agent is a text that the browser sends to every website, to say which browser and device it is.
The settings come ready-made in devices.
Devices
devices is a list of ready-made settings for real browsers and phones.
Each device sets the browser, the screen size, the user agent and more.
...devices['Desktop Chrome'] copies all of these settings into the project.
| Device | Browser | Viewport |
|---|---|---|
'Desktop Chrome' | Chromium | 1280 x 720 |
'Desktop Firefox' | Firefox | 1280 x 720 |
'Desktop Safari' | WebKit | 1280 x 720 |
'Pixel 7' | Chromium | 412 x 839 |
'iPhone 15' | WebKit | 393 x 659 |
Test on Mobile
Add phone projects to test how your site works on a small touch screen.
Projects are explained in the Browsers and Projects chapter.
This config has one desktop project and two phone projects:
Example
playwright.config.js
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
{ name: 'Mobile Chrome', use: { ...devices['Pixel 7'] } },
{ name: 'Mobile Safari', use: { ...devices['iPhone 15'] } },
],
This test adds a todo, and then prints the project name and the viewport size.
testInfo, the second argument of the test function, holds information about the running test.
testInfo.project.name is the name of the project that runs the test.
page.viewportSize() returns the size of the page in pixels:
Example
tests/mobile.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('can add a todo', async ({ page }, testInfo) => {
await page.goto('todo.html');
await page.getByPlaceholder('What needs to be done?').fill('Learn Playwright');
await page.getByRole('button', { name: 'Add' }).click();
await expect(page.getByTestId('todo-count')).toHaveText('1 item left');
console.log(testInfo.project.name, page.viewportSize());
});
Output:
Running 3 tests using 3 workers
[1/3] [chromium] › tests/mobile.spec.js:4:1 › can add a todo
[2/3] [Mobile Safari] › tests/mobile.spec.js:4:1 › can add a todo
[3/3] [Mobile Chrome] › tests/mobile.spec.js:4:1 › can add a todo
[Mobile Safari] › tests/mobile.spec.js:4:1 › can add a todo
Mobile Safari { width: 393, height: 659 }
[chromium] › tests/mobile.spec.js:4:1 › can add a todo
chromium { width: 1280, height: 720 }
[Mobile Chrome] › tests/mobile.spec.js:4:1 › can add a todo
Mobile Chrome { width: 412, height: 839 }
3 passed (2.3s)
The same test ran in each project, with the viewport size of that device.
This is emulation: the browser behaves like a phone, with a small screen and touch events.
It is not a real phone, but it finds most layout problems.
A project name can have spaces.
Put it in quotes on the command line: --project="Mobile Safari".