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:
- Sync
- Async
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:
- Sync
- Async
API reference#
Viewport#
Create a context with custom viewport size:
- Sync
- Async
API reference#
Locale & timezone#
- Sync
- Async
API reference#
Permissions#
Allow all pages in the context to show system notifications:
- Sync
- Async
Grant all pages in the existing context access to current location:
- Sync
- Async
Grant notifications access from a specific domain:
- Sync
- Async
Revoke all permissions:
- Sync
- Async
API reference#
- browser.new_context(**kwargs)
- browser_context.grant_permissions(permissions, **kwargs)
- browser_context.clear_permissions()
Geolocation#
Create a context with "geolocation" permissions granted:
- Sync
- Async
Change the location later:
- Sync
- Async
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.
- Sync
- Async