2
2
3
3
Puppeteer can be used for testing Chrome Extensions.
4
4
5
- ::: caution
5
+ ## Load extensions
6
6
7
- Extensions are not available in chrome-headless-shell (headless: 'shell'),
8
- also known as the old headless mode.
7
+ ### Using ` LaunchOptions `
9
8
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' ;
15
12
16
- :::
13
+ const pathToExtension = path .join (process .cwd (), ' my-extension' );
14
+ const browser = await puppeteer .launch ({
15
+ pipe: true ,
16
+ enableExtensions: [pathToExtension ],
17
+ });
18
+ ```
17
19
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
21
21
22
22
``` ts
23
23
import puppeteer from ' puppeteer' ;
24
24
import path from ' path' ;
25
25
26
26
const pathToExtension = path .join (process .cwd (), ' my-extension' );
27
27
const browser = await puppeteer .launch ({
28
- args: [
29
- ` --disable-extensions-except=${pathToExtension } ` ,
30
- ` --load-extension=${pathToExtension } ` ,
31
- ],
28
+ pipe: true ,
29
+ enableExtensions: true ,
32
30
});
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 );
39
33
```
40
34
41
- ::: note
35
+ ## Background contexts
42
36
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.
45
38
46
- :::
39
+ ### Service worker (MV3)
47
40
48
41
``` ts
49
42
import puppeteer from ' puppeteer' ;
50
43
import path from ' path' ;
51
44
52
45
const pathToExtension = path .join (process .cwd (), ' my-extension' );
53
46
const browser = await puppeteer .launch ({
54
- args: [
55
- ` --disable-extensions-except=${pathToExtension } ` ,
56
- ` --load-extension=${pathToExtension } ` ,
57
- ],
47
+ pipe: true ,
48
+ enableExtensions: [pathToExtension ],
58
49
});
59
50
60
51
const workerTarget = await browser .waitForTarget (
@@ -66,11 +57,46 @@ const workerTarget = await browser.waitForTarget(
66
57
67
58
const worker = await workerTarget .worker ();
68
59
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
70
95
await worker .evaluate (' chrome.action.openPopup();' );
71
96
72
97
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.
74
100
target => target .type () === ' page' && target .url ().endsWith (' popup.html' ),
75
101
);
76
102
@@ -81,8 +107,12 @@ const popupPage = popupTarget.asPage();
81
107
await browser .close ();
82
108
```
83
109
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.
85
115
86
- It is not yet possible to test extension content scripts.
116
+ ## Learn more
87
117
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