test-extension.mjs

4.3 KB · raw

1import { chromium } from "playwright";2import fs from "fs/promises";3import os from "os";4import path from "path";5import { fileURLToPath } from "url";67const __filename = fileURLToPath(import.meta.url);8const __dirname = path.dirname(__filename);9const EXTENSION_PATH = path.join(__dirname, "dist");1011async function main() {12  const userDataDir = path.join(13    os.tmpdir(),14    `peach-playwright-profile-${Date.now()}`15  );16  await fs.mkdir(userDataDir, { recursive: true });1718  const context = await chromium.launchPersistentContext(userDataDir, {19    headless: false,20    args: [21      `--disable-extensions-except=${EXTENSION_PATH}`,22      `--load-extension=${EXTENSION_PATH}`,23      "--no-sandbox",24      "--disable-setuid-sandbox",25    ],26  });2728  const page = await context.newPage();2930  // Capture console messages31  page.on("console", (msg) => {32    console.log(`[console.${msg.type()}] ${msg.text()}`);33  });3435  page.on("pageerror", (err) => {36    console.log(`[pageerror] ${err.message}`);37  });3839  // Click the extension icon to open popup40  let [serviceWorker] = context.serviceWorkers();41  if (!serviceWorker) {42    serviceWorker = await context.waitForEvent("serviceworker");43  }4445  const extId = new URL(serviceWorker.url()).host;46  console.log(`Extension ID: ${extId}`);4748  const popup = await context.newPage();49  await popup.goto(`chrome-extension://${extId}/src/popup/index.html`);5051  popup.on("console", (msg) => {52    console.log(`[popup console.${msg.type()}] ${msg.text()}`);53  });5455  popup.on("pageerror", (err) => {56    console.log(`[popup pageerror] ${err.message}`);57  });5859  // Wait for the page to load60  await popup.waitForTimeout(2000);6162  const title = await popup.title();63  console.log(`Popup title: ${title}`);6465  const createButton = popup.getByRole("button", { name: "create vault" });66  const unlockButton = popup.getByRole("button", { name: "unlock" });6768  if (await createButton.isVisible()) {69    console.log("\nCreating fresh vault...");70    await popup.locator("#master-password").fill("testpassword123");71    await popup.locator("#confirm-password").fill("testpassword123");72    await createButton.click();73    await popup.waitForTimeout(1500);74  }7576  const newEntryButton = popup.getByRole("button", {77    name: "New entry",78    exact: true,79  });80  await newEntryButton.click();81  console.log("New entry form opened");8283  await popup.locator(".peach-new-name-input").fill("GitHub");84  await popup.locator('input[placeholder="domain.com"]').fill("https://github.com");85  await popup.locator('input[placeholder="username or email"]').fill("june");86  await popup.locator('input[placeholder="password"]').fill("supersecret123!");87  await popup.getByRole("button", { name: "create" }).click();88  console.log("Entry created");8990  await popup.waitForTimeout(1000);91  console.log(92    `Entry visible after save: ${await popup.locator("text=GitHub").first().isVisible()}`93  );9495  await popup.close();96  console.log("Popup closed to simulate clicking away");9798  const reopenedPopup = await context.newPage();99  await reopenedPopup.goto(`chrome-extension://${extId}/src/popup/index.html`);100  await reopenedPopup.waitForTimeout(1500);101102  const createVisibleAfterReopen = await reopenedPopup103    .getByRole("button", { name: "create vault" })104    .isVisible()105    .catch(() => false);106  const unlockVisibleAfterReopen = await reopenedPopup107    .getByRole("button", { name: "unlock" })108    .isVisible()109    .catch(() => false);110  const entryVisibleAfterReopen = await reopenedPopup111    .locator("text=GitHub")112    .first()113    .isVisible()114    .catch(() => false);115116  console.log(`Create vault visible after reopen: ${createVisibleAfterReopen}`);117  console.log(`Unlock visible after reopen: ${unlockVisibleAfterReopen}`);118  console.log(`Entry visible after reopen: ${entryVisibleAfterReopen}`);119120  if (!entryVisibleAfterReopen && unlockVisibleAfterReopen) {121    const reopenedUnlockButton = reopenedPopup.getByRole("button", {122      name: "unlock",123    });124    await reopenedPopup.locator("#master-password").fill("testpassword123");125    await reopenedUnlockButton.click();126    await reopenedPopup.waitForTimeout(1500);127    console.log(128      `Entry still present after unlock: ${await reopenedPopup129        .locator("text=GitHub")130        .first()131        .isVisible()}`132    );133  }134135  await context.close();136  console.log("\nDone.");137}138139main().catch(console.error);