Playwright Mobile Testing
Playwright Mobile Testing, mainly including functions such as simulating mobile devices, geolocation, and permission control.
In addition to simulating desktop browsers, Playwright also supportsmobile device testing, including:
- Simulating different phone models (screen size, resolution, UA, etc.)
- Simulating touch events (tap, swipe, long press)
- Simulating geolocation (GPS)
- Simulating permissions (camera, location, notifications)
This allows us to test on a PC with effects close to a real phone.
Simulating Devices
Playwright provides some common device configurations (based onChrome DevToolsdevice descriptors), and can bedevicesinvoked.
Example
(async () => {
const iPhone = devices['iPhone 12']; // Select device
const browser = await chromium.launch();
const context = await browser.newContext({
...iPhone // Apply device configuration
});
const page = await context.newPage();
await page.goto('https://www.example.com');
await page.screenshot({ path: 'mobile.png' });
await browser.close();
})();
What can be simulated:
- Screen resolution(width、height、deviceScaleFactor)
- User-Agent(simulated mobile UA)
- Touch event support
- Landscape/portrait switching
Simulating Touch Operations
Playwright enablestouch events。
Common operations:
1. Tap
await page.tap('button#submit');
2. Swipe/Drag
await page.touchscreen.tap(200, 300); // 点击指定坐标 await page.touchscreen.swipe(200, 300, 200, 800); // 自定义封装,模拟滑动
3. Long press
await page.touchscreen.down(200, 300); await page.waitForTimeout(1000); // 按住 1 秒 await page.touchscreen.up();
Simulating Geolocation
Can simulate the phone'sGPS positioning。
Example
geolocation: { longitude: 116.4074, latitude: 39.9042 }, // Beijing
permissions: ['geolocation']
});
const page = await context.newPage();
await page.goto('https://www.google.com/maps');
Simulating Permissions
Playwright supports granting or denying common permissions, for example:
geolocation(Geolocation)camera(Camera)microphone(Microphone)notifications(Notifications)
const context = await browser.newContext({
permissions: ['geolocation', 'notifications']
});
Simulating Landscape/Portrait Switch
By modifyingviewportthe configuration.
await context.newPage({
viewport: { width: 812, height: 375 }, // 横屏
isMobile: true
});
Complete Example: Mobile Testing
Example
(async () => {
const pixel5 = devices['Pixel 5'];
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext({
...pixel5,
geolocation: { longitude: 121.4737, latitude: 31.2304 }, // Shanghai
permissions: ['geolocation']
});
const page = await context.newPage();
await page.goto('https://www.google.com/maps');
await page.screenshot({ path: 'mobile-map.png' });
await browser.close();
})();
Common APIs
| API | Description | Example |
|---|---|---|
devices['iPhone 12'] |
Get device configuration | const iphone = devices['iPhone 12'] |
context = browser.newContext({...device}) |
Enable device simulation | ...iPhone |
page.tap(selector) |
Tap element | await page.tap('#btn') |
page.touchscreen.tap(x,y) |
Click at coordinates | await page.touchscreen.tap(100,200) |
page.touchscreen.down(x,y) |
Finger down | await page.touchscreen.down(100,200) |
page.touchscreen.up() |
Finger up | await page.touchscreen.up() |
geolocation |
Set geolocation | { longitude: 120, latitude: 30 } |
permissions |
Set permissions | ['geolocation','camera'] |
viewport |
Set screen width and height | { width: 812, height: 375 } |
isMobile |
Enable mobile mode | isMobile: true |
Common Devices
Playwright has a built-in list of common devices, includingiPhone series, Pixel series, iPad, etc.
Playwright Common Device List
Playwright includes dozens of common device configurations (based on Chrome DevTools data). When using, via:
const { devices } = require('playwright');
const iphone = devices['iPhone 12'];
You can get the configuration parameters of that device.
iPhone Series
| Device Name | viewport | deviceScaleFactor | UA (abbrev.) | Remarks |
|---|---|---|---|---|
iPhone SE |
375 × 667 | 2 | Safari iOS | Small screen classic |
iPhone 6 |
375 × 667 | 2 | Safari iOS | Older model |
iPhone 7 |
375 × 667 | 2 | Safari iOS | Older model |
iPhone 8 |
375 × 667 | 2 | Safari iOS | Common |
iPhone 8 Plus |
414 × 736 | 3 | Safari iOS | Large screen |
iPhone X |
375 × 812 | 3 | Safari iOS | Notch screen |
iPhone 11 |
414 × 896 | 2 | Safari iOS | Common |
iPhone 11 Pro |
375 × 812 | 3 | Safari iOS | |
iPhone 11 Pro Max |
414 × 896 | 3 | Safari iOS | |
iPhone 12 |
390 × 844 | 3 | Safari iOS | Popular |
iPhone 12 Pro |
390 × 844 | 3 | Safari iOS | |
iPhone 12 Pro Max |
428 × 926 | 3 | Safari iOS | Large screen |
iPhone 12 Mini |
360 × 780 | 3 | Safari iOS | Small screen |
iPhone 13 |
390 × 844 | 3 | Safari iOS | Popular |
iPhone 13 Pro |
390 × 844 | 3 | Safari iOS | |
iPhone 13 Pro Max |
428 × 926 | 3 | Safari iOS | |
iPhone 13 Mini |
360 × 780 | 3 | Safari iOS |
Google Pixel Series
| Device Name | viewport | deviceScaleFactor | UA (abbrev.) | Remarks |
|---|---|---|---|---|
Pixel 2 |
411 × 731 | 2.6 | Chrome Android | Older model |
Pixel 2 XL |
411 × 823 | 3.5 | Chrome Android | Large screen |
Pixel 3 |
393 × 786 | 2.75 | Chrome Android | |
Pixel 3 XL |
412 × 847 | 3.5 | Chrome Android | |
Pixel 4 |
353 × 745 | 2.75 | Chrome Android | |
Pixel 4 XL |
412 × 869 | 3.5 | Chrome Android | |
Pixel 5 |
393 × 851 | 2.75 | Chrome Android | Recommended |
iPad Series
| Device Name | viewport | deviceScaleFactor | UA (abbrev.) | Remarks |
|---|---|---|---|---|
iPad (gen 6) |
768 × 1024 | 2 | Safari iOS | Common |
iPad (gen 7) |
810 × 1080 | 2 | Safari iOS | |
iPad Air |
820 × 1180 | 2 | Safari iOS | |
iPad Mini |
768 × 1024 | 2 | Safari iOS | |
iPad Pro 11 |
834 × 1194 | 2 | Safari iOS | |
iPad Pro 12.9 |
1024 × 1366 | 2 | Safari iOS | Large screen |
Other Android Models
| Device Name | viewport | deviceScaleFactor | UA (abbrev.) | Remarks |
|---|---|---|---|---|
Galaxy S5 |
360 × 640 | 3 | Chrome Android | Older |
Galaxy S8 |
360 × 740 | 4 | Chrome Android | |
Galaxy S9+ |
412 × 846 | 4.5 | Chrome Android | |
Galaxy Note 3 |
360 × 640 | 3 | Chrome Android | |
Galaxy Note 9 |
414 × 846 | 4.5 | Chrome Android | |
Galaxy A51/71 |
412 × 914 | 2.63 | Chrome Android | Popular |
Usage
Example
(async () => {
const iPhone = devices['iPhone 12'];
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext({
...iPhone,
geolocation: { longitude: 116.4074, latitude: 39.9042 }, // Beijing
permissions: ['geolocation']
});
const page = await context.newPage();
await page.goto('https://www.example.com');
await page.screenshot({ path: 'iphone12.png' });
await browser.close();
})();