sop-material-preview.spec.ts
5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { expect, test, type Page, type Route } from '@playwright/test';
type MaterialItem = {
type: string;
url: string;
};
interface SopMaterialPreviewResponse {
id: number;
content: string;
bgColor: string;
intervalPx: number;
materials: MaterialItem[];
}
interface SopMaterialPreviewInitMessage {
type: 'SOP_MATERIAL_PREVIEW_INIT';
payload: {
id?: number;
content: string;
bgColor: string;
intervalPx: number;
materials: MaterialItem[];
};
}
const transparentPng = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
'base64',
);
const queryPreviewResponse: SopMaterialPreviewResponse = {
id: 321,
content: 'AITO 精品保养 SOP 海报',
bgColor: '#f5f1e8',
intervalPx: 12,
materials: [
{
type: 'image',
url: 'https://e2e.local/sop-material-query.png',
},
],
};
const iframePreviewMessage: SopMaterialPreviewInitMessage = {
type: 'SOP_MATERIAL_PREVIEW_INIT',
payload: {
id: 654,
content: 'iframe 直接预览 SOP 海报',
bgColor: '#eef6ff',
intervalPx: 8,
materials: [
{
type: 'image',
url: 'https://e2e.local/sop-material-iframe.png',
},
],
},
};
async function fulfillJson(route: Route, body: unknown) {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify(body),
});
}
async function fulfillPng(route: Route) {
await route.fulfill({
status: 200,
contentType: 'image/png',
body: transparentPng,
});
}
async function setupMaterialRoutes(page: Page, response = queryPreviewResponse) {
const apiRequests: string[] = [];
await page.route('https://e2e.local/**', fulfillPng);
await page.route('**/api/sop-material-preview?**', async (route) => {
apiRequests.push(route.request().url());
await fulfillJson(route, response);
});
return {
apiRequests,
};
}
test.describe('sop-material-preview E2E generated from the core journey rules', () => {
test('P0 Happy Path: Given actionId query, When the user opens the page, Then SOP material renders from the API model', async ({ page }) => {
const { apiRequests } = await setupMaterialRoutes(page);
await test.step('Given 可重复的 actionId query 和隔离的 API/material 拦截', async () => {
expect(queryPreviewResponse.materials).toHaveLength(1);
});
await test.step('When 用户通过 actionId 打开 SOP 素材预览页', async () => {
await page.goto('/sop-material-preview.html?actionId=321&taskId=88&customerInstanceId=9001');
});
await test.step('Then 前端请求素材接口并透传关键 query 参数', async () => {
await expect.poll(() => apiRequests).toHaveLength(1);
const apiUrl = new URL(apiRequests[0]);
expect(apiUrl.pathname).toBe('/api/sop-material-preview');
expect(apiUrl.searchParams.get('actionId')).toBe('321');
expect(apiUrl.searchParams.get('taskId')).toBe('88');
expect(apiUrl.searchParams.get('customerInstanceId')).toBe('9001');
});
await test.step('Then 页面渲染素材图片、标题和接口返回的背景色', async () => {
const image = page.locator('img[src="https://e2e.local/sop-material-query.png"]');
await expect(image).toBeVisible();
await expect(page).toHaveTitle('AITO 精品保养 SOP 海报');
await expect(page.locator('main')).toHaveCSS('background-color', 'rgb(245, 241, 232)');
await expect(page.locator('body')).toHaveCSS('background-color', 'rgb(245, 241, 232)');
});
});
test('P1 Critical Branch: Given iframe embedding, When parent sends init payload, Then direct preview renders without querying the API', async ({ page, baseURL }) => {
const { apiRequests } = await setupMaterialRoutes(page);
await test.step('Given SOP 素材页作为 iframe 被父页面嵌入', async () => {
const iframeSrc = new URL('/sop-material-preview.html', baseURL).toString();
await page.setContent(`
<!doctype html>
<html>
<body>
<iframe title="SOP material preview" src="${iframeSrc}"></iframe>
</body>
</html>
`);
});
const frame = page.frameLocator('iframe[title="SOP material preview"]');
await test.step('When 子页面 ready 后父页面发送 SOP_MATERIAL_PREVIEW_INIT', async () => {
await expect(frame.locator('main')).toBeVisible();
await frame.locator('body').evaluate((body, message) => {
body.ownerDocument.defaultView?.dispatchEvent(new MessageEvent('message', {
data: message,
origin: 'https://dj.glsx.com.cn',
}));
}, iframePreviewMessage);
});
await test.step('Then iframe 内直接渲染素材图片、标题和背景色', async () => {
const image = frame.locator('img[src="https://e2e.local/sop-material-iframe.png"]');
await expect(image).toBeVisible();
await expect(frame.locator('main')).toHaveCSS('background-color', 'rgb(238, 246, 255)');
await expect.poll(() => apiRequests).toHaveLength(0);
});
});
test('P1 Critical Error Path: Given no actionId and no init message, When the page opens, Then it stays blank and does not query the API', async ({ page }) => {
const { apiRequests } = await setupMaterialRoutes(page);
await test.step('Given URL 不包含 actionId 且没有 iframe 初始化消息', async () => {
expect(apiRequests).toHaveLength(0);
});
await test.step('When 用户直接打开 SOP 素材预览页', async () => {
await page.goto('/sop-material-preview.html');
});
await test.step('Then 页面保持空白承载态且不发起素材查询', async () => {
await expect(page.locator('main img')).toHaveCount(0);
await expect(page.locator('main video')).toHaveCount(0);
await expect(page.locator('main')).toHaveCSS('background-color', 'rgb(255, 255, 255)');
await expect.poll(() => apiRequests).toHaveLength(0);
});
});
});