Files
dacli/tests/e2e/auth.setup.ts
T
ssw aaa13e567d v17: 端到端验证 _helpers/ 标准库 (workSystem/lbbq, 4/4 PASS)
新增:
- tests/e2e/v17-stdlib-verify.spec.ts (端到端验证 spec, 3 tests)
- tests/e2e/auth.setup.ts 改 workSystem (tds DB 已重置, 2026-06-15)
- _helpers/auth.ts 默认 appCode 改 workSystem

修改:
- _helpers/auth.ts APP_LOGIN_PATH 默认 tds → workSystem

验证结果 (workSystem/lbbq, webType=1 但有 4 字段可填):
✓ test 1 (9.5s)  首页 + clickMenu 导航 (1.3s)
✓ test 2 (16.0s) 列表 → +新增 → 填 4 字段 → 保存 → 行数 0→1 (modal 822ms)
✓ test 3 (13.8s) waitRowCountChange 智能等: 565ms (vs 盲等 2000ms)
✓ setup (0.2s)   storageState 复用 (跳 UI 登录)

证明 _helpers/ 库 7 核心 API 真实可用:
  clickMenu / clickAdd / waitFormModalReady
  fillByLabel / selectByLabel / fillTextareaByLabel / fillNumberByLabel
  saveModal / waitModalClose
  countRows / getTotalText / waitRowCountChange

typecheck 干净
2026-06-15 15:27:15 +08:00

50 lines
2.3 KiB
TypeScript

/**
* tests/e2e/auth.setup.ts — Playwright projects.setup 模式
*
* 跑一次 UI 登录 (admin/123456 @ tds), 把 cookie + localStorage 持久化到 .auth/admin.json,
* 后续 spec 通过 use.storageState 复用登录态, 不必每个 test 重输密码.
*
* 跑法:
* npx playwright test --project=setup # 单独刷新登录态
* npx playwright test # 自动先跑 setup 再跑 chromium
*
* TTL 优化: 6 小时内复用, 超时则本次重登 (远短于 dacli 7 天 token).
*
* 关键路径 (来自 ssw-e2e-test 验证):
* 登录页: /jnpf_app_tds/login
* 登录后: /jnpf_app_tds/home
* 会话共享: 同一 host (nginx) 下所有 jnpf_app_X 路径共用 storage (浏览器层 host-wide)
*/
import { test as setup, expect } from '@playwright/test'
import { isAuthFresh, loginAsAdmin, AUTH_FILE, APP_HOME_PATH } from './_helpers/auth'
import { mkdirSync } from 'node:fs'
import { dirname } from 'node:path'
setup('authenticate as admin/123456 @ workSystem (v17 verify)', async ({ page }) => {
// 新鲜则跳过 — Playwright 仍会写 storageState 给后续 project 用, 我们手动保留旧文件
if (isAuthFresh()) {
setup.info().annotations.push({ type: 'note', description: 'auth fresh, skip UI login' })
// 必须依然 storageState 写一次到 fixture, 否则 Playwright 会用空 state
// 用 page.context() 加载现有 file: 我们让 context 直接从 file 初始化 — 但 setup project 没法在 fixture 之前换 context
// 折中: 不在 setup project 用 storageState fixture, 改在 chromium project use.storageState 引用同 file
// 故此处直接 return — chromium project 会从 AUTH_FILE 读
return
}
mkdirSync(dirname(AUTH_FILE), { recursive: true })
// v17: 改 workSystem (tds DB 已重置, 2026-06-15 用户清空其他应用)
const ok = await loginAsAdmin(page, { appCode: 'workSystem' })
expect(ok, 'UI 登录应跳到 /home').toBe(true)
// 健壮性: 验证 storage 有 access token
const hasAccess = await page.evaluate(() => {
return Object.keys(localStorage).some(
(k) => k.includes('core-access') && (localStorage.getItem(k)?.length ?? 0) > 50
)
})
expect(hasAccess, '登录后 localStorage 应有加密 access token').toBe(true)
await page.context().storageState({ path: AUTH_FILE })
})