Device and environment emulation

  • viewport size, device scale factor, touch support
  • locale, timezone
  • color scheme
  • geolocation
  • etc

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.

All pages created in the context above will share the user agent specified:

API reference

Viewport, color scheme

Create a context with custom viewport size:

  1. // Create context with given viewport
  2. const context = await browser.newContext({
  3. viewport: { width: 1280, height: 1024 }
  4. // Resize viewport for individual page
  5. await page.setViewportSize({ width: 1600, height: 1200 });
  6. // Emulate high-DPI
  7. const context = await browser.newContext({
  8. viewport: { width: 2560, height: 1440 },
  9. deviceScaleFactor: 2,
  10. });
  11. // Create device with the dark color scheme:
  12. const context = await browser.newContext({
  13. });
  14. // Change color scheme for the page
  15. await page.emulateMedia({ colorScheme: 'dark' });

API reference

  1. const { chromium, devices } =
  2. const browser = await chromium.launch();
  3. const pixel2 = devices['Pixel 2'];
  4. const context = await browser.newContext({
  5. ...pixel2,
  6. });

All pages created in the context above will share the same device parameters.

API reference

Locale & timezone

API reference

Allow all pages in the context to show system notifications:

  1. const context = await browser.newContext({
  2. permissions: ['notifications'],
  3. });

Grant all pages in the existing context access to current location:

  1. await context.grantPermissions(['geolocation']);

Revoke all permissions:

  1. await context.clearPermissions();

API reference

Geolocation

Create a context with "geolocation" permissions granted:

  1. const context = await browser.newContext({
  2. geolocation: { longitude: 48.858455, latitude: 2.294474 },
  3. permissions: ['geolocation']
  4. });

Change the location later:

API reference