Skip to content

Commit 43899e3

Browse files
authored
test: update to mv3 extensions (#13413)
1 parent 2b8ee62 commit 43899e3

4 files changed

Lines changed: 39 additions & 131 deletions

File tree

test/TestExpectations.json

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@
5353
"platforms": ["darwin", "linux", "win32"],
5454
"parameters": ["webDriverBiDi"],
5555
"expectations": ["SKIP"],
56-
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
56+
"comment": "No extensions in WebDriver BiDi so far"
57+
},
58+
{
59+
"testIdPattern": "[extensions.spec] *",
60+
"platforms": ["darwin", "linux", "win32"],
61+
"parameters": ["firefox"],
62+
"expectations": ["SKIP"],
63+
"comment": "Chrome-specific test"
5764
},
5865
{
5966
"testIdPattern": "[headful.spec] *",
@@ -369,6 +376,13 @@
369376
"expectations": ["SKIP"],
370377
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
371378
},
379+
{
380+
"testIdPattern": "[extensions.spec] *",
381+
"platforms": ["darwin", "linux", "win32"],
382+
"parameters": ["chrome", "chrome-headless-shell"],
383+
"expectations": ["SKIP"],
384+
"comment": "No extensions in chrome-headless-shell"
385+
},
372386
{
373387
"testIdPattern": "[fixtures.spec] Fixtures dumpio option should work with pipe option",
374388
"platforms": ["darwin", "linux", "win32"],
@@ -1561,41 +1575,6 @@
15611575
"expectations": ["SKIP"],
15621576
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
15631577
},
1564-
{
1565-
"testIdPattern": "[extensions.spec] extensions background_page target type should be available",
1566-
"platforms": ["darwin", "linux", "win32"],
1567-
"parameters": ["cdp", "chrome"],
1568-
"expectations": ["SKIP"],
1569-
"comment": "See https://github.com/puppeteer/puppeteer/issues/12866"
1570-
},
1571-
{
1572-
"testIdPattern": "[extensions.spec] extensions background_page target type should be available",
1573-
"platforms": ["darwin", "linux", "win32"],
1574-
"parameters": ["cdp", "firefox"],
1575-
"expectations": ["SKIP"],
1576-
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
1577-
},
1578-
{
1579-
"testIdPattern": "[extensions.spec] extensions service_worker target type should be available",
1580-
"platforms": ["darwin", "linux", "win32"],
1581-
"parameters": ["cdp", "firefox"],
1582-
"expectations": ["SKIP"],
1583-
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
1584-
},
1585-
{
1586-
"testIdPattern": "[extensions.spec] extensions target.page() should return a background_page",
1587-
"platforms": ["darwin", "linux", "win32"],
1588-
"parameters": ["cdp", "chrome"],
1589-
"expectations": ["SKIP"],
1590-
"comment": "See https://github.com/puppeteer/puppeteer/issues/12866"
1591-
},
1592-
{
1593-
"testIdPattern": "[extensions.spec] extensions target.page() should return a background_page",
1594-
"platforms": ["darwin", "linux", "win32"],
1595-
"parameters": ["cdp", "firefox"],
1596-
"expectations": ["SKIP"],
1597-
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
1598-
},
15991578
{
16001579
"testIdPattern": "[fixtures.spec] Fixtures should close the browser when the node process closes",
16011580
"platforms": ["darwin", "linux", "win32"],
@@ -3467,13 +3446,6 @@
34673446
"expectations": ["SKIP"],
34683447
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
34693448
},
3470-
{
3471-
"testIdPattern": "[extensions.spec] extensions service_worker target type should be available",
3472-
"platforms": ["darwin", "linux", "win32"],
3473-
"parameters": ["cdp", "chrome", "chrome-headless-shell"],
3474-
"expectations": ["SKIP"],
3475-
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
3476-
},
34773449
{
34783450
"testIdPattern": "[network.spec] network Network Events Page.Events.Request",
34793451
"platforms": ["linux"],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Mock script for background extension
2-
window.MAGIC = 42;
2+
globalThis.MAGIC = 42;

test/assets/simple-extension/manifest.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"name": "Simple extension",
33
"version": "0.1",
44
"background": {
5-
"scripts": ["index.js"]
5+
"service_worker": "index.js",
6+
"type": "module"
67
},
78
"content_scripts": [{
89
"matches": ["<all_urls>"],
910
"css": [],
1011
"js": ["content-script.js"]
1112
}],
1213
"permissions": ["background", "activeTab"],
13-
"manifest_version": 2
14+
"manifest_version": 3
1415
}

test/src/cdp/extensions.spec.ts

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import path from 'path';
88

99
import expect from 'expect';
10-
import type {LaunchOptions} from 'puppeteer-core/internal/node/LaunchOptions.js';
1110

12-
import {getTestState, launch} from '../mocha-utils.js';
11+
import {setupSeparateTestBrowserHooks} from '../mocha-utils.js';
1312

1413
const extensionPath = path.join(
1514
__dirname,
@@ -18,101 +17,37 @@ const extensionPath = path.join(
1817
'assets',
1918
'simple-extension',
2019
);
21-
const serviceWorkerExtensionPath = path.join(
22-
__dirname,
23-
'..',
24-
'..',
25-
'assets',
26-
'serviceworkers',
27-
'extension',
28-
);
2920

3021
describe('extensions', function () {
31-
/* These tests fire up an actual browser so let's
32-
* allow a higher timeout
33-
*/
34-
this.timeout(20_000);
35-
36-
let extensionOptions: LaunchOptions;
37-
const browsers: Array<() => Promise<void>> = [];
38-
39-
beforeEach(async () => {
40-
const {defaultBrowserOptions} = await getTestState({
41-
skipLaunch: true,
42-
});
43-
44-
extensionOptions = Object.assign({}, defaultBrowserOptions, {
22+
const state = setupSeparateTestBrowserHooks(
23+
{
4524
args: [
4625
`--disable-extensions-except=${extensionPath}`,
4726
`--load-extension=${extensionPath}`,
4827
],
28+
},
29+
{createContext: false},
30+
);
31+
32+
it('service_worker target type should be available', async function () {
33+
const {browser} = state;
34+
const serviceWorkerTarget = await browser.waitForTarget(target => {
35+
return target.type() === 'service_worker';
4936
});
50-
});
51-
52-
async function launchBrowser(options: typeof extensionOptions) {
53-
const {browser, close} = await launch(options);
54-
browsers.push(close);
55-
return browser;
56-
}
57-
58-
afterEach(async () => {
59-
await Promise.all(
60-
browsers.map((close, index) => {
61-
delete browsers[index];
62-
return close();
63-
}),
64-
);
65-
});
66-
67-
it('background_page target type should be available', async () => {
68-
const browserWithExtension = await launchBrowser(extensionOptions);
69-
const page = await browserWithExtension.newPage();
70-
const backgroundPageTarget = await browserWithExtension.waitForTarget(
71-
target => {
72-
return target.type() === 'background_page';
73-
},
74-
);
75-
await page.close();
76-
await browserWithExtension.close();
77-
expect(backgroundPageTarget).toBeTruthy();
78-
});
79-
80-
it('service_worker target type should be available', async () => {
81-
const browserWithExtension = await launchBrowser({
82-
args: [
83-
`--disable-extensions-except=${serviceWorkerExtensionPath}`,
84-
`--load-extension=${serviceWorkerExtensionPath}`,
85-
],
86-
});
87-
const page = await browserWithExtension.newPage();
88-
const serviceWorkerTarget = await browserWithExtension.waitForTarget(
89-
target => {
90-
return target.type() === 'service_worker';
91-
},
92-
);
93-
await page.close();
94-
await browserWithExtension.close();
9537
expect(serviceWorkerTarget).toBeTruthy();
9638
});
9739

98-
it('target.page() should return a background_page', async function () {
99-
const browserWithExtension = await launchBrowser(extensionOptions);
100-
const backgroundPageTarget = await browserWithExtension.waitForTarget(
101-
target => {
102-
return target.type() === 'background_page';
103-
},
104-
);
105-
const page = (await backgroundPageTarget.page())!;
106-
expect(
107-
await page.evaluate(() => {
108-
return 2 * 3;
109-
}),
110-
).toBe(6);
40+
it('can evaluate in the service worker', async function () {
41+
const {browser} = state;
42+
const serviceWorkerTarget = await browser.waitForTarget(target => {
43+
return target.type() === 'service_worker';
44+
});
45+
const worker = await serviceWorkerTarget.worker();
11146
expect(
112-
await page.evaluate(() => {
113-
return (globalThis as any).MAGIC;
47+
await worker!.evaluate(() => {
48+
// @ts-expect-error different context.
49+
return globalThis.MAGIC;
11450
}),
11551
).toBe(42);
116-
await browserWithExtension.close();
11752
});
11853
});

0 commit comments

Comments
 (0)