[FEAT] 導入 Playwright E2E 測試環境與登入功能測試腳本
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m36s

This commit is contained in:
2026-03-06 15:38:27 +08:00
parent 02e5f5d4ea
commit e11193c2a7
8 changed files with 548 additions and 60 deletions

14
e2e/helpers/auth.ts Normal file
View File

@@ -0,0 +1,14 @@
import { Page } from '@playwright/test';
/**
* 共用登入函式
* 使用測試帳號登入 Star ERP 系統
*/
export async function login(page: Page, username = 'mama', password = 'mama9453') {
await page.goto('/');
await page.fill('#username', username);
await page.fill('#password', password);
await page.getByRole('button', { name: '登入系統' }).click();
// 等待儀表板載入完成
await page.waitForSelector('text=系統概況', { timeout: 10000 });
}

72
e2e/login.spec.ts Normal file
View File

@@ -0,0 +1,72 @@
import { test, expect } from '@playwright/test';
import { login } from './helpers/auth';
/**
* Star ERP 登入流程端到端測試
*/
test.describe('登入功能', () => {
test('頁面應正確顯示登入表單', async ({ page }) => {
// 前往登入頁面
await page.goto('/');
// 驗證:帳號輸入框存在
await expect(page.locator('#username')).toBeVisible();
// 驗證:密碼輸入框存在
await expect(page.locator('#password')).toBeVisible();
// 驗證:登入按鈕存在
await expect(page.getByRole('button', { name: '登入系統' })).toBeVisible();
});
test('輸入錯誤的帳密應顯示錯誤訊息', async ({ page }) => {
await page.goto('/');
// 輸入錯誤的帳號密碼
await page.fill('#username', 'wronguser');
await page.fill('#password', 'wrongpassword');
// 點擊登入
await page.getByRole('button', { name: '登入系統' }).click();
// 等待頁面回應
await page.waitForTimeout(2000);
// 驗證:應該還是停在登入頁面(未成功跳轉)
await expect(page).toHaveURL(/\//);
// 驗證:頁面上應顯示某種錯誤提示(紅色文字或 toast
// 先用寬鬆的檢查方式,後續可以根據實際錯誤訊息調整
const hasError = await page.locator('.text-red-500, .text-red-600, [role="alert"], .toast, [data-sonner-toast]').count();
expect(hasError).toBeGreaterThan(0);
});
test('輸入正確帳密後應成功登入並跳轉', async ({ page }) => {
// 使用共用登入函式
await login(page);
// 驗證:頁面右上角應顯示使用者名稱
await expect(page.getByText('mama')).toBeVisible();
// 驗證:儀表板的關鍵指標卡片存在
await expect(page.getByText('庫存總值')).toBeVisible();
// 截圖留存(成功登入畫面)
await page.screenshot({ path: 'e2e/screenshots/login-success.png', fullPage: true });
});
test('空白帳密不應能登入', async ({ page }) => {
await page.goto('/');
// 不填任何東西,直接點登入
await page.getByRole('button', { name: '登入系統' }).click();
// 等待頁面回應
await page.waitForTimeout(1000);
// 驗證:應該還在登入頁面
await expect(page.locator('#username')).toBeVisible();
});
});