YouTip LogoYouTip

Playwright Mobile Test

Playwright Mobile Testing |

\\n\\n
\\n\\n

Playwright Mobile testing๏ผŒMainly includes mobile device simulation, geolocation, permission control, etc..Features.

\\n\\n

Playwright Besides simulating desktop browsers, also supports Mobile device testing๏ผŒIncludes:

\\n\\n
    \\n
  • Simulate different phone models (screen size, resolution, UA, etc.).๏ผ‰
  • \\n
  • Simulate touch events (tap, swipe, long press)
  • \\n
  • Simulate geographic location (GPS)
  • \\n
  • Simulate permissions๏ผˆCamera, Location, Notification)
  • \\n
\\n\\n

This allows us to test on a PC with results closely resembling a real mobile phone.

\\n\\n
\\n\\n

Emulate device

\\n\\n

Playwright Provides some common device configurations (based on Chrome DevTools Device description), can be configured via devices Call.

\\n\\n

Examples

\\n\\n
const { chromium, devices } = require('playwright');\\n\\n(async () => {\\n\\n  const iPhone = devices['iPhone 12'];// Select device\\n\\n  const browser = await chromium.launch();\\n\\n  const context = await browser.newContext({\\n    ...iPhone// Apply device configuration\\n  });\\n\\n  const page = await context.newPage();\\n\\n  await page.goto('https://www.example.com');\\n\\n  await page.screenshot({ path: 'mobile.png' });\\n\\n  await browser.close();\\n\\n})();
\\n\\n

Supported simulations:

\\n\\n
    \\n
  • Screen resolution๏ผˆwidthใ€heightใ€deviceScaleFactor๏ผ‰
  • \\n
  • User-Agent๏ผˆSimulate mobile UA)
  • \\n
  • Touch eventSupports
  • \\n
  • Switch between landscape and portrait modes
  • \\n
\\n\\n
\\n\\n

Simulate touch operations

\\n\\n

Playwright Enabled in mobile device mode Touch eventใ€‚

\\n\\n

CommonOperation:

\\n\\n

1ใ€Tap

\\n\\n
await page.tap('button#submit');
\\n\\n

2ใ€Slide/Drag

\\n\\n
await page.touchscreen.tap(200, 300); // Click at specified coordinates\\nawait page.touchscreen.swipe(200, 300, 200, 800); // Custom wrapper, simulate slide
\\n\\n

3ใ€Long press

\\n\\n
await page.touchscreen.down(200, 300);\\nawait page.waitForTimeout(1000); // Hold down for 1 Second\\nawait page.touchscreen.up();
\\n\\n
\\n\\n

Simulate geographic location

\\n\\n

Can simulate mobile phone's GPS Locationใ€‚

\\n\\n

Examples

\\n\\n
const context = await browser.newContext({\\n  geolocation: { longitude: 116.4074, latitude: 39.9042 },// Beijing\\n  permissions: ['geolocation']\\n});\\n\\nconst page = await context.newPage();\\n\\nawait page.goto('https://www.google.com/maps');
\\n\\n
\\n\\n

Simulate permissions

\\n\\n

Playwright Supports granting or denying common permissions, such as:

\\n\\n
    \\n
  • geolocation๏ผˆGeolocation)
  • \\n
  • camera๏ผˆCamera)
  • \\n
  • microphone๏ผˆMicrophone)
  • \\n
  • notifications๏ผˆNotification)
  • \\n
\\n\\n
const context = await browser.newContext({ permissions: ['geolocation', 'notifications'] });
\\n\\n
\\n\\n

Simulate landscape and portrait orientation switching

\\n\\n

By modifying viewport Configuration implementation.

\\n\\n
await context.newPage({ viewport: { width: 812, height: 375 }, // Landscape mode\\n  isMobile: true\\n});
\\n\\n
\\n\\n

Complete Examples: Mobile Testing

\\n\\n

Examples

\\n\\n
const { chromium, devices } = require('playwright');\\n\\n(async () => {\\n\\n  const pixel5 = devices['Pixel 5'];\\n\\n  const browser = await chromium.launch({ headless: false });\\n\\n  const context = await browser.newContext({\\n    ...pixel5,\\n    geolocation: { longitude: 121.4737, latitude: 31.2304 },// Shanghai\\n    permissions: ['geolocation']\\n  });\\n\\n  const page = await context.newPage();\\n\\n  await page.goto('https://www.google.com/maps');\\n\\n  await page.screenshot({ path: 'mobile-map.png' });\\n\\n  await browser.close();\\n\\n})();
\\n\\n
\\n\\n

Common APIs

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
APIDescriptionExample
devices['iPhone 12']Get device configurationconst iphone = devices['iPhone 12']
context = browser.newContext({...device})Enable device emulation...iPhone
page.tap(selector)Tap elementawait page.tap('#btn')
page.touchscreen.tap(x,y)Click at coordinatesawait page.touchscreen.tap(100,200)
page.touchscreen.down(x,y)Press fingerawait page.touchscreen.down(100,200)
page.touchscreen.up()Lift fingerawait page.touchscreen.up()
geolocationSet Location{ longitude: 120, latitude: 30 }
permissionsSet permissions['geolocation','camera']
viewportSet screen width and height{ width: 812, height: 375 }
isMobileEnable mobile modeisMobile: true
\\n\\n
\\n\\n

Common devices

\\n\\n

Playwright Built-in Common device list๏ผŒInclude iPhone iPhone series, Pixel series, iPad etc.

\\n\\n

Playwright Common device list

\\n\\n

Playwright Includes dozens of built-in Common device configurations (based on Chrome DevTools data). Accessed via:

\\n\\n
const { devices } = require('playwright');\\nconst iphone = devices['iPhone 12'];
\\n\\n

you can obtain the configuration parameters for that device.

\\n\\n

iPhone Series

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
Device nameviewportdeviceScaleFactorUA (Abbreviation)Notes
iPhone SE375 ร— 6672Safari iOSSmall screen classic model
iPhone 6375 ร— 6672Safari iOSOlder model
iPhone 7375 ร— 6672Safari iOSOlder model
iPhone 8375 ร— 6672Safari iOSCommon
iPhone 8 Plus414 ร— 7363Safari iOSLarge screen
iPhone X375 ร— 8123Safari iOSNotch screen
iPhone 11414 ร— 8962Safari iOSCommon
iPhone 11 Pro375 ร— 8123Safari iOS
iPhone 11 Pro Max414 ร— 8963Safari iOS
iPhone 12390 ร— 8443Safari iOSPopular
iPhone 12 Pro390 ร— 8443Safari iOS
iPhone 12 Pro Max428 ร— 9263Safari iOSLarge screen
iPhone 12 Mini360 ร— 7803Safari iOSSmall screen
iPhone 13390 ร— 8443Safari iOSPopular
iPhone 13 Pro390 ร— 8443Safari iOS
iPhone 13 Pro Max428 ร— 9263Safari iOS
iPhone 13 Mini360 ร— 7803Safari iOS
\\n\\n

Google Pixel Series

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
Device nameviewportdeviceScaleFactorUA (Abbreviation)Notes
Pixel 2411 ร— 7312.6Chrome AndroidOlder model
Pixel 2 XL411 ร— 8233.5Chrome AndroidLarge screen
Pixel 3393 ร— 7862.75Chrome Android
Pixel 3 XL412 ร— 8473.5Chrome Android
Pixel 4353 ร— 7452.75Chrome Android
Pixel 4 XL412 ร— 8693.5Chrome Android
Pixel 5393 ร— 8512.75Chrome AndroidRecommended
\\n\\n

iPad Series

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
Device nameviewportdeviceScaleFactorUA (Abbreviation)Notes
iPad (gen 6)768 ร— 10242Safari iOSCommon
iPad (gen 7)810 ร— 10802Safari iOS
iPad Air820 ร— 11802Safari iOS
iPad Mini768 ร— 10242Safari iOS
iPad Pro 11834 ร— 11942Safari iOS
iPad Pro 12.91024 ร— 13662Safari iOSLarge screen
\\n\\n

Other Android models

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
Device nameviewportdeviceScaleFactorUA (Abbreviation)Notes
Galaxy S5360 ร— 6403Chrome AndroidOlder
Galaxy S8360 ร— 7404Chrome Android
Galaxy S9+412 ร— 8464.5Chrome Android
Galaxy Note 3360 ร— 6403Chrome Android
Galaxy Note 9414 ร— 8464.5Chrome Android
Galaxy A51/71412 ร— 9142.63Chrome AndroidPopular
\\n\\n

Usage

\\n\\n

Examples

\\n\\n
const { chromium, devices } = require('playwright');\\n\\n(async () => {\\n\\n  const iPhone = devices['iPhone 12'];\\n\\n  const browser = await chromium.launch({ headless: false });\\n\\n  const context = await browser.newContext({\\n    ...iPhone,\\n    geolocation: { longitude: 116.4074, latitude: 39.9042 },// Beijing\\n    permissions: ['geolocation']\\n  });\\n\\n  const page = await context.newPage();\\n\\n  await page.goto('https://www.example.com');\\n\\n  await page.screenshot({ path: 'iphone12.png' });\\n\\n  await browser.close();\\n\\n})();
โ† Rust OperatorsPlaywright Network Interceptio โ†’