visual-check.spec.ts

1.7 KB · raw

1import { test, BrowserContext } from "@playwright/test";2import { fileURLToPath } from "url";3import { dirname, join } from "path";4import os from "os";5import fs from "fs/promises";67const __filename = fileURLToPath(import.meta.url);8const __dirname = dirname(__filename);9const EXTENSION_PATH = join(__dirname, "..", "dist");1011async function waitForSW(ctx: BrowserContext, maxWait = 15000): Promise<string> {12  const start = Date.now();13  let sw = ctx.serviceWorkers()[0];14  while (!sw && Date.now() - start < maxWait) {15    await new Promise(r => setTimeout(r, 500));16    sw = ctx.serviceWorkers()[0];17  }18  if (!sw) throw new Error(`No SW after ${maxWait}ms`);19  return new URL(sw.url()).host;20}2122async function makeContext() {23  const { chromium } = await import("playwright");24  const userDataDir = join(os.tmpdir(), `peach-diag-${Date.now()}`);25  await fs.mkdir(userDataDir, { recursive: true });26  return chromium.launchPersistentContext(userDataDir, {27    headless: false,28    args: [`--load-extension=${EXTENSION_PATH}`, `--disable-extensions-except=${EXTENSION_PATH}`],29  });30}3132test("visual check - extension popup renders correctly", async () => {33  const ctx = await makeContext();34  const extId = await waitForSW(ctx);3536  const popup = await ctx.newPage();37  await popup.goto(`chrome-extension://${extId}/src/popup/index.html`);38  await popup.waitForTimeout(3000);3940  await popup.screenshot({ path: "/tmp/peach-popup-lock.png", fullPage: false });4142  const headline = popup.locator(".peach-unlock-headline, .peach-onboarding-headline").first();43  const headlineText = await headline.textContent().catch(() => "not found");44  console.log("Headline:", headlineText);4546  await ctx.close();47});