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

Playwright Device and Browser Emulation

Playwright Device and Browser Emulation

This chapter introduces how to emulate mobile devices, geolocation, time zones, language, color schemes, and other browser environment parameters in Playwright.


Mobile Device Emulation

Playwright includes a large set of built-in device presets that can emulate the viewport, User-Agent, and touch behavior of various phones and tablets.

Using the devices Preset

Pass devices The object can quickly emulate a specific device.

Example

import

{

chromium

,

devices

}

from

'playwright'

;

// Emulate iPhone 15

const

browser

=

await chromium.

launch

(

)

;

const

context

=

await browser.

newContext

(

devices

[

'iPhone 15'

]

)

;

const

page

=

await context.

newPage

(

)

;

await page.

goto

(

'https://www.runoob.com/'

)

;

// The page will render with the iPhone 15 viewport and UA

Using device presets in the configuration file:

Example

// File path: playwright.config.ts

import

{

defineConfig

,

devices

}

from

'@playwright/test'

;

export

default

defineConfig

(

{

  projects

:

[

   

{

      name

:

'mobile'

,

      use

:

{

...

devices

[

'Pixel 7'

]

}

,

   

}

,

 

]

,

}

)

;

Manually Configuring the Viewport

If you don't need a full device preset, you can also specify the viewport size manually.

Example

const

context

=

await browser.

newContext

(

{

  viewport

:

{

width

:

375

,

height

:

812

}

,

   

// Viewport size

  deviceScaleFactor

:

3

,

                     

// Device pixel ratio (Retina display)

  isMobile

:

true

,

                           

// Enable mobile mode

  hasTouch

:

true

,

                           

// Enable touch emulation

  userAgent

:

'Mozilla/5.0 ... Mobile ...'

,

   

// Custom UA

}

)

;


Geolocation Emulation

Playwright can emulate a device's GPS location information.

Example

test

(

'Emulate Beijing Location'

,

async

(

{

context

,

page

}

)

=>

{

 

// Grant geolocation permission

  await context.

grantPermissions

(

[

'geolocation'

]

)

;

 

// Set specific latitude and longitude (Beijing coordinates)

  await context.

setGeolocation

(

{

    latitude

:

39.9042

,

    longitude

:

116.4074

,

 

}

)

;

  await page.

goto

(

'https://www.runoob.com/map'

)

;

 

// The page will obtain Beijing's location information

}

)

;


Time Zone and Language Emulation

Verify internationalization and time-zone-related functionality by setting locale and timezone.

Example

const

context

=

await browser.

newContext

(

{

  locale

:

'zh-CN'

,

               

// Set the browser language to Simplified Chinese

  timezone

:

'Asia/Shanghai'

,

     

// Set the time zone to Shanghai (UTC+8)

}

)

;

// Set globally in the config file

export

default

defineConfig

(

{

  use

:

{

    locale

:

'en-US'

,

    timezone

:

'America/New_York'

,

 

}

,

}

)

;

Common locale values:

locale Language/Region
'zh-CN' Simplified Chinese (China)
'zh-TW' Traditional Chinese (Taiwan)
'en-US' English (United States)
'ja-JP' Japanese
'ko-KR' Korean

Color Scheme Emulation

Emulate light or dark mode to test the prefers-color-scheme effect of media queries.

Example

// Test dark mode

const

darkContext

=

await browser.

newContext

(

{

  colorScheme

:

'dark'

,

}

)

;

// Possible values: 'light', 'dark', 'no-preference'

Setting different color schemes for different projects in the configuration file:

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

  projects

:

[

   

{

      name

:

'chromium-light'

,

      use

:

{

colorScheme

:

'light'

}

,

   

}

,

   

{

      name

:

'chromium-dark'

,

      use

:

{

colorScheme

:

'dark'

}

,

   

}

,

 

]

,

}

)

;


Permission Emulation

Use context.grantPermissions() to grant browser permissions.

Example

await context.

grantPermissions

(

[

 

'geolocation'

,

       

// Geolocation

 

'notifications'

,

     

// Notifications

 

'camera'

,

           

// Camera

 

'microphone'

,

       

// Microphone

 

'clipboard-read'

,

   

// Read clipboard

 

'clipboard-write'

,

   

// Write clipboard

]

)

;

// Clear all permissions

await context.

clearPermissions

(

)

;


Cookie Operations

With BrowserContext you can read, add, and clear cookies.

Example

// Add a cookie

await context.

addCookies

(

[

 

{

    name

:

'runoob_session'

,

    value

:

'session_token_value'

,

    domain

:

'www.runoob.com'

,

    path

:

'/'

,

    httpOnly

:

true

,

    secure

:

true

,

    sameSite

:

'Lax'

,

 

}

,

]

)

;

// Get all cookies

const

cookies

=

await context.

cookies

(

)

;

console.

log

(

cookies

)

;

// Get cookies for a specific URL

const

siteCookies

=

await context.

cookies

(

'https://www.runoob.com'

)

;

// Clear all cookies

await context.

clearCookies

(

)

;

Cookie Parameter Description

Field Type Description
name string Cookie name
value string Cookie value
domain string Domain the cookie applies to
path string Path the cookie applies to, default '/'
expires number Expiration time (Unix timestamp)
httpOnly boolean Whether it is HTTP-only
secure boolean Whether it is HTTPS-only
sameSite string SameSite policy:'Strict' | 'Lax' | 'None'

localStorage and SessionStorage Operations

Pass page.evaluate() Operate on storage in the page context.

Example

// Set localStorage

await page.

evaluate

(

(

)

=>

{

  localStorage.

setItem

(

'runoob_key'

,

'runoob_value'

)

;

}

)

;

// Read localStorage

const

value

=

await page.

evaluate

(

(

)

=>

{

 

return

localStorage.

getItem

(

'runoob_key'

)

;

}

)

;

console.

log

(

value

)

;

// 'runoob_value'

// Clear localStorage

await page.

evaluate

(

(

)

=>

{

  localStorage.

clear

(

)

;

}

)

;

Cookie and Storage operations should all be done page.goto() before this, ensuring the data is available when the page loads.