Device and environment emulation
Playwright allows overriding various parameters of the device where the browser is running:
- viewport size, device scale factor, touch support
- locale, timezone
- color scheme
- geolocation
Most of these parameters are configured during the browser context construction, but some of them such as viewport size can be changed for individual pages.
Devices#
Playwright comes with a registry of device parameters for selected mobile devices. It can be used to simulate browser behavior on a mobile device:
All pages created in the context above will share the same device parameters.
API reference#
User agent#
All pages created in the context above will share the user agent specified:
API reference#
Viewport#
Create a context with custom viewport size:
API reference#
Locale & timezone#
API reference#
Permissions#
Allow all pages in the context to show system notifications:
Grant all pages in the existing context access to current location:
Grant notifications access from a specific domain:
Revoke all permissions:
API reference#
browser.newContext([options])browserContext.grantPermissions(permissions[][, options])browserContext.clearPermissions()
Geolocation#
Create a context with "geolocation" permissions granted:
Change the location later:
Note you can only change geolocation for all pages in the context.
API reference#
Color scheme and media#
Create a context with dark or light mode. Pages created in this context will follow this color scheme preference.