CI / build (push) Successful in 10m1s
Co-Authored-By: Claude Opus 5.5 <[email protected]>
182 lines
8.9 KiB
TypeScript
182 lines
8.9 KiB
TypeScript
import { expect, test, type Page } from "@playwright/test";
|
|
import { createAllRolesUser } from "./db";
|
|
import { DEMO, loginAs } from "./helpers";
|
|
|
|
const noHorizontalScroll = (page: Page) =>
|
|
page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth);
|
|
|
|
test.describe("全站框架", () => {
|
|
test("登入後頂部列顯示今日日期(含星期)、通知、姓名與部門", async ({ page }) => {
|
|
await loginAs(page, DEMO.employee);
|
|
await expect(page.getByTestId("today")).toHaveText(
|
|
/^\d{4} 年 \d{1,2} 月 \d{1,2} 日 星期[日一二三四五六]$/,
|
|
);
|
|
await expect(page.getByRole("button", { name: "通知" })).toBeVisible();
|
|
await expect(page.getByTestId("user-name")).toHaveText("王小明");
|
|
await expect(page.getByTestId("user-department")).toHaveText("設計部(示範)");
|
|
await expect(page.getByRole("heading", { level: 1 })).toHaveText("您好,王小明");
|
|
|
|
await page.getByRole("button", { name: "通知" }).click();
|
|
await expect(page.getByText("目前沒有通知")).toBeVisible();
|
|
});
|
|
|
|
test("側欄顯示品牌與底部標語;長頁面捲到底時側欄仍填滿視窗", async ({ page }) => {
|
|
await page.setViewportSize({ width: 1440, height: 900 });
|
|
await loginAs(page, DEMO.employee, "/help");
|
|
const sidebar = page.getByRole("complementary", { name: "側欄" });
|
|
await expect(sidebar.getByTestId("sidebar-brand")).toContainText("AI 補助申請系統");
|
|
await expect(sidebar.getByTestId("sidebar-brand")).toContainText(
|
|
"AI Subsidy Application System",
|
|
);
|
|
await expect(sidebar.getByTestId("sidebar-footer")).toContainText("© 2026 AI 補助申請系統");
|
|
await expect(sidebar.getByTestId("app-version")).toHaveText(/^v\d+\.\d+\.\d+$/);
|
|
|
|
await page.evaluate(() => window.scrollTo(0, document.documentElement.scrollHeight));
|
|
expect(await page.evaluate(() => window.scrollY)).toBeGreaterThan(0);
|
|
const box = await sidebar.boundingBox();
|
|
expect(box?.y).toBe(0);
|
|
expect(box?.height).toBe(900);
|
|
await expect(sidebar.getByTestId("sidebar-footer")).toBeInViewport();
|
|
});
|
|
|
|
test("選單最多、視窗較矮時,底部裝飾不遮擋導覽項目", async ({ page }) => {
|
|
await page.setViewportSize({ width: 1280, height: 720 });
|
|
await loginAs(page, createAllRolesUser(), "/dashboard");
|
|
const sidebar = page.getByRole("complementary", { name: "側欄" });
|
|
const links = sidebar.getByRole("navigation", { name: "主要導覽" }).getByRole("link");
|
|
const last = links.last();
|
|
await expect(last).toHaveText("常見問題");
|
|
await last.scrollIntoViewIfNeeded();
|
|
await expect(last).toBeInViewport({ ratio: 1 });
|
|
const footer = (await sidebar.getByTestId("sidebar-footer").boundingBox())!;
|
|
const lastBox = (await last.boundingBox())!;
|
|
expect(lastBox.y + lastBox.height).toBeLessThanOrEqual(footer.y);
|
|
// 裝飾不超出底部標語區
|
|
const wave = (await sidebar.getByTestId("sidebar-footer").locator("svg").boundingBox())!;
|
|
expect(wave.y).toBeGreaterThanOrEqual(footer.y - 0.5);
|
|
});
|
|
|
|
test("未登入開啟 /applications 時導向登入頁", async ({ page }) => {
|
|
await page.goto("/applications");
|
|
await expect(page).toHaveURL(/\/login\?returnTo=%2Fapplications$/);
|
|
});
|
|
|
|
test("導覽高亮目前頁面;員工看不到部門審核", async ({ page }) => {
|
|
await loginAs(page, DEMO.employee, "/applications");
|
|
const nav = page.getByRole("navigation", { name: "主要導覽" });
|
|
await expect(nav.getByRole("link", { name: "我的申請" })).toHaveAttribute(
|
|
"aria-current",
|
|
"page",
|
|
);
|
|
await expect(nav.locator('[aria-current="page"]')).toHaveCount(1);
|
|
await expect(nav.getByRole("link", { name: "部門審核" })).toHaveCount(0);
|
|
});
|
|
|
|
test("兼任主管看到部門審核與部門月結", async ({ page }) => {
|
|
await loginAs(page, DEMO.manager);
|
|
const nav = page.getByRole("navigation", { name: "主要導覽" });
|
|
await expect(nav.getByRole("link")).toHaveText([
|
|
"首頁",
|
|
"新增申請",
|
|
"我的申請",
|
|
"部門審核",
|
|
"部門月結",
|
|
"使用說明",
|
|
"常見問題",
|
|
]);
|
|
});
|
|
|
|
test("首頁、使用說明、常見問題可開啟;常見問題說明實際補助於月結後確認", async ({ page }) => {
|
|
await loginAs(page, DEMO.employee);
|
|
await expect(page.getByRole("heading", { level: 1 })).toHaveText("您好,王小明");
|
|
await page
|
|
.getByRole("navigation", { name: "主要導覽" })
|
|
.getByRole("link", { name: "使用說明" })
|
|
.click();
|
|
await expect(page.getByRole("heading", { level: 1 })).toHaveText("使用說明");
|
|
await expect(page.getByText(/實際補助金額於月結後確認/)).toBeVisible();
|
|
await page
|
|
.getByRole("navigation", { name: "主要導覽" })
|
|
.getByRole("link", { name: "常見問題" })
|
|
.click();
|
|
await expect(page.getByRole("heading", { level: 1 })).toHaveText("常見問題");
|
|
await expect(page.getByText(/實際補助金額於月結後確認/)).toBeVisible();
|
|
});
|
|
});
|
|
|
|
test.describe("錯誤與回饋", () => {
|
|
test("送出缺 3 項必填的表單:頁首顯示錯誤摘要並捲動到第一個錯誤欄位", async ({ page }) => {
|
|
await loginAs(page, DEMO.employee, "/design-tokens");
|
|
await page.getByRole("button", { name: "送出" }).click();
|
|
await expect(page.getByText("尚有 3 項資料需完成")).toBeVisible();
|
|
await expect(page.locator("#sample-plan")).toBeFocused();
|
|
await expect(page.locator("#sample-plan")).toBeInViewport();
|
|
for (const message of ["請填寫訂閱方案", "請填寫支付金額(台幣)", "請填寫工作用途說明"]) {
|
|
// 錯誤摘要一次、欄位旁 inline 一次
|
|
await expect(page.getByText(message)).toHaveCount(2);
|
|
}
|
|
});
|
|
|
|
test("補齊後送出顯示提示訊息;確認視窗按取消不執行", async ({ page }) => {
|
|
await loginAs(page, DEMO.employee, "/design-tokens");
|
|
await page.getByRole("button", { name: "不核准(範例)" }).click();
|
|
await expect(page.getByRole("alertdialog")).toContainText("不核准後申請人無法再對此案件補件");
|
|
await page.getByRole("button", { name: "取消" }).click();
|
|
await expect(page.getByText("已不核准(範例)")).toHaveCount(0);
|
|
|
|
await page.getByLabel("訂閱方案/項目名稱").fill("ChatGPT Plus");
|
|
await page.getByLabel("支付金額(台幣)").fill("899");
|
|
await expect(page.getByText("NT$899")).toBeVisible();
|
|
await page.getByLabel("工作用途說明").fill("提案發想與資料整理");
|
|
await page.getByRole("button", { name: "送出" }).click();
|
|
await expect(page.getByText("已送出(範例)")).toBeVisible();
|
|
});
|
|
});
|
|
|
|
test.describe("響應式版面", () => {
|
|
for (const width of [375, 768, 1280]) {
|
|
test(`${width}px:無水平捲動,主要按鈕未被遮擋`, async ({ page }) => {
|
|
await page.setViewportSize({ width, height: 800 });
|
|
await loginAs(page, DEMO.employee, "/design-tokens");
|
|
expect(await noHorizontalScroll(page)).toBe(true);
|
|
const submit = page.getByRole("button", { name: "送出" });
|
|
// 手機固定在底部(一開始就看得到);平板與桌機在長表單底部,捲動到即可
|
|
if (width >= 768) await submit.scrollIntoViewIfNeeded();
|
|
await expect(submit).toBeInViewport({ ratio: 1 });
|
|
await submit.click({ trial: true }); // 確認按鈕可點擊、未被其他元素遮住
|
|
});
|
|
}
|
|
|
|
test("375px:側欄收合為漢堡選單;長表單捲動後主要按鈕固定在底部", async ({ page }) => {
|
|
await page.setViewportSize({ width: 375, height: 700 });
|
|
await loginAs(page, DEMO.employee, "/design-tokens");
|
|
await expect(page.getByRole("complementary", { name: "側欄" })).toBeHidden();
|
|
await page.getByRole("button", { name: "開啟選單" }).click();
|
|
const drawer = page.getByRole("dialog");
|
|
await expect(drawer.getByRole("link", { name: "我的申請" })).toBeVisible();
|
|
await page.getByRole("button", { name: "關閉選單" }).click();
|
|
|
|
await page.mouse.wheel(0, 2000);
|
|
const actions = page.getByTestId("primary-actions");
|
|
const box = await actions.boundingBox();
|
|
expect(box).not.toBeNull();
|
|
expect(Math.round(box!.y + box!.height)).toBe(700);
|
|
});
|
|
|
|
test("768px:保留側欄並可收合", async ({ page }) => {
|
|
await page.setViewportSize({ width: 768, height: 900 });
|
|
await loginAs(page, DEMO.employee);
|
|
const sidebar = page.getByRole("complementary", { name: "側欄" });
|
|
await expect(sidebar).toBeVisible();
|
|
await page.getByRole("button", { name: "收合側欄" }).click();
|
|
await expect(sidebar).toHaveAttribute("data-collapsed", "true");
|
|
expect(await noHorizontalScroll(page)).toBe(true);
|
|
});
|
|
|
|
test("375px:資料表格改為卡片清單", async ({ page }) => {
|
|
await page.setViewportSize({ width: 375, height: 800 });
|
|
await loginAs(page, DEMO.sysadmin, "/admin/audit-log");
|
|
expect(await noHorizontalScroll(page)).toBe(true);
|
|
});
|
|
});
|