Files
story/test/e2e/visual-smoke.mjs

90 lines
5.2 KiB
JavaScript

import { createRequire } from "node:module";
import { mkdir } from "node:fs/promises";
import { fileURLToPath } from "node:url";
const require = createRequire(process.env.PLAYWRIGHT_REQUIRE_FROM ?? import.meta.url);
const { chromium } = require("playwright");
const outputDirectory = new URL("./screenshots/", import.meta.url);
const webURL = process.env.VISUAL_WEB_URL ?? "http://localhost:3100";
const webHostPattern = new RegExp(new URL(webURL).host.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
const oidcURLPattern = new RegExp(process.env.VISUAL_OIDC_URL_PATTERN ?? "localhost:58180");
const username = process.env.VISUAL_USERNAME ?? "owner";
const password = process.env.VISUAL_PASSWORD ?? "LocalOnly123!";
await mkdir(outputDirectory, { recursive: true });
const browser = await chromium.launch({
headless: true,
executablePath: process.env.CHROME_PATH ?? "C:/Program Files/Google/Chrome/Application/chrome.exe",
});
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 }, deviceScaleFactor: 1 });
const consoleErrors = [];
const failedRequests = [];
page.on("console", (message) => { if (message.type() === "error") consoleErrors.push(message.text()); });
page.on("requestfailed", (request) => {
const failure = request.failure()?.errorText ?? "failed";
const requestURL = new URL(request.url());
if (request.method() === "GET" && requestURL.pathname === "/api/v1/events" && failure.includes("ERR_ABORTED")) return;
failedRequests.push(`${request.method()} ${request.url()} ${failure}`);
});
try {
await page.goto(webURL, { waitUntil: "networkidle" });
await page.getByRole("button", { name: "登录研发环境" }).click();
await page.waitForURL(oidcURLPattern);
await page.locator("#username").fill(username);
await page.locator("#password").fill(password);
await page.locator("#kc-login").click();
await page.waitForURL(webHostPattern, { timeout: 15000 });
await page.getByRole("heading", { name: "家庭业务工作台" }).waitFor();
await page.screenshot({ path: fileURLToPath(new URL("workspace-desktop.png", outputDirectory)), fullPage: true });
await page.getByRole("button", { name: "隐私请求", exact: true }).click();
await page.getByRole("button", { name: "永久删除主体", exact: true }).click();
const deleteForm = page.getByRole("form", { name: "永久删除主体确认" });
await deleteForm.waitFor();
const selectedSubjectName = (await page.locator(".privacy-toolbar select option:checked").textContent())?.trim() ?? "";
await deleteForm.getByLabel("删除原因").fill("核对页面内不可逆操作的双重确认流程");
await deleteForm.getByLabel(/^输入长者称呼/).fill("错误称呼");
const mismatchVisible = await deleteForm.getByRole("alert").isVisible();
const deleteSubmit = deleteForm.getByRole("button", { name: "确认永久删除" });
const mismatchSubmitDisabled = await deleteSubmit.isDisabled();
await deleteForm.getByLabel(/^输入长者称呼/).fill(selectedSubjectName);
const exactMatchSubmitEnabled = await deleteSubmit.isEnabled();
await page.screenshot({ path: fileURLToPath(new URL("privacy-delete-confirmation.png", outputDirectory)), fullPage: true });
await deleteForm.getByRole("button", { name: "取消" }).click();
const deleteFormClosed = await deleteForm.count() === 0;
await page.getByRole("button", { name: "固件发布" }).click();
await page.getByRole("heading", { name: "固件版本" }).waitFor();
await page.screenshot({ path: fileURLToPath(new URL("firmware-desktop.png", outputDirectory)), fullPage: true });
const desktopOverflow = await page.evaluate(() => [...document.querySelectorAll("body *")]
.filter((element) => element instanceof HTMLElement && element.scrollWidth > element.clientWidth + 2 && getComputedStyle(element).overflowX === "visible")
.slice(0, 20)
.map((element) => `${element.tagName}.${element.className}: ${element.clientWidth}/${element.scrollWidth}`));
await page.setViewportSize({ width: 1024, height: 900 });
await page.screenshot({ path: fileURLToPath(new URL("firmware-tablet.png", outputDirectory)), fullPage: true });
const tabletOverflow = await page.evaluate(() => [...document.querySelectorAll("body *")]
.filter((element) => element instanceof HTMLElement && element.scrollWidth > element.clientWidth + 2 && getComputedStyle(element).overflowX === "visible")
.slice(0, 20)
.map((element) => `${element.tagName}.${element.className}: ${element.clientWidth}/${element.scrollWidth}`));
const result = {
title: await page.title(),
url: page.url(),
desktopOverflow,
tabletOverflow,
consoleErrors,
failedRequests,
firmwareTabVisible: await page.getByText("上传并校验固件制品", { exact: true }).isVisible(),
privacyDeleteConfirmation: { mismatchVisible, mismatchSubmitDisabled, exactMatchSubmitEnabled, deleteFormClosed },
};
console.log(JSON.stringify(result, null, 2));
if (desktopOverflow.length || tabletOverflow.length || consoleErrors.length || failedRequests.length || !result.firmwareTabVisible || !mismatchVisible || !mismatchSubmitDisabled || !exactMatchSubmitEnabled || !deleteFormClosed) {
throw new Error("Visual smoke detected an overflow, browser error, failed request, or missing firmware workspace");
}
} finally {
await browser.close();
}