Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST TOOLS MATH

Playwright Mobile Emulation


Share

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.

DeviceBrowserViewport
'Desktop Chrome'Chromium1280 x 720
'Desktop Firefox'Firefox1280 x 720
'Desktop Safari'WebKit1280 x 720
'Pixel 7'Chromium412 x 839
'iPhone 15'WebKit393 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".



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.