Playwright Mobile Testing |
\\n\\n\\n\\n
Playwright Mobile testing๏ผMainly includes mobile device simulation, geolocation, permission control, etc..Features.
\\n\\nPlaywright 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
This allows us to test on a PC with results closely resembling a real mobile phone.
\\n\\n\\n\\n
Emulate device
\\n\\nPlaywright Provides some common device configurations (based on Chrome DevTools Device description), can be configured via devices Call.
Examples
\\n\\nconst { 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\\nSupported 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
Simulate touch operations
\\n\\nPlaywright Enabled in mobile device mode Touch eventใ
\\n\\nCommonOperation:
\\n\\n1ใTap
\\n\\nawait page.tap('button#submit');\\n\\n2ใSlide/Drag
\\n\\nawait page.touchscreen.tap(200, 300); // Click at specified coordinates\\nawait page.touchscreen.swipe(200, 300, 200, 800); // Custom wrapper, simulate slide\\n\\n3ใLong press
\\n\\nawait 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\\nCan simulate mobile phone's GPS Locationใ
\\n\\nExamples
\\n\\nconst 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\\nPlaywright Supports granting or denying common permissions, such as:
\\n\\n- \\n
geolocation๏ผGeolocation) \\n camera๏ผCamera) \\n microphone๏ผMicrophone) \\n notifications๏ผNotification) \\n
const context = await browser.newContext({ permissions: ['geolocation', 'notifications'] });\\n\\n\\n\\n
Simulate landscape and portrait orientation switching
\\n\\nBy modifying viewport Configuration implementation.
await context.newPage({ viewport: { width: 812, height: 375 }, // Landscape mode\\n isMobile: true\\n});\\n\\n\\n\\n
Complete Examples: Mobile Testing
\\n\\nExamples
\\n\\nconst { 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| API | \\nDescription | \\nExample | \\n
|---|---|---|
devices['iPhone 12'] | \\n Get device configuration | \\nconst iphone = devices['iPhone 12'] | \\n
context = browser.newContext({...device}) | \\n Enable device emulation | \\n...iPhone | \\n
page.tap(selector) | \\n Tap element | \\nawait page.tap('#btn') | \\n
page.touchscreen.tap(x,y) | \\n Click at coordinates | \\nawait page.touchscreen.tap(100,200) | \\n
page.touchscreen.down(x,y) | \\n Press finger | \\nawait page.touchscreen.down(100,200) | \\n
page.touchscreen.up() | \\n Lift finger | \\nawait page.touchscreen.up() | \\n
geolocation | \\n Set Location | \\n{ longitude: 120, latitude: 30 } | \\n
permissions | \\n Set permissions | \\n['geolocation','camera'] | \\n
viewport | \\n Set screen width and height | \\n{ width: 812, height: 375 } | \\n
isMobile | \\n Enable mobile mode | \\nisMobile: true | \\n
\\n\\n
Common devices
\\n\\nPlaywright Built-in Common device list๏ผInclude iPhone iPhone series, Pixel series, iPad etc.
\\n\\nPlaywright Common device list
\\n\\nPlaywright Includes dozens of built-in Common device configurations (based on Chrome DevTools data). Accessed via:
\\n\\nconst { devices } = require('playwright');\\nconst iphone = devices['iPhone 12'];\\n\\nyou can obtain the configuration parameters for that device.
\\n\\niPhone Series
\\n\\n| Device name | \\nviewport | \\ndeviceScaleFactor | \\nUA (Abbreviation) | \\nNotes | \\n
|---|---|---|---|---|
iPhone SE | \\n 375 ร 667 | \\n2 | \\nSafari iOS | \\nSmall screen classic model | \\n
iPhone 6 | \\n 375 ร 667 | \\n2 | \\nSafari iOS | \\nOlder model | \\n
iPhone 7 | \\n 375 ร 667 | \\n2 | \\nSafari iOS | \\nOlder model | \\n
iPhone 8 | \\n 375 ร 667 | \\n2 | \\nSafari iOS | \\nCommon | \\n
iPhone 8 Plus | \\n 414 ร 736 | \\n3 | \\nSafari iOS | \\nLarge screen | \\n
iPhone X | \\n 375 ร 812 | \\n3 | \\nSafari iOS | \\nNotch screen | \\n
iPhone 11 | \\n 414 ร 896 | \\n2 | \\nSafari iOS | \\nCommon | \\n
iPhone 11 Pro | \\n 375 ร 812 | \\n3 | \\nSafari iOS | \\n\\n |
iPhone 11 Pro Max | \\n 414 ร 896 | \\n3 | \\nSafari iOS | \\n\\n |
iPhone 12 | \\n 390 ร 844 | \\n3 | \\nSafari iOS | \\nPopular | \\n
iPhone 12 Pro | \\n 390 ร 844 | \\n3 | \\nSafari iOS | \\n\\n |
iPhone 12 Pro Max | \\n 428 ร 926 | \\n3 | \\nSafari iOS | \\nLarge screen | \\n
iPhone 12 Mini | \\n 360 ร 780 | \\n3 | \\nSafari iOS | \\nSmall screen | \\n
iPhone 13 | \\n 390 ร 844 | \\n3 | \\nSafari iOS | \\nPopular | \\n
iPhone 13 Pro | \\n 390 ร 844 | \\n3 | \\nSafari iOS | \\n\\n |
iPhone 13 Pro Max | \\n 428 ร 926 | \\n3 | \\nSafari iOS | \\n\\n |
iPhone 13 Mini | \\n 360 ร 780 | \\n3 | \\nSafari iOS | \\n\\n |
Google Pixel Series
\\n\\n| Device name | \\nviewport | \\ndeviceScaleFactor | \\nUA (Abbreviation) | \\nNotes | \\n
|---|---|---|---|---|
Pixel 2 | \\n 411 ร 731 | \\n2.6 | \\nChrome Android | \\nOlder model | \\n
Pixel 2 XL | \\n 411 ร 823 | \\n3.5 | \\nChrome Android | \\nLarge screen | \\n
Pixel 3 | \\n 393 ร 786 | \\n2.75 | \\nChrome Android | \\n\\n |
Pixel 3 XL | \\n 412 ร 847 | \\n3.5 | \\nChrome Android | \\n\\n |
Pixel 4 | \\n 353 ร 745 | \\n2.75 | \\nChrome Android | \\n\\n |
Pixel 4 XL | \\n 412 ร 869 | \\n3.5 | \\nChrome Android | \\n\\n |
Pixel 5 | \\n 393 ร 851 | \\n2.75 | \\nChrome Android | \\nRecommended | \\n
iPad Series
\\n\\n| Device name | \\nviewport | \\ndeviceScaleFactor | \\nUA (Abbreviation) | \\nNotes | \\n
|---|---|---|---|---|
iPad (gen 6) | \\n 768 ร 1024 | \\n2 | \\nSafari iOS | \\nCommon | \\n
iPad (gen 7) | \\n 810 ร 1080 | \\n2 | \\nSafari iOS | \\n\\n |
iPad Air | \\n 820 ร 1180 | \\n2 | \\nSafari iOS | \\n\\n |
iPad Mini | \\n 768 ร 1024 | \\n2 | \\nSafari iOS | \\n\\n |
iPad Pro 11 | \\n 834 ร 1194 | \\n2 | \\nSafari iOS | \\n\\n |
iPad Pro 12.9 | \\n 1024 ร 1366 | \\n2 | \\nSafari iOS | \\nLarge screen | \\n
Other Android models
\\n\\n| Device name | \\nviewport | \\ndeviceScaleFactor | \\nUA (Abbreviation) | \\nNotes | \\n
|---|---|---|---|---|
Galaxy S5 | \\n 360 ร 640 | \\n3 | \\nChrome Android | \\nOlder | \\n
Galaxy S8 | \\n 360 ร 740 | \\n4 | \\nChrome Android | \\n\\n |
Galaxy S9+ | \\n 412 ร 846 | \\n4.5 | \\nChrome Android | \\n\\n |
Galaxy Note 3 | \\n 360 ร 640 | \\n3 | \\nChrome Android | \\n\\n |
Galaxy Note 9 | \\n 414 ร 846 | \\n4.5 | \\nChrome Android | \\n\\n |
Galaxy A51/71 | \\n 412 ร 914 | \\n2.63 | \\nChrome Android | \\nPopular | \\n
Usage
\\n\\nExamples
\\n\\nconst { 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})();
YouTip