Files
eip4-web/e2e/script-replay.spec.ts

100 lines
4.2 KiB
TypeScript
Raw Permalink 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 { expect, test, type Page } from '@playwright/test';
// ============================================================================
// P4-5 端到端:改写后脚本挂回 amis 页面 schema 的 onEvent 重放
// ----------------------------------------------------------------------------
// 链路:页面渲染(mock schema 带 onEvent)→ 触发 inited/change → ajax 调
// /api/script/execute-template(vite proxy → 后端 8086,取改写后脚本经 facade 执行)
// → 返回 amis 指令数组 → 前端 replayScriptCommands 自定义动作逐条 doAction。
// 口径:schema 取自前端 mock(public/schemas/*.json,onEvent 形态 = 转换器 attachScriptOnEvent
// 产出,已单测固定);脚本与指令来自后端真实改写产物 + 真实保真层引擎(非桩)。
// amis 渲染需登录态:种入任意非空令牌过 RequireAuth(schema 走 mock 源,免后端鉴权;
// 仅 /api/script/execute-template 走后端,POC 默认链不拦截)。
// ============================================================================
// 种入登录态。注意:必须用真实 JWT——/api/script/execute-template 虽不在拦截清单,
// 但请求一旦携带非法 Bearer 令牌,JwtAuthFilter 会直接 400(F1-4b 实测),
// 假 token 会让重放链路静默失败。口径与 schema-samples.spec.ts 一致。
import http from 'node:http';
let _tokenCache: string | undefined;
async function getToken(): Promise<string> {
if (_tokenCache) return _tokenCache;
return new Promise((resolve, reject) => {
const body = JSON.stringify({ account: 'admin', password: '111111' });
const req = http.request(
{
hostname: 'localhost',
port: 8093,
path: '/api/auth/login',
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) },
},
(res) => {
let data = '';
res.on('data', (c) => (data += c));
res.on('end', () => {
try {
const parsed = JSON.parse(data) as { success: boolean; token: string };
if (!parsed.success) reject(new Error('登录失败'));
_tokenCache = parsed.token;
resolve(parsed.token);
} catch (e) {
reject(e);
}
});
},
);
req.on('error', reject);
req.write(body);
req.end();
});
}
async function seedAuth(page: Page) {
const token = await getToken();
await page.addInitScript(
({ t }: { t: string }) => {
localStorage.setItem('eip4_token', t);
localStorage.setItem(
'eip4_profile',
JSON.stringify({ userId: 'ADMIN', account: 'admin', name: '系统管理员', roles: [] }),
);
},
{ t: token },
);
}
test('inited 重放:福利保险结算单脚本隐藏 BEGINDATE 字段', async ({ page }) => {
await seedAuth(page);
await page.goto('/page/welfare-settlement');
// amis 渲染出表单(起始年字段先在)
await expect(page.getByText('起始年')).toBeVisible({ timeout: 20_000 });
// inited 触发 → ajax 取脚本 setFieldVisible("BEGINDATE", false) → 重放 hidden
// 断言 BEGINDATE 字段(起始日期 label)被隐藏
await expect(page.getByText('起始日期')).toBeHidden({ timeout: 15_000 });
await page.screenshot({ path: 'e2e/__screenshots__/welfare-hidden.png', fullPage: true });
});
test('change 重放 + 服务端 getFieldValue 闭环:办公用品领用申请回填 OLD_STORE_ID', async ({ page }) => {
await seedAuth(page);
await page.goto('/page/office-collar');
await expect(page.getByText('仓库', { exact: true })).toBeVisible({ timeout: 20_000 });
// 改 STORE_ID → change 触发 → ajax 传 item(含 STORE_ID)→ 脚本
// bindField("OLD_STORE_ID", getFieldValue("STORE_ID")) → 重放 setValue OLD_STORE_ID
const storeInput = page.locator('input[name="STORE_ID"]');
await storeInput.fill('WH-7788');
await storeInput.blur();
// 断言 OLD_STORE_ID 被脚本回填为 STORE_ID 的值(服务端 getFieldValue 从 item 求值闭环)
await expect(page.locator('input[name="OLD_STORE_ID"]')).toHaveValue('WH-7788', { timeout: 15_000 });
await page.screenshot({ path: 'e2e/__screenshots__/office-collar-setvalue.png', fullPage: true });
});