Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update the extensions guide #12290

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 57 additions & 19 deletions docs/guides/chrome-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ Puppeteer can be used for testing Chrome Extensions.

OrKoN marked this conversation as resolved.
Show resolved Hide resolved
:::caution

Extensions in Chrome/Chromium currently only work in non-headless mode and
experimental Chrome headless mode.
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.

:::

Expand All @@ -17,22 +23,19 @@ an extension whose source is located in `./my-extension`:
import puppeteer from 'puppeteer';
import path from 'path';

(async () => {
const pathToExtension = path.join(process.cwd(), 'my-extension');
const browser = await puppeteer.launch({
headless: 'new',
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();
})();
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();
```

:::note
Expand All @@ -42,8 +45,43 @@ Chrome Manifest V3 extensions have a background ServiceWorker of type

:::

```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();
```

:::note

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what the content script callout here was referring to? Or to put it another way, what would we want to exist before we could remove this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is about executing code in the content scripts created by extensions


:::