/** * F1-4 抽样渲染实测 —— 27 个样本模板跨 9 类型真实浏览器渲染 * * 断言目标:渲染成功(amis-host 挂载 + 无 schema-error-card),不断言有无业务数据。 * 登录方式:Node 侧预登录拿真实 JWT,通过 addInitScript 直接注入 localStorage。 */ import { expect, test, type Page, type TestInfo } from '@playwright/test'; import * as http from 'node:http'; const BACKEND_NODE = 'http://localhost:8093'; // ---- Node 侧登录(一次,所有 test 共用) ---- let _tokenCache: string | null = null; async function getToken(): Promise { 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; userId: string; account: string; name: string; roles: 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(); }); } // 种入真实 JWT(Node 侧已拿到 token,直接注入为常量) 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 }, ); } // 27 个样本(9 种 type,各 ≥2 个) const SAMPLES: Array<{ type: string; id: string }> = [ // type1 { type: 'type1', id: '00551ef5-1c76-4015-bec9-2e1c8b631deb' }, { type: 'type1', id: '009c4f52-636a-4688-8148-75cabe83fd83' }, { type: 'type1', id: '00e7a83b-dd1f-4d7a-a574-bc362bc785d2' }, // type3 { type: 'type3', id: '0211a0e2-2073-4b24-b59f-e6757e5dcff0' }, { type: 'type3', id: '0246d4bd-232b-465d-9265-8c1fc1ecbdf2' }, { type: 'type3', id: '082f27b4-7ae9-4b21-acc9-2b2a6f7d9797' }, // type4 { type: 'type4', id: '19e418d1-ea62-47c1-9fb4-c33f94c306c4' }, { type: 'type4', id: '1b7af860-f11d-4b64-931c-13264207d57c' }, { type: 'type4', id: '21d82b1c-1db0-499a-b2c7-48befd42a9d7' }, // type5 { type: 'type5', id: '062041e3-0862-49b1-a304-34c873ac5b9e' }, { type: 'type5', id: 'f4446608-68e3-40f3-8546-2511de73fbdc' }, // type6 { type: 'type6', id: '05226af5-5c77-470f-b039-d894fc55714d' }, { type: 'type6', id: '08158341-1b41-48be-9226-90a206960e8f' }, { type: 'type6', id: '1d9e666d-8923-4d9a-8644-05ba3fa533aa' }, // type7 { type: 'type7', id: '00511ac1-ee64-4ecd-a550-22842ee5be72' }, { type: 'type7', id: '00a64619-0014-412f-8ca9-95edcf988c2f' }, { type: 'type7', id: '0267be11-591a-4533-b7cc-079cef81086e' }, // type100 { type: 'type100', id: '00ff3f9d-b744-4e55-a731-f98d16d6f7fe' }, { type: 'type100', id: '06a1d7d2-a202-4b8f-aef3-2e25e3db0ed7' }, { type: 'type100', id: '06fe97fa-6ebe-41e7-9f88-c6a635c1887e' }, // type101 { type: 'type101', id: '138b7ca7-befb-4a0c-bfc6-ed8fb4d4e434' }, { type: 'type101', id: '42861f01-14ea-482d-8031-f30612dfccf4' }, { type: 'type101', id: '43d2e233-08cf-4019-850e-5c2c073d1f76' }, // type200 { type: 'type200', id: '00654052-f899-45d6-bbd4-04457c13025a' }, { type: 'type200', id: '03ec5566-7d99-4eba-85ce-31e25f5e48ab' }, { type: 'type200', id: '063fdf2a-3032-4220-9974-32f0a5932aa6' }, ]; for (const sample of SAMPLES) { test(`[${sample.type}] ${sample.id}`, async ({ page }, testInfo: TestInfo) => { await seedAuth(page); await page.goto(`/page/${sample.id}`); // 等待渲染完成:amis-host 出现(amis SDK 加载需要时间) await expect(page.locator('[data-testid="amis-host"]')).toBeVisible({ timeout: 45_000 }); // 确认无错误边界卡片(schema 加载失败/amis 崩溃时会出现) await expect(page.locator('[data-testid="schema-error-card"]')).not.toBeVisible(); // F2-8 竞态修复:截图前等 amis 内容区可见(表格行/Page body),避免截到 loading 状态 await page.waitForSelector( '[data-testid="amis-host"] .cxd-Page-body, [data-testid="amis-host"] table, [data-testid="amis-host"] .cxd-CRUD', { timeout: 10_000 }, ).catch(() => {/* 无表格内容(空页/树/form),已确认 amis-host 可见,继续截图 */}); // 截图存 e2e/artifacts/ const screenshotPath = `e2e/artifacts/${sample.type}-${sample.id}.png`; await page.screenshot({ path: screenshotPath, fullPage: false }); await testInfo.attach(`${sample.type}-${sample.id}`, { path: screenshotPath, contentType: 'image/png', }); }); }