Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Emulating LaunchDarkly Downtime in Playwright

You can use a fixture in Playwright to abort all network calls to LaunchDarkly before the test runs. This allows for flexibility of the user to:

  1. Invoke running different tests when the LaunchDarkly connection is blocked. The customer may expect different behavior.
  2. Allows for a user to run tests as they were prior, assuming the connection to LaunchDarkly is made.
  3. The user can invoke one test against both scenarios. For example, my application should return a 200 whether I can connect to LaunchDarkly or not.

Create a fixture

const { test: base } = require('@playwright/test');

const blockLaunchDarkly = base.extend({
  page: async ({ page }, use) => {
    // Block all requests to LaunchDarkly before the test runs
    await page.route('**/*launchdarkly.com/**', route => route.abort());
    await use(page);
  },
});

const connectedLaunchDarkly = base;

module.exports = {
  blockLaunchDarkly,
  connectedLaunchDarkly,
  expect: base.expect,
};

Example test

const { blockLaunchDarkly, connectedLaunchDarkly, expect } = require('./fixtures');

/**
 * Shared tests that run against BOTH scenarios:
 * - LaunchDarkly blocked (unreachable)
 * - LaunchDarkly connected (working)
 * 
 * This ensures the app works regardless of LaunchDarkly availability.
 */

const scenarios = [
  { testFn: blockLaunchDarkly, name: 'LD blocked' },
  { testFn: connectedLaunchDarkly, name: 'LD connected' },
];

// Run the same test for each scenario
for (const { testFn, name } of scenarios) {
  testFn(`app returns 200 (${name})`, async ({ page }) => {
    const response = await page.goto('/');
    expect(response.status()).toBe(200);
  });
}