Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Mobile Testing


Playwright Mobile Testing

Playwright Mobile Testing, mainly including simulating mobile devices, geolocation, permission control, and other features.

In addition to simulating desktop browsers, Playwright also supports mobile device testing, including:

This lets us test on a PC with results close to those on a real phone.


Simulate devices

Playwright provides some common device configurations (based on Chrome DevTools device descriptors), which can be devices invoked.

Example

const

{

chromium

,

devices

}

=

require

(

'playwright'

)

;

(

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

(

)

;

}

)

(

)

;

Simulatable content:


Simulate touch operations

Playwright enables in mobile device mode touch 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();


Simulate geolocation

Can simulate phone GPS location。

Example

const

context

=

await browser.

newContext

(

{

  geolocation

:

{

longitude

:

116.4074

,

latitude

:

39.9042

}

,

// Beijing

  permissions

:

[

'geolocation'

]

}

)

;

const

page

=

await context.

newPage

(

)

;

await page.

goto

(

'https://www.google.com/maps'

)

;


Simulate permissions

Playwright supports granting or denying common permissions, for example:

const context = await browser.newContext({

  permissions: ['geolocation', 'notifications']

});


Simulate orientation switching

By modifying viewport configuration.

await context.newPage({

  viewport: { width: 812, height: 375 }, // 横屏

  isMobile: true

});


Complete example: Mobile testing

Example

const

{

chromium

,

devices

}

=

require

(

'playwright'

)

;

(

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 emulation ...iPhone
page.tap(selector) Touch-tap an element await page.tap('#btn')
page.touchscreen.tap(x,y) Tap 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 built-in common device list, including iPhone series, Pixel series, iPad , etc.

Playwright common device list

Playwright has dozens of built-in common device configurations (based on Chrome DevTools data). Use them via:

const { devices } = require('playwright');

const iphone = devices['iPhone 12'];

to get the device's configuration parameters.

iPhone series

Device name viewport deviceScaleFactor UA (abbreviation) Notes
iPhone SE 375 × 667 2 Safari iOS Classic small-screen model
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 Notched 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 (abbreviation) Notes
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 Recommend

iPad series

Device name viewport deviceScaleFactor UA (abbreviation) Notes
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 (abbreviation) Notes
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

const

{

chromium

,

devices

}

=

require

(

'playwright'

)

;

(

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

(

)

;

}

)

(

)

;