Skip to content

Commit 712f7c1

Browse files
authored
docs: Update extensions guide with new launch option (#13852)
1 parent 265275d commit 712f7c1

File tree

1 file changed

+65
-35
lines changed

1 file changed

+65
-35
lines changed

docs/guides/chrome-extensions.md

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,50 @@
22

33
Puppeteer can be used for testing Chrome Extensions.
44

5-
:::caution
5+
## Load extensions
66

7-
Extensions are not available in chrome-headless-shell (headless: 'shell'),
8-
also known as the old headless mode.
7+
### Using `LaunchOptions`
98

10-
:::
11-
12-
:::note
13-
14-
See https://developer.chrome.com/docs/extensions/how-to/test/end-to-end-testing for more details.
9+
```ts
10+
import puppeteer from 'puppeteer';
11+
import path from 'path';
1512

16-
:::
13+
const pathToExtension = path.join(process.cwd(), 'my-extension');
14+
const browser = await puppeteer.launch({
15+
pipe: true,
16+
enableExtensions: [pathToExtension],
17+
});
18+
```
1719

18-
The following is code for getting a handle to the
19-
[background page](https://developer.chrome.com/extensions/background_pages) of
20-
an extension whose source is located in `./my-extension`:
20+
### At runtime
2121

2222
```ts
2323
import puppeteer from 'puppeteer';
2424
import path from 'path';
2525

2626
const pathToExtension = path.join(process.cwd(), 'my-extension');
2727
const browser = await puppeteer.launch({
28-
args: [
29-
`--disable-extensions-except=${pathToExtension}`,
30-
`--load-extension=${pathToExtension}`,
31-
],
28+
pipe: true,
29+
enableExtensions: true,
3230
});
33-
const backgroundPageTarget = await browser.waitForTarget(
34-
target => target.type() === 'background_page',
35-
);
36-
const backgroundPage = await backgroundPageTarget.page();
37-
// Test the background page as you would any other page.
38-
await browser.close();
31+
32+
await browser.installExtension(pathToExtension);
3933
```
4034

41-
:::note
35+
## Background contexts
4236

43-
Chrome Manifest V3 extensions have a background ServiceWorker of type
44-
'service_worker', instead of a page of type 'background_page'.
37+
You can get a reference to the extension service worker or background page, which can be useful for evaluating code in the extension context or forcefully terminating the service worker.
4538

46-
:::
39+
### Service worker (MV3)
4740

4841
```ts
4942
import puppeteer from 'puppeteer';
5043
import path from 'path';
5144

5245
const pathToExtension = path.join(process.cwd(), 'my-extension');
5346
const browser = await puppeteer.launch({
54-
args: [
55-
`--disable-extensions-except=${pathToExtension}`,
56-
`--load-extension=${pathToExtension}`,
57-
],
47+
pipe: true,
48+
enableExtensions: [pathToExtension],
5849
});
5950

6051
const workerTarget = await browser.waitForTarget(
@@ -66,11 +57,46 @@ const workerTarget = await browser.waitForTarget(
6657

6758
const worker = await workerTarget.worker();
6859

69-
// Open a popup (available for Canary channels).
60+
// Test the service worker.
61+
62+
await browser.close();
63+
```
64+
65+
### Background page (MV2)
66+
67+
The following is code for getting a handle to the
68+
[background page](https://developer.chrome.com/extensions/background_pages) of
69+
an extension whose source is located in `./my-extension`:
70+
71+
```ts
72+
import puppeteer from 'puppeteer';
73+
import path from 'path';
74+
75+
const pathToExtension = path.join(process.cwd(), 'my-extension');
76+
const browser = await puppeteer.launch({
77+
pipe: true,
78+
enableExtensions: [pathToExtension],
79+
});
80+
const backgroundPageTarget = await browser.waitForTarget(
81+
target => target.type() === 'background_page',
82+
);
83+
const backgroundPage = await backgroundPageTarget.page();
84+
85+
// Test the background page as you would any other page.
86+
87+
await browser.close();
88+
```
89+
90+
## Popup
91+
92+
Access the service worker [as above](#service-worker-mv3). Then:
93+
94+
```ts
7095
await worker.evaluate('chrome.action.openPopup();');
7196

7297
const popupTarget = await browser.waitForTarget(
73-
// Assumes that there is only one page with the URL ending with popup.html and that is the popup created by the extension.
98+
// Assumes that there is only one page with the URL ending with popup.html
99+
// and that is the popup created by the extension.
74100
target => target.type() === 'page' && target.url().endsWith('popup.html'),
75101
);
76102

@@ -81,8 +107,12 @@ const popupPage = popupTarget.asPage();
81107
await browser.close();
82108
```
83109

84-
:::note
110+
## Content scripts
111+
112+
Content scripts are injected as normal. Use `browser.newPage()` and `page.goto()` to navigate to a page where a content script will be injected.
113+
114+
It is not currently possible to evaluate code in the content script isolated world.
85115

86-
It is not yet possible to test extension content scripts.
116+
## Learn more
87117

88-
:::
118+
To learn more, see the documentation on [Chrome for Developers](https://developer.chrome.com/docs/extensions/how-to/test/end-to-end-testing).

0 commit comments

Comments
 (0)