2022-10-13 08:46:53 +00:00
|
|
|
# Chrome Extensions
|
|
|
|
|
|
|
|
Puppeteer can be used for testing Chrome Extensions.
|
|
|
|
|
|
|
|
:::caution
|
|
|
|
|
2024-04-18 10:33:30 +00:00
|
|
|
Extensions are not available in chrome-headless-shell (headless: 'shell'),
|
|
|
|
also known as the old headless mode.
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
:::note
|
|
|
|
|
|
|
|
See https://developer.chrome.com/docs/extensions/how-to/test/end-to-end-testing for more details.
|
2022-10-13 08:46:53 +00:00
|
|
|
|
|
|
|
:::
|
|
|
|
|
2022-10-17 11:07:41 +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-13 08:46:53 +00:00
|
|
|
|
|
|
|
```ts
|
2022-12-09 12:57:39 +00:00
|
|
|
import puppeteer from 'puppeteer';
|
|
|
|
import path from 'path';
|
2022-10-13 08:46:53 +00:00
|
|
|
|
2024-04-18 10:33:30 +00:00
|
|
|
const pathToExtension = path.join(process.cwd(), 'my-extension');
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
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();
|
2022-10-13 08:46:53 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
:::note
|
|
|
|
|
2022-10-17 11:07:41 +00:00
|
|
|
Chrome Manifest V3 extensions have a background ServiceWorker of type
|
|
|
|
'service_worker', instead of a page of type 'background_page'.
|
2022-10-13 08:46:53 +00:00
|
|
|
|
|
|
|
:::
|
|
|
|
|
2024-04-18 10:33:30 +00:00
|
|
|
```ts
|
|
|
|
import puppeteer from 'puppeteer';
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
const pathToExtension = path.join(process.cwd(), 'my-extension');
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
args: [
|
|
|
|
`--disable-extensions-except=${pathToExtension}`,
|
|
|
|
`--load-extension=${pathToExtension}`,
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const workerTarget = await browser.waitForTarget(
|
|
|
|
// Assumes that there is only one service worker created by the extension and its URL ends with background.js.
|
|
|
|
target =>
|
|
|
|
target.type() === 'service_worker' && target.url().endsWith('background.js')
|
|
|
|
);
|
|
|
|
|
|
|
|
const worker = await workerTarget.worker();
|
|
|
|
|
|
|
|
// Open a popup (available for Canary channels).
|
|
|
|
await worker.evaluate('chrome.action.openPopup();');
|
|
|
|
|
|
|
|
const popupTarget = await browser.waitForTarget(
|
|
|
|
// Assumes that there is only one page with the URL ending with popup.html and that is the popup created by the extension.
|
|
|
|
target => target.type() === 'page' && target.url().endsWith('popup.html')
|
|
|
|
);
|
|
|
|
|
|
|
|
const popupPage = popupTarget.asPage();
|
|
|
|
|
|
|
|
// Test the popup page as you would any other page.
|
|
|
|
|
|
|
|
await browser.close();
|
|
|
|
```
|
|
|
|
|
2022-10-13 08:46:53 +00:00
|
|
|
:::note
|
|
|
|
|
2024-04-18 10:33:30 +00:00
|
|
|
It is not yet possible to test extension content scripts.
|
2022-10-13 08:46:53 +00:00
|
|
|
|
|
|
|
:::
|