User Agents

Web browsers and everyday web tech, explained

How to Open and Test a Chrome Extension Popup with Playwright

· 4 min read · Settings and Privacy

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to automate the opening and testing of a Chrome extension's popup using Playwright. You'll learn to load the extension, launch a persistent browser context, click the toolbar icon, and interact with the popup's UI—all within a test script. This approach works for MV3 extensions and helps you verify popup behavior in CI or local development.

What You'll Need

  • Node.js (v16 or later) installed
  • Playwright installed (npm init playwright@latest or add to an existing project)
  • A Chrome extension with a popup (manifest.json with action or browser_action)
  • Basic knowledge of JavaScript/TypeScript and Playwright test runner

Step-by-Step

  1. Load the extension in a persistent context. Playwright's default browser context does not support extensions. You must launch a persistent context using launchPersistentContext with a user data directory and specify the extension path via --disable-extensions-except and --load-extension arguments.
  2. Write the test script. Create a test file, e.g., popup.spec.js. Import test and expect from @playwright/test.
  3. Launch the persistent context. In the test, use . The first argument is a user data directory path (use a temporary folder). Set headless: false because Chrome extensions do not work in headless mode (as of Playwright 1.40).
  4. Get the extension ID. After launching, the extension is loaded. To find its ID, you can either read the manifest.json (if it has a key field) or query the service worker. A common trick: use context.serviceWorkers() to get the worker URL, which contains the extension ID.
  5. Open the popup. The popup is a separate page. You can open it by navigating to chrome-extension://<extensionId>/popup.html (or whatever the popup file is named). Use context.newPage() and page.goto(url).
  6. Interact with the popup. Once the popup page is loaded, use Playwright's locators to click buttons, fill inputs, or assert text. For example, await page.click('button#save').
  7. Test the popup's behavior. Verify that clicking the toolbar icon opens the popup by testing the popup page directly—this is the standard approach because Playwright cannot click the browser's toolbar icon (it's part of the browser UI).
  8. Close the context. After tests, close the persistent context to clean up.

Common Mistakes

  • Using a normal browser context. Extensions are not available in a regular browser.newContext(); you must use launchPersistentContext.
  • Running in headless mode. Chrome extensions do not load in headless mode. You must set headless: false.
  • Incorrect extension path. The path in --load-extension must point to the folder containing the manifest.json (not the parent folder).
  • Forgetting to wait for the service worker. The extension might take a moment to register. Use context.waitForEvent('serviceworker') to ensure it's ready.
  • Hardcoding the extension ID. The ID is generated randomly unless you specify a key in manifest. Always derive it dynamically.

Tips

  • Use a unique user data directory per test run to avoid conflicts.
  • Set headless: false and slowMo: 100 to visually debug.
  • If your popup uses chrome.storage, you can pre-seed data via the extension's background script.
  • Use to keep the config in one place.
  • For CI, consider using a virtual display (e.g., Xvfb) since headless won't work.

Troubleshooting

Extension not loading? Check that the manifest is valid and the path is correct. Also ensure you're using Chromium, not Firefox (Firefox has different extension APIs).

Popup page returns 404? Verify the popup file name in manifest (e.g., action.default_popup). Use page.goto with the exact path.

Cannot find extension ID? Use context.serviceWorkers() and parse the URL. Alternatively, add a key field to manifest to fix the ID.

Test fails in CI? Ensure you have a display or use Xvfb. Also, set headless: false even in CI.

Popup doesn't respond to clicks? Wait for the popup to fully load using page.waitForLoadState('networkidle') or a specific element.

Frequently Asked Questions

Can I test the popup by clicking the actual toolbar icon?

No, Playwright cannot interact with the browser's chrome UI (toolbar). You must open the popup URL directly.

Does this work with Manifest V3?

Yes, the steps work for MV3 extensions. The service worker is used to get the extension ID.

Can I run tests in headless mode?

No, Chrome extensions require headed mode. Use a virtual display in CI.

How do I get the extension ID reliably?

Add a key field to your manifest to have a fixed ID, or parse the service worker URL.

Can I use Playwright's codegen for popup testing?

Yes, but you need to manually set the launch options. Codegen won't automatically load extensions.

What if my popup uses iframes or shadow DOM?

Playwright supports both. Use frameLocator() for iframes and page.locator('css=...') with piercing for shadow DOM.