|
| 1 | +import { test as base, chromium, type BrowserContext } from "@playwright/test"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +export const test = base.extend<{ |
| 5 | + context: BrowserContext; |
| 6 | + extensionId: string; |
| 7 | +}>({ |
| 8 | + // eslint-disable-next-line no-empty-pattern |
| 9 | + context: async ({}, use) => { |
| 10 | + const pathToExtension = path.resolve(__dirname, "../dist/ext"); |
| 11 | + const context = await chromium.launchPersistentContext("", { |
| 12 | + headless: false, |
| 13 | + args: ["--headless=new", `--disable-extensions-except=${pathToExtension}`, `--load-extension=${pathToExtension}`], |
| 14 | + }); |
| 15 | + await use(context); |
| 16 | + await context.close(); |
| 17 | + }, |
| 18 | + extensionId: async ({ context }, use) => { |
| 19 | + let [background] = context.serviceWorkers(); |
| 20 | + if (!background) { |
| 21 | + background = await context.waitForEvent("serviceworker"); |
| 22 | + } |
| 23 | + const extensionId = background.url().split("/")[2]; |
| 24 | + |
| 25 | + // Dismiss the first-use guide by navigating to the options page and setting localStorage, |
| 26 | + // then reload to apply the change before any tests run. |
| 27 | + const initPage = await context.newPage(); |
| 28 | + await initPage.goto(`chrome-extension://${extensionId}/src/options.html`); |
| 29 | + await initPage.waitForLoadState("domcontentloaded"); |
| 30 | + await initPage.evaluate(() => { |
| 31 | + localStorage.setItem("firstUse", "false"); |
| 32 | + }); |
| 33 | + await initPage.close(); |
| 34 | + |
| 35 | + await use(extensionId); |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +export const expect = test.expect; |
0 commit comments