Skip to content

Commit f276aea

Browse files
committed
Add extension-fixtures.mjs
1 parent 15e4f70 commit f276aea

File tree

3 files changed

+82
-136
lines changed

3 files changed

+82
-136
lines changed

examples/content-esm/template.spec.mjs

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,14 @@
11
import path from 'path'
2+
import {fileURLToPath} from 'url'
23
import {execSync} from 'child_process'
3-
import {test as base, chromium} from '@playwright/test'
4+
import {extensionFixtures} from '../extension-fixtures.mjs'
5+
6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = path.dirname(__filename)
48

59
const exampleDir = 'examples/content-esm'
610
const pathToExtension = path.join(__dirname, `dist/chrome`)
711

8-
/**
9-
* @typedef {import('@playwright/test').BrowserContext} BrowserContext
10-
*/
11-
12-
const extensionFixtures = (
13-
/** @type {string} */ pathToExtension,
14-
/** @type {boolean} */ headless
15-
) => {
16-
return base.extend({
17-
/** @type {() => Promise<BrowserContext>} */
18-
context: async (_, use) => {
19-
const context = await chromium.launchPersistentContext('', {
20-
headless: false,
21-
args: [
22-
headless ? `--headless=new` : '',
23-
`--disable-extensions-except=${pathToExtension}`,
24-
`--load-extension=${pathToExtension}`,
25-
'--no-first-run', // Disable Chrome's native first run experience.
26-
'--disable-client-side-phishing-detection', // Disables client-side phishing detection
27-
'--disable-component-extensions-with-background-pages', // Disable some built-in extensions that aren't affected by '--disable-extensions'
28-
'--disable-default-apps', // Disable installation of default apps
29-
'--disable-features=InterestFeedContentSuggestions', // Disables the Discover feed on NTP
30-
'--disable-features=Translate', // Disables Chrome translation, both the manual option and the popup prompt when a page with differing language is detected.
31-
'--hide-scrollbars', // Hide scrollbars from screenshots.
32-
'--mute-audio', // Mute any audio
33-
'--no-default-browser-check', // Disable the default browser check, do not prompt to set it as such
34-
'--no-first-run', // Skip first run wizards
35-
'--ash-no-nudges', // Avoids blue bubble "user education" nudges (eg., "… give your browser a new look", Memory Saver)
36-
'--disable-search-engine-choice-screen', // Disable the 2023+ search engine choice screen
37-
'--disable-features=MediaRoute', // Avoid the startup dialog for `Do you want the application “Chromium.app” to accept incoming network connections?`. Also disables the Chrome Media Router which creates background networking activity to discover cast targets. A superset of disabling DialMediaRouteProvider.
38-
'--use-mock-keychain', // Use mock keychain on Mac to prevent the blocking permissions dialog about "Chrome wants to use your confidential information stored in your keychain"
39-
'--disable-background-networking', // Disable various background network services, including extension updating, safe browsing service, upgrade detector, translate, UMA
40-
'--disable-breakpad', // Disable crashdump collection (reporting is already disabled in Chromium)
41-
'--disable-component-update', // Don't update the browser 'components' listed at chrome://components/
42-
'--disable-domain-reliability', // Disables Domain Reliability Monitoring, which tracks whether the browser has difficulty contacting Google-owned sites and uploads reports to Google.
43-
'--disable-features=AutofillServerCommunicatio', // Disables autofill server communication. This feature isn't disabled via other 'parent' flags.
44-
'--disable-features=CertificateTransparencyComponentUpdate',
45-
'--disable-sync', // Disable syncing to a Google account
46-
'--disable-features=OptimizationHints', // Used for turning on Breakpad crash reporting in a debug environment where crash reporting is typically compiled but disabled. Disable the Chrome Optimization Guide and networking with its service API
47-
'--disable-features=DialMediaRouteProvider', // A weaker form of disabling the MediaRouter feature. See that flag's details.
48-
'--no-pings', // Don't send hyperlink auditing pings
49-
'--enable-features=SidePanelUpdates' // Ensure the side panel is visible. This is used for testing the side panel feature.
50-
].filter((arg) => !!arg)
51-
})
52-
await use(context)
53-
await context.close()
54-
},
55-
/** @type {() => Promise<string>} */
56-
extensionId: async ({context}, use) => {
57-
/*
58-
// for manifest v2:
59-
let [background] = context.backgroundPages()
60-
if (!background)
61-
background = await context.waitForEvent('backgroundpage')
62-
*/
63-
64-
// for manifest v3:
65-
let [background] = context.serviceWorkers()
66-
if (!background) background = await context.waitForEvent('serviceworker')
67-
68-
const extensionId = background.url().split('/')[2]
69-
await use(extensionId)
70-
}
71-
})
72-
}
73-
7412
const test = extensionFixtures(pathToExtension, true)
7513

7614
test.beforeAll(async () => {

examples/extension-fixtures.mjs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {test as base, chromium} from '@playwright/test'
2+
3+
/**
4+
* @typedef {import('@playwright/test').BrowserContext} BrowserContext
5+
*/
6+
const extensionFixtures = (
7+
/** @type {string} */ pathToExtension,
8+
/** @type {boolean} */ headless
9+
/** @returns {import('@playwright/test').TestModifier<{}, {context: BrowserContext, extensionId: string}>} */
10+
) => {
11+
return base.extend({
12+
/** @type {() => Promise<BrowserContext>} */
13+
// eslint-disable-next-line no-empty-pattern
14+
context: async ({}, use) => {
15+
const context = await chromium.launchPersistentContext('', {
16+
headless: false,
17+
args: [
18+
headless ? `--headless=new` : '',
19+
`--disable-extensions-except=${pathToExtension}`,
20+
`--load-extension=${pathToExtension}`,
21+
'--no-first-run', // Disable Chrome's native first run experience.
22+
'--disable-client-side-phishing-detection', // Disables client-side phishing detection
23+
'--disable-component-extensions-with-background-pages', // Disable some built-in extensions that aren't affected by '--disable-extensions'
24+
'--disable-default-apps', // Disable installation of default apps
25+
'--disable-features=InterestFeedContentSuggestions', // Disables the Discover feed on NTP
26+
'--disable-features=Translate', // Disables Chrome translation, both the manual option and the popup prompt when a page with differing language is detected.
27+
'--hide-scrollbars', // Hide scrollbars from screenshots.
28+
'--mute-audio', // Mute any audio
29+
'--no-default-browser-check', // Disable the default browser check, do not prompt to set it as such
30+
'--no-first-run', // Skip first run wizards
31+
'--ash-no-nudges', // Avoids blue bubble "user education" nudges (eg., "… give your browser a new look", Memory Saver)
32+
'--disable-search-engine-choice-screen', // Disable the 2023+ search engine choice screen
33+
'--disable-features=MediaRoute', // Avoid the startup dialog for `Do you want the application “Chromium.app” to accept incoming network connections?`. Also disables the Chrome Media Router which creates background networking activity to discover cast targets. A superset of disabling DialMediaRouteProvider.
34+
'--use-mock-keychain', // Use mock keychain on Mac to prevent the blocking permissions dialog about "Chrome wants to use your confidential information stored in your keychain"
35+
'--disable-background-networking', // Disable various background network services, including extension updating, safe browsing service, upgrade detector, translate, UMA
36+
'--disable-breakpad', // Disable crashdump collection (reporting is already disabled in Chromium)
37+
'--disable-component-update', // Don't update the browser 'components' listed at chrome://components/
38+
'--disable-domain-reliability', // Disables Domain Reliability Monitoring, which tracks whether the browser has difficulty contacting Google-owned sites and uploads reports to Google.
39+
'--disable-features=AutofillServerCommunicatio', // Disables autofill server communication. This feature isn't disabled via other 'parent' flags.
40+
'--disable-features=CertificateTransparencyComponentUpdate',
41+
'--disable-sync', // Disable syncing to a Google account
42+
'--disable-features=OptimizationHints', // Used for turning on Breakpad crash reporting in a debug environment where crash reporting is typically compiled but disabled. Disable the Chrome Optimization Guide and networking with its service API
43+
'--disable-features=DialMediaRouteProvider', // A weaker form of disabling the MediaRouter feature. See that flag's details.
44+
'--no-pings', // Don't send hyperlink auditing pings
45+
'--enable-features=SidePanelUpdates' // Ensure the side panel is visible. This is used for testing the side panel feature.
46+
].filter((arg) => !!arg)
47+
})
48+
await use(context)
49+
await context.close()
50+
},
51+
/** @type {() => Promise<string>} */
52+
extensionId: async ({context}, use) => {
53+
/*
54+
// for manifest v2:
55+
let [background] = context.backgroundPages()
56+
if (!background)
57+
background = await context.waitForEvent('backgroundpage')
58+
*/
59+
60+
// for manifest v3:
61+
let [background] = context.serviceWorkers()
62+
if (!background) background = await context.waitForEvent('serviceworker')
63+
64+
const extensionId = background.url().split('/')[2]
65+
await use(extensionId)
66+
}
67+
})
68+
}
69+
70+
export {extensionFixtures}

examples/new-esm/template.spec.mjs

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,15 @@
11
import path from 'path'
2+
import {fileURLToPath} from 'url'
23
import {execSync} from 'child_process'
4+
import {extensionFixtures} from '../extension-fixtures.mjs'
35

4-
import {test as base, chromium} from '@playwright/test'
6+
// Recreate __dirname for ES Modules
7+
const __filename = fileURLToPath(import.meta.url)
8+
const __dirname = path.dirname(__filename)
59

6-
const exampleDir = 'examples/content-esm'
10+
const exampleDir = 'examples/new-esm'
711
const pathToExtension = path.join(__dirname, `dist/chrome`)
812

9-
/**
10-
* @typedef {import('@playwright/test').BrowserContext} BrowserContext
11-
*/
12-
13-
const extensionFixtures = (
14-
/** @type {string} */ pathToExtension,
15-
/** @type {boolean} */ headless
16-
) => {
17-
return base.extend({
18-
/** @type {() => Promise<BrowserContext>} */
19-
context: async (_, use) => {
20-
const context = await chromium.launchPersistentContext('', {
21-
headless: false,
22-
args: [
23-
headless ? `--headless=new` : '',
24-
`--disable-extensions-except=${pathToExtension}`,
25-
`--load-extension=${pathToExtension}`,
26-
'--no-first-run', // Disable Chrome's native first run experience.
27-
'--disable-client-side-phishing-detection', // Disables client-side phishing detection
28-
'--disable-component-extensions-with-background-pages', // Disable some built-in extensions that aren't affected by '--disable-extensions'
29-
'--disable-default-apps', // Disable installation of default apps
30-
'--disable-features=InterestFeedContentSuggestions', // Disables the Discover feed on NTP
31-
'--disable-features=Translate', // Disables Chrome translation, both the manual option and the popup prompt when a page with differing language is detected.
32-
'--hide-scrollbars', // Hide scrollbars from screenshots.
33-
'--mute-audio', // Mute any audio
34-
'--no-default-browser-check', // Disable the default browser check, do not prompt to set it as such
35-
'--no-first-run', // Skip first run wizards
36-
'--ash-no-nudges', // Avoids blue bubble "user education" nudges (eg., "… give your browser a new look", Memory Saver)
37-
'--disable-search-engine-choice-screen', // Disable the 2023+ search engine choice screen
38-
'--disable-features=MediaRoute', // Avoid the startup dialog for `Do you want the application “Chromium.app” to accept incoming network connections?`. Also disables the Chrome Media Router which creates background networking activity to discover cast targets. A superset of disabling DialMediaRouteProvider.
39-
'--use-mock-keychain', // Use mock keychain on Mac to prevent the blocking permissions dialog about "Chrome wants to use your confidential information stored in your keychain"
40-
'--disable-background-networking', // Disable various background network services, including extension updating, safe browsing service, upgrade detector, translate, UMA
41-
'--disable-breakpad', // Disable crashdump collection (reporting is already disabled in Chromium)
42-
'--disable-component-update', // Don't update the browser 'components' listed at chrome://components/
43-
'--disable-domain-reliability', // Disables Domain Reliability Monitoring, which tracks whether the browser has difficulty contacting Google-owned sites and uploads reports to Google.
44-
'--disable-features=AutofillServerCommunicatio', // Disables autofill server communication. This feature isn't disabled via other 'parent' flags.
45-
'--disable-features=CertificateTransparencyComponentUpdate',
46-
'--disable-sync', // Disable syncing to a Google account
47-
'--disable-features=OptimizationHints', // Used for turning on Breakpad crash reporting in a debug environment where crash reporting is typically compiled but disabled. Disable the Chrome Optimization Guide and networking with its service API
48-
'--disable-features=DialMediaRouteProvider', // A weaker form of disabling the MediaRouter feature. See that flag's details.
49-
'--no-pings', // Don't send hyperlink auditing pings
50-
'--enable-features=SidePanelUpdates' // Ensure the side panel is visible. This is used for testing the side panel feature.
51-
].filter((arg) => !!arg)
52-
})
53-
await use(context)
54-
await context.close()
55-
},
56-
/** @type {() => Promise<string>} */
57-
extensionId: async ({context}, use) => {
58-
/*
59-
// for manifest v2:
60-
let [background] = context.backgroundPages()
61-
if (!background)
62-
background = await context.waitForEvent('backgroundpage')
63-
*/
64-
65-
// for manifest v3:
66-
let [background] = context.serviceWorkers()
67-
if (!background) background = await context.waitForEvent('serviceworker')
68-
69-
const extensionId = background.url().split('/')[2]
70-
await use(extensionId)
71-
}
72-
})
73-
}
74-
7513
const test = extensionFixtures(pathToExtension, true)
7614

7715
test.beforeAll(async () => {
@@ -85,7 +23,7 @@ test('should exist an element with the welcome message text', async ({
8523
}) => {
8624
await page.goto('chrome://newtab/')
8725
const h1 = page.locator('h1')
88-
await test.expect(h1).toHaveText('Welcome to your New Extension')
26+
await test.expect(h1).toHaveText('Welcome to your New ESModule Extension')
8927
})
9028

9129
test('should exist a default color value', async ({page}) => {

0 commit comments

Comments
 (0)