How to change User-Agent for WebdriverIO  & Playwright Tests

How to change User-Agent for WebdriverIO & Playwright Tests

How to ensure Segment analytics is not triggered for automated tests

·

2 min read

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

Did you find this article valuable?

Support Jags by becoming a sponsor. Any amount is appreciated!