Files
workspace/code/g3soft-erp/web/e2e/support/fixtures.ts
T
2026-10-14 17:25:47 +08:00

38 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test as base, expect } from '@playwright/test'
import { SESSION_STORAGE_KEY, loadSession, type PersistedSession } from './session'
export { expect }
/**
* 免登录的 test:每个 page 在应用脚本执行**之前**就把 sessionStorage 种好,
* 于是路由守卫(`isAuthenticated` 只看 token 非空)直接放行,用例一进站就是登录态。
*
* 为什么不是 `storageState`:token 存在 sessionStorage,而 Playwright 的 storageState
* 只覆盖 cookie + localStorage,种不进去。
*
* 这里覆盖的是内置 `context` fixture —— 同名依赖拿到的是基类的原始实现(不是自己,
* 否则就无限递归了)。放 `context` 而不是 `page`:addInitScript 挂在 context 上,
* 用例里新开的 tab / iframe 也一并生效。
*
* 手写等价物:
* sessionStorage.setItem('g3erp:login', JSON.stringify({ loginInfo: { token, user } }))
*
* ⚠️ 别在用例里真去提交登录表单:那会刷新服务端会话,把本轮其他并发用例一起踢下线。
* 要测登录流程本身,得先用一个**专用账号**,并关掉 VITE_DEV_AUTO_LOGIN。
*/
export const test = base.extend({
context: async ({ context }, use) => {
const { token, user } = loadSession()
const seed: PersistedSession = { loginInfo: { token, user } }
await context.addInitScript(
(payload: { key: string; value: PersistedSession }) => {
window.sessionStorage.setItem(payload.key, JSON.stringify(payload.value))
},
{ key: SESSION_STORAGE_KEY, value: seed },
)
await use(context)
},
})