fixtures.ts
1 import path from 'node:path' 2 import { setTimeout as sleep } from 'node:timers/promises' 3 import fs from 'fs-extra' 4 import { type BrowserContext, test as base, chromium } from '@playwright/test' 5 import type { Manifest } from 'webextension-polyfill' 6 7 export { name } from '../package.json' 8 9 export const extensionPath = path.join(__dirname, '../extension') 10 11 export const test = base.extend<{ 12 context: BrowserContext 13 extensionId: string 14 }>({ 15 context: async ({ headless }, use) => { 16 // workaround for the Vite server has started but contentScript is not yet. 17 await sleep(1000) 18 const context = await chromium.launchPersistentContext('', { 19 headless, 20 args: [ 21 ...(headless ? ['--headless=new'] : []), 22 `--disable-extensions-except=${extensionPath}`, 23 `--load-extension=${extensionPath}`, 24 ], 25 }) 26 await use(context) 27 await context.close() 28 }, 29 extensionId: async ({ context }, use) => { 30 // for manifest v3: 31 let [background] = context.serviceWorkers() 32 if (!background) 33 background = await context.waitForEvent('serviceworker') 34 35 const extensionId = background.url().split('/')[2] 36 await use(extensionId) 37 }, 38 }) 39 40 export const expect = test.expect 41 42 export function isDevArtifact() { 43 const manifest: Manifest.WebExtensionManifest = fs.readJsonSync(path.resolve(extensionPath, 'manifest.json')) 44 return Boolean( 45 typeof manifest.content_security_policy === 'object' 46 && manifest.content_security_policy.extension_pages?.includes('localhost'), 47 ) 48 }