Skip to main content

Command Palette

Search for a command to run...

Mastering Playwright: Effortless Custom User Agent Integration

Seamlessly Integrating Custom User Agents into Your Playwright Environment

Updated
1 min read
Mastering Playwright: Effortless Custom User Agent Integration
J

I am Senior Test Automation Engineer, who is interested in Open Source & Quality at scale.

The previous article detailed the process for modifying the user agent specifically for tests. However, it did not address how to alter the user agent globally within Playwright.

To update the global user agent in Playwright, refer to the following code snippet. Please adapt your global setup function using this as a guide:

 const config = GlobalConfiguration.getInstance({}),
    browser = await config.getConfig().getBrowser(),
    context = await browser.newContext({
      ...config.getPlaywrightConfig().use,
      userAgent: 'made-up-user-agent',
    });
 await context.tracing.start({ screenshots: true, snapshots: true });
 const page = await context.newPage();
  await context.tracing.stop({
    path: './<trace-file-name>.zip',
  });
  await browser.close();
103 views