Working with Chrome Extensions

Playwright can be used for testing Chrome Extensions.

NOTE Extensions in Chrome / Chromium currently only work in non-headless mode.

The following is code for getting a handle to the background page of an extension whose source is located in ./my-extension:

  1. const { chromium } = require('playwright');
  2. (async () => {
  3. const pathToExtension = require('path').join(__dirname, 'my-extension');
  4. const userDataDir = '/tmp/test-user-data-dir';
  5. const browserContext = await chromium.launchPersistentContext(userDataDir,{
  6. headless: false,
  7. args: [
  8. `--disable-extensions-except=${pathToExtension}`,
  9. `--load-extension=${pathToExtension}`
  10. ]
  11. });
  12. const backgroundPage = browserContext.backgroundPages()[0];
  13. // Test the background page as you would any other page.
  14. await browserContext.close();
  15. })();

NOTE It is not yet possible to test extension popups or content scripts.