|
| 1 | +import { chromium, type BrowserContext } from "@playwright/test"; |
| 2 | +import path from "path"; |
| 3 | +import { test } from "./"; |
| 4 | + |
| 5 | +const pathToExtension = path.resolve("demos/test-app/dist"); |
| 6 | + |
| 7 | +export const chromeExtensionTest = test.extend<{ |
| 8 | + context: BrowserContext; |
| 9 | + extensionId: string; |
| 10 | + extensionUrl: string; |
| 11 | +}>({ |
| 12 | + // Spread operator is required by PlayWright |
| 13 | + // eslint-disable-next-line no-empty-pattern |
| 14 | + context: async ({}, use) => { |
| 15 | + const context = await chromium.launchPersistentContext("", { |
| 16 | + channel: "chromium", |
| 17 | + args: [ |
| 18 | + `--disable-extensions-except=${pathToExtension}`, |
| 19 | + `--load-extension=${pathToExtension}`, |
| 20 | + "--ignore-certificate-errors", |
| 21 | + "--host-resolver-rules=MAP * localhost:5173, EXCLUDE localhost", |
| 22 | + ], |
| 23 | + }); |
| 24 | + // Close the default about:blank page so that page indices align |
| 25 | + // with what the authorize fixtures expect. |
| 26 | + const [defaultPage] = context.pages(); |
| 27 | + if (defaultPage !== undefined) { |
| 28 | + await defaultPage.close(); |
| 29 | + } |
| 30 | + await use(context); |
| 31 | + await context.close(); |
| 32 | + }, |
| 33 | + extensionId: async ({ context }, use) => { |
| 34 | + let [serviceWorker] = context.serviceWorkers(); |
| 35 | + if (serviceWorker === undefined) { |
| 36 | + serviceWorker = await context.waitForEvent("serviceworker"); |
| 37 | + } |
| 38 | + await use(serviceWorker.url().split("/")[2]); |
| 39 | + }, |
| 40 | + extensionUrl: async ({ extensionId }, use) => { |
| 41 | + await use(`chrome-extension://${extensionId}/index.html`); |
| 42 | + }, |
| 43 | +}); |
0 commit comments