#!/bin/bash # 为指定 Bug 生成 Playwright 测试用例 # 用法: ./generate-bug-test.sh [bug_steps] BUG_ID="$1" BUG_TITLE="$2" BUG_STEPS="$3" if [ -z "$BUG_ID" ] || [ -z "$BUG_TITLE" ]; then echo "用法: $0 [bug_steps]" exit 1 fi SPEC_DIR="$(dirname "$0")/../specs" SPEC_FILE="${SPEC_DIR}/bug-${BUG_ID}.spec.ts" # 如果测试已存在,跳过 if [ -f "$SPEC_FILE" ]; then echo "SKIP: ${SPEC_FILE} 已存在" exit 0 fi mkdir -p "$SPEC_DIR" # 从标题推断模块 infer_route() { local t="$1" if echo "$t" | grep -qi "门诊医生\|门诊诊前\|门诊挂号"; then echo "/doctorstation"; return; fi if echo "$t" | grep -qi "住院医生\|临床医嘱\|医嘱录入"; then echo "/inpatientDoctor"; return; fi if echo "$t" | grep -qi "住院护士\|补费\|发退药\|医嘱执行"; then echo "/inpatientNurse"; return; fi if echo "$t" | grep -qi "分诊\|排队\|候诊"; then echo "/triageandqueuemanage"; return; fi if echo "$t" | grep -qi "挂号\|预约\|签到"; then echo "/registration"; return; fi if echo "$t" | grep -qi "手术\|计费"; then echo "/operatingroom"; return; fi if echo "$t" | grep -qi "诊断\|中医"; then echo "/inpatientDoctor"; return; fi if echo "$t" | grep -qi "病历\|EMR"; then echo "/doctorstation"; return; fi if echo "$t" | grep -qi "目录\|诊疗"; then echo "/catalog"; return; fi if echo "$t" | grep -qi "药房\|发药\|库存"; then echo "/pharmacy"; return; fi echo "/" } ROUTE=$(infer_route "$BUG_TITLE") STEPS_COMMENT="" if [ -n "$BUG_STEPS" ]; then STEPS_COMMENT="// 复现步骤: // $(echo "$BUG_STEPS" | head -5)" fi cat > "$SPEC_FILE" << SPECEOF import { test, expect } from '@playwright/test'; import { LoginPage } from '../pages/LoginPage'; /** * Bug #${BUG_ID}: ${BUG_TITLE} * 自动生成: $(date '+%Y-%m-%d %H:%M:%S') */ test.describe('🐛 Bug#${BUG_ID}', () => { let loginPage: LoginPage; test.beforeEach(async ({ page }) => { loginPage = new LoginPage(page); await loginPage.goto(); await loginPage.login( process.env.TEST_USERNAME || 'admin', process.env.TEST_PASSWORD || 'admin123' ); await loginPage.expectLoginSuccess(); }); test('#${BUG_ID} ${BUG_TITLE} @bug${BUG_ID} @regression', async ({ page }) => { await page.goto('${ROUTE}'); await page.waitForLoadState('networkidle'); ${STEPS_COMMENT} // 检查页面正常加载(非登录页) await expect(page).not.toHaveURL(/.*login.*/); // 检查无 JS 错误 const jsErrors: string[] = []; page.on('pageerror', (err) => jsErrors.push(err.message)); await page.waitForTimeout(2000); // 页面基本可交互 const body = page.locator('body'); await expect(body).toBeVisible(); // 截图记录 await page.screenshot({ path: 'tests/e2e/report/bug-${BUG_ID}-result.png', fullPage: true }); // 无 JS 错误 expect(jsErrors).toEqual([]); }); }); SPECEOF echo "OK: ${SPEC_FILE}"