IHOR.DEVKNOWLEDGE_BASE
MENU

~/blog / Publishing / end-to-end-testing-chrome-extensions-with-playwright.md

// guide · 3 min read

End-to-End Testing Chrome Extensions with Playwright

Load the built extension in Playwright’s bundled Chromium persistent context, test reopened popup settings, and separate page tests from toolbar permission checks.

Quick answer

Launch Playwright’s bundled Chromium with a persistent context and load the built extension directory. Use the documented chromium channel for extension-capable headless execution, or run headed. Current Playwright documentation notes that Google Chrome and Microsoft Edge removed the side-loading command-line flags needed by this setup.

Test an observable user result in the installed package: a setting survives reopening, an injected result appears on a fixture page, or an exported report has the expected contents. A test of a directly opened popup.html proves its page behavior; it does not reproduce a toolbar invocation or automatically grant activeTab.

Practical implementation steps

  1. Build first and point the browser at the distributable root containing manifest.json. Testing source while uploading a different directory misses packaging failures. Pin the Playwright version and install its matching browser in CI.
  2. Use a fresh temporary persistent profile for independent tests. Share a profile only for a deliberate restart or upgrade scenario; parallel workers must not race on one profile directory.
  3. Retrieve the extension identifier from its worker URL when the example extension has a worker. Do not hardcode a developer’s unpacked identifier. Workerless extensions need a different identification strategy.
  4. Assert visible outcomes with retrying locators. Wait for an acknowledged save rather than sleeping for an arbitrary number of milliseconds. Close and reopen the extension page to test persistence.
  5. Add a controlled local HTTP fixture for content-script behavior. Test navigation, denied access, repeated invocation, and restoration separately. Keep live third-party sites out of deterministic CI checks.

A LocaleStress-style report flow needs additional tests for skipped nodes, restoration conflicts, and optional history; one settings test cannot establish those behaviors. Keep browser-toolbar permission checks and Store-install checks explicitly identified in your release evidence.

Implementation example

This example assumes an MV3 worker and a popup with a labelled Highlights checkbox, Save button, and #status acknowledgement. Adapt selectors to your product. The example has not been run against a portfolio extension package.

import { test, expect, chromium } from "@playwright/test";
import path from "node:path";

test("saved preference survives reopening", async () => {
  const extensionPath = path.resolve("dist");
  const context = await chromium.launchPersistentContext("", {
    channel: "chromium",
    args: [
      `--disable-extensions-except=${extensionPath}`,
      `--load-extension=${extensionPath}`,
    ],
  });
  try {
    const worker = context.serviceWorkers()[0]
      ?? await context.waitForEvent("serviceworker");
    const id = new URL(worker.url()).host;
    let page = await context.newPage();
    await page.goto(`chrome-extension://${id}/popup.html`);
    await page.getByLabel("Highlights").uncheck();
    await page.getByRole("button", { name: "Save", exact: true }).click();
    await expect(page.locator("#status")).toHaveText("Saved.");
    await page.close();
    page = await context.newPage();
    await page.goto(`chrome-extension://${id}/popup.html`);
    await expect(page.getByLabel("Highlights")).not.toBeChecked();
  } finally {
    await context.close();
  }
});

Tradeoffs and alternatives

Headless Chromium is useful for repeatable CI; headed review remains useful for actual toolbar and side-panel interactions. Traces and screenshots help explain failures, but may contain page data. Use controlled fixtures and retain artifacts deliberately. Unit tests remain cheaper for report transformations and validation.

Common mistakes

  • Using ordinary nonpersistent contexts for extension loading.
  • Assuming every Chrome channel accepts side-loading flags.
  • Treating direct popup navigation as an activeTab user gesture.
  • Sharing one profile directory across parallel tests.
  • Sleeping instead of waiting for a visible completion condition.

Production checklist

  • The tested directory is the packaged artifact.
  • Profiles and fixtures are isolated.
  • Assertions await user-visible results.
  • Toolbar permissions and denied pages have separate coverage.
  • CI records Playwright/browser versions and useful failure artifacts.

FAQ

Can a screenshot prove the extension works?

It proves a rendered state, not persistence, permissions, or recovery. Combine it with behavior assertions.

Should every test load the extension?

No. Keep pure decisions in the unit suite and use the browser for real integration boundaries.

What should I automate first?

One primary result and one failure state in the actual built package, with an explicit note about any toolbar interaction still requiring a manual check.

Primary sources

Ihor Kharchyshyn

Independent full-stack product engineer writing source-backed implementation guides for browser extensions and modern web products.

Need this implemented in a real product?

Browser tools with a clear purpose, focused permissions, and a maintainable Manifest V3 architecture.