In this section, we will explore how to use Playwright to emulate different devices and geolocations. This is particularly useful for testing how your web application behaves on various devices and in different geographical locations. By the end of this section, you will be able to simulate mobile devices, tablets, and desktops, as well as set geolocation parameters for your tests.

Key Concepts

  1. Device Emulation: Simulating different devices by setting specific screen sizes, user agents, and other device-specific parameters.
  2. Geolocation Emulation: Setting the geographical location for your browser context to test location-based features.
  3. Viewport: The visible area of a web page in a browser, which can be adjusted to mimic different devices.

Device Emulation

Playwright provides a straightforward way to emulate devices using predefined device descriptors. These descriptors include settings for screen size, user agent, and more.

Example: Emulating an iPhone 11

import { chromium, devices } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext({
    ...devices['iPhone 11']
  });
  const page = await context.newPage();
  await page.goto('https://example.com');
  await page.screenshot({ path: 'example-iphone11.png' });
  await browser.close();
})();

Explanation

  • chromium.launch(): Launches a new browser instance.
  • devices['iPhone 11']: Retrieves the predefined settings for an iPhone 11.
  • context.newPage(): Opens a new page in the emulated context.
  • page.goto(): Navigates to the specified URL.
  • page.screenshot(): Captures a screenshot of the page.

Geolocation Emulation

To test location-based features, you can set the geolocation for your browser context.

Example: Setting Geolocation to New York City

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext({
    geolocation: { latitude: 40.7128, longitude: -74.0060 },
    permissions: ['geolocation']
  });
  const page = await context.newPage();
  await page.goto('https://maps.example.com');
  // Perform actions that require geolocation
  await browser.close();
})();

Explanation

  • geolocation: Sets the latitude and longitude for the browser context.
  • permissions: Grants the necessary permissions for geolocation access.

Practical Exercise

Task: Write a script to emulate a Samsung Galaxy S9 and set the geolocation to Paris, France. Navigate to a weather website and take a screenshot.

Solution

import { chromium, devices } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext({
    ...devices['Galaxy S9'],
    geolocation: { latitude: 48.8566, longitude: 2.3522 },
    permissions: ['geolocation']
  });
  const page = await context.newPage();
  await page.goto('https://weather.example.com');
  await page.screenshot({ path: 'weather-paris.png' });
  await browser.close();
})();

Common Mistakes and Tips

  • Forgetting Permissions: Always ensure you grant the necessary permissions when using geolocation.
  • Device Descriptors: Use the correct device descriptor names as defined in Playwright's devices module.

Conclusion

In this section, you learned how to emulate devices and geolocations using Playwright. These techniques are essential for testing responsive designs and location-based features. In the next section, we will delve into organizing tests and test suites to enhance your test automation strategy.

© Copyright 2024. All rights reserved