puppeteer/website/versioned_docs/version-19.6.0/guides/chrome-extensions.md

50 lines
1.2 KiB
Markdown
Raw Normal View History

2022-10-14 12:54:46 +00:00
# Chrome Extensions
Puppeteer can be used for testing Chrome Extensions.
:::caution
2022-10-21 13:52:43 +00:00
Extensions in Chrome/Chromium currently only work in non-headless mode and
experimental Chrome headless mode.
2022-10-14 12:54:46 +00:00
:::
2022-10-21 13:52:43 +00:00
The following is code for getting a handle to the
[background page](https://developer.chrome.com/extensions/background_pages) of
an extension whose source is located in `./my-extension`:
2022-10-14 12:54:46 +00:00
```ts
2022-12-16 13:21:28 +00:00
import puppeteer from 'puppeteer';
import path from 'path';
2022-10-14 12:54:46 +00:00
(async () => {
2022-12-16 13:21:28 +00:00
const pathToExtension = path.join(process.cwd(), 'my-extension');
2022-10-14 12:54:46 +00:00
const browser = await puppeteer.launch({
headless: 'chrome',
args: [
`--disable-extensions-except=${pathToExtension}`,
`--load-extension=${pathToExtension}`,
],
});
const backgroundPageTarget = await browser.waitForTarget(
target => target.type() === 'background_page'
);
const backgroundPage = await backgroundPageTarget.page();
// Test the background page as you would any other page.
await browser.close();
})();
```
:::note
2022-10-21 13:52:43 +00:00
Chrome Manifest V3 extensions have a background ServiceWorker of type
'service_worker', instead of a page of type 'background_page'.
2022-10-14 12:54:46 +00:00
:::
:::note
It is not yet possible to test extension popups or content scripts.
:::