Skip to main content

Command Palette

Search for a command to run...

How to change User-Agent for WebdriverIO & Playwright Tests

How to ensure Segment analytics is not triggered for automated tests

Updated
2 min read
How to change User-Agent for WebdriverIO  & Playwright Tests
J

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

One may want to change/update the user agent for various use cases.

One such use case is :

  • Ensuring user analytics (e.g. Segment) is not triggered for your automated test cases. Not doing this may result in a higher analytics bill.

Solution:

The solution is to update your config and leave the tests untouched.

Config for WebdriverIO BrowserStack Run

export const config: WebdriverIO.Config = {
  runner: 'local',
  specs: ['./features/**/*.feature'],
  exclude: [],
  maxInstances: 2,
  capabilities: [
    {
      'bstack:options': {
        os: process.env.OS_NAME || 'OS X',
        osVersion: process.env.OS_VERSION || 'Big Sur',
      },
      browserName: 'Chrome',
      browserVersion: 'latest',
      'goog:chromeOptions': {
        args: ['user-agent=madeup-user-agent-name'],
      },
    },
  ],
}

Config for WebDriverIO Local Run

export const config: WebdriverIO.Config = {
  runner: 'local',
  specs: ['./features/**/*.feature'],
  exclude: [],
  maxInstances: 10,
  capabilities: [
    {
      maxInstances: 1,
      browserName: 'chrome',
      acceptInsecureCerts: true,
      'goog:chromeOptions': {
        args: ['user-agent=madeup-user-agent-name'],
      },
    },
  ],
}

Config for Playwright Local Run

const config: PlaywrightTestConfig = {
   use: {
    ignoreHTTPSErrors: true,
    contextOptions: {
      bypassCSP: true,
      recordVideo: {
        dir: 'e2e/videos',
      },
      userAgent: 'madeup-user-agent-name',
    },
    headless: true,
    screenshot: 'only-on-failure',
    trace: 'on',
    video: 'retain-on-failure',
  },
};
export default config;

Config for Playwright Docker Run

import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  use: {
    headless: true,
    navigationTimeout: 30000,
    actionTimeout: 10000,
    screenshot: 'only-on-failure',
    ignoreHTTPSErrors: true,
    contextOptions: {
      bypassCSP: true,
    },
  },
  projects: [
    {
      name: 'Chrome',
      use: {
        browserName: 'chromium',
        contextOptions: {
          ignoreHTTPSErrors: true,
          userAgent: 'madeup-user-agent-name',
        },
      },
    },
  ],
};
export default config;

Resources

Testing

Part 3 of 6

Discover the power of testing in software development, from manual to automated techniques. Learn how to ensure quality and reliability, and explore the latest trends and best practices.

Up next

How to add screenshot on failures for allure reports for WebDriverIO using Typescript

How to add screenshot in allure reports on test failures