marketingPushStrategy.e2e.js
12 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import path from "path";
import { expect, test } from "@playwright/test";
import { createLoggedInPage, getBaseUrl } from "./helpers/auth.js";
const BASE_URL = getBaseUrl();
const IMAGE_FIXTURE = path.resolve(process.cwd(), "public", "header-man.jpg");
const LOGIN_ACCOUNT = "glsadmin";
const LOGIN_PASSWORD = "123456";
const RECORD_DETAIL_STRATEGY_NAME = "测试发送策略";
function getUniqueStrategyName(prefix = "E2E营销推送策略") {
return `${prefix}-${Date.now()}`;
}
async function waitForNetworkStable(page) {
await page.waitForLoadState("networkidle");
await page.waitForTimeout(500);
}
async function waitForMetaResponse(page) {
const response = await page.waitForResponse((resp) => {
const request = resp.request();
const postData = request.postData() || "";
return request.method() === "POST"
&& postData.includes("method=dj.api.smartcarlife.sop.strategy.meta");
}, { timeout: 60000 });
expect(response.ok()).toBeTruthy();
return response;
}
async function waitForModifyPageReady(page) {
await expect(page.locator(".wrapper")).toBeVisible({ timeout: 30000 });
await expect(page.locator(".box").first()).toContainText("基础信息", { timeout: 30000 });
await expect(page.locator(".footer")).toBeVisible({ timeout: 30000 });
await page.waitForFunction(() => {
const wrapper = document.querySelector(".wrapper");
if (!wrapper) {
return false;
}
return wrapper.querySelectorAll("input, textarea, .el-select, .el-radio").length > 0;
}, null, { timeout: 60000 });
}
async function gotoStrategyList(page) {
await page.goto(`${BASE_URL}/#/marketing-push-strategy`);
await waitForNetworkStable(page);
await expect(page.locator(".show-search")).toBeVisible();
await expect(page.locator(".d2-table")).toBeVisible();
}
async function selectElementOption(page, trigger, optionText) {
await trigger.click();
const dropdown = page.locator(".el-select-dropdown:visible").last();
await expect(dropdown).toBeVisible();
if (optionText) {
const targetOption = dropdown.locator(".el-select-dropdown__item").filter({ hasText: optionText }).first();
await expect(targetOption).toBeVisible({ timeout: 10000 });
await targetOption.click();
} else {
await dropdown.locator(".el-select-dropdown__item:not(.is-disabled)").first().click();
}
await page.waitForTimeout(300);
}
async function waitForSuccessMessage(page, text = "操作成功") {
await expect(page.locator(".el-message .el-message__content").filter({ hasText: text }).last()).toBeVisible({ timeout: 20000 });
}
async function filterByStrategyName(page, strategyName) {
const nameInput = page.locator(".show-search input").first();
await nameInput.fill(strategyName);
await page.getByRole("button", { name: "查询" }).click();
await waitForNetworkStable(page);
}
async function resetSearchForm(page) {
await page.getByRole("button", { name: "重置" }).click();
await waitForNetworkStable(page);
}
async function getStrategyRow(page, strategyName) {
return page.locator(".el-table__body tr").filter({ hasText: strategyName }).first();
}
async function waitForStrategyRow(page, strategyName, options = {}) {
const {
attempts = 10,
interval = 2000,
} = options;
for (let index = 0; index < attempts; index += 1) {
await filterByStrategyName(page, strategyName);
const row = await getStrategyRow(page, strategyName);
if (await row.count()) {
return row;
}
await page.waitForTimeout(interval);
}
throw new Error(`未查询到策略: ${strategyName}`);
}
async function clickRowAction(row, actionText) {
const actionCell = row.locator("td").last();
const buttonAction = actionCell.locator("button").filter({ hasText: actionText }).first();
if (await buttonAction.count()) {
await buttonAction.click({ force: true });
return;
}
await actionCell.evaluate((cell, text) => {
const target = Array.from(cell.querySelectorAll("*")).find((node) => {
return node.textContent && node.textContent.trim() === text;
});
if (!target) {
throw new Error(`未找到操作: ${text}`);
}
target.click();
}, actionText);
}
async function fillMinimalWebpageStrategyForm(page, strategyName) {
const baseBox = page.locator(".box").nth(0);
const strategyNameInput = page.getByPlaceholder("请输入策略名称");
await expect(strategyNameInput).toBeVisible({ timeout: 20000 });
await strategyNameInput.fill(strategyName);
await baseBox.getByText("不限门店", { exact: true }).click();
await selectElementOption(page, baseBox.locator(".el-select").first(), "客户新车交付后");
await expect(baseBox).toContainText("策略将面向已完成首次触客的一销潜客");
const step2Box = page.locator(".box").nth(1);
await selectElementOption(page, step2Box.locator(".el-select").nth(0), "到达特定日期时间");
await selectElementOption(page, step2Box.locator(".el-select").nth(1), null);
const step3Box = page.locator(".box").nth(2);
const branch = step3Box.locator(".branch-item-wrapper").first();
await selectElementOption(page, branch.locator(".actions-item-wrapper .el-select").first(), "向客户群发送消息");
await branch.getByText("网页", { exact: true }).click();
await expect(branch.getByText("网页未设置", { exact: true })).toBeVisible();
await branch.getByRole("button", { name: "编辑", exact: true }).click();
const dialog = page.locator(".el-dialog__wrapper:visible").filter({ hasText: "编辑网页内容" }).last();
await expect(dialog).toBeVisible();
await dialog.getByPlaceholder("请输入网页标题").fill(`${strategyName}-网页`);
await dialog.getByPlaceholder("请输入网页描述").fill("E2E网页描述");
const uploadInput = dialog.locator("input[type='file']").last();
await uploadInput.setInputFiles(IMAGE_FIXTURE);
const materialItem = dialog.locator(".material-item-wrapper").first();
await expect(materialItem).toBeVisible({ timeout: 120000 });
await expect(dialog.locator(".page-config-wrapper")).not.toContainText("上传中", { timeout: 120000 });
await dialog.getByRole("button", { name: "确定" }).click();
await expect(dialog).toBeHidden({ timeout: 15000 });
await expect(branch.getByText("网页已设置", { exact: true })).toBeVisible();
}
async function submitStrategy(page) {
await page.getByRole("button", { name: "提交" }).click();
await waitForSuccessMessage(page);
await page.waitForURL(/#\/marketing-push-strategy$/);
await waitForNetworkStable(page);
}
async function openFirstRecordDetail(page) {
const row = await waitForStrategyRow(page, RECORD_DETAIL_STRATEGY_NAME, {
attempts: 8,
interval: 1500,
});
await clickRowAction(row, "执行记录");
const recordDialogWrapper = page.locator(".el-dialog__wrapper").filter({ hasText: "查看执行记录" }).last();
await expect(recordDialogWrapper).toBeVisible({ timeout: 15000 });
await waitForNetworkStable(page);
await expect(recordDialogWrapper).toContainText("查看策略详情", { timeout: 15000 });
const visibleDialogCount = await page.locator(".el-dialog__wrapper:visible").count();
const detailButton = recordDialogWrapper.locator("button:visible").filter({ hasText: "查看策略详情" }).first();
await expect(detailButton).toBeVisible({ timeout: 15000 });
await detailButton.click({ force: true });
await expect(page.locator(".el-dialog__wrapper:visible")).toHaveCount(visibleDialogCount + 1, { timeout: 15000 });
const detailDialog = page.locator(".el-dialog__wrapper:visible").filter({
has: page.locator(".strategy-detail-dialog"),
}).last();
await expect(detailDialog).toBeVisible({ timeout: 15000 });
await expect(detailDialog).toContainText("查看策略详情", { timeout: 15000 });
await expect(detailDialog.locator(".strategy-detail-dialog")).toBeVisible({ timeout: 15000 });
return { recordDialog: recordDialogWrapper, detailDialog };
}
test.describe.serial("营销推送策略 E2E", () => {
let sharedPage;
let createdStrategyName = "";
let editedStrategyName = "";
test.beforeAll(async ({ browser }) => {
sharedPage = await createLoggedInPage(browser, {
account: LOGIN_ACCOUNT,
password: LOGIN_PASSWORD,
timeout: 20000,
navigationTimeout: 60000,
});
await sharedPage.waitForTimeout(500);
});
test.afterAll(async () => {
await sharedPage.context().close();
});
test("应支持列表查询与重置", async () => {
await gotoStrategyList(sharedPage);
const searchInput = sharedPage.locator(".show-search input").first();
await searchInput.fill("营销");
await sharedPage.getByRole("button", { name: "查询" }).click();
await waitForNetworkStable(sharedPage);
await sharedPage.getByRole("button", { name: "重置" }).click();
await waitForNetworkStable(sharedPage);
await expect(searchInput).toHaveValue("");
});
test("应支持新增网页消息策略,并完成修改、启停和删除", async () => {
test.slow();
createdStrategyName = getUniqueStrategyName();
editedStrategyName = `${createdStrategyName}-已编辑`;
await gotoStrategyList(sharedPage);
const metaResponsePromise = waitForMetaResponse(sharedPage);
await sharedPage.getByText("新增策略", { exact: true }).click();
await sharedPage.waitForURL(/#\/marketing-push-strategy\/modify\?editType=add/);
await metaResponsePromise;
await waitForModifyPageReady(sharedPage);
await fillMinimalWebpageStrategyForm(sharedPage, createdStrategyName);
await submitStrategy(sharedPage);
await filterByStrategyName(sharedPage, createdStrategyName);
const createdRow = await getStrategyRow(sharedPage, createdStrategyName);
await expect(createdRow).toBeVisible();
const editMetaResponsePromise = waitForMetaResponse(sharedPage);
await clickRowAction(createdRow, "修改");
await sharedPage.waitForURL(/#\/marketing-push-strategy\/modify\?editType=edit/);
await editMetaResponsePromise;
await waitForModifyPageReady(sharedPage);
const baseBox = sharedPage.locator(".box").nth(0);
await baseBox.locator("input").first().fill(editedStrategyName);
await submitStrategy(sharedPage);
const editedRow = await waitForStrategyRow(sharedPage, createdStrategyName, {
attempts: 20,
interval: 2500,
});
await expect(editedRow).toBeVisible();
await editedRow.locator(".el-switch").click();
await waitForSuccessMessage(sharedPage);
await waitForNetworkStable(sharedPage);
await filterByStrategyName(sharedPage, createdStrategyName);
const rowAfterStatusChange = await getStrategyRow(sharedPage, createdStrategyName);
await expect(rowAfterStatusChange).toBeVisible();
const strategyNameForDelete = ((await rowAfterStatusChange.locator("td").nth(1).textContent()) || "").trim();
await clickRowAction(rowAfterStatusChange, "删除");
const confirmDialog = sharedPage.locator(".el-message-box:visible").last();
await expect(confirmDialog).toBeVisible({ timeout: 10000 });
await expect(confirmDialog).toContainText(`确定要删除策略"${strategyNameForDelete}"吗?`);
await confirmDialog.getByRole("button", { name: "确定" }).click();
await waitForSuccessMessage(sharedPage);
await waitForNetworkStable(sharedPage);
await filterByStrategyName(sharedPage, editedStrategyName);
await expect(await getStrategyRow(sharedPage, editedStrategyName)).toHaveCount(0);
});
test("应支持查看执行记录与策略详情", async () => {
test.slow();
test.setTimeout(120000);
await gotoStrategyList(sharedPage);
await resetSearchForm(sharedPage);
await filterByStrategyName(sharedPage, RECORD_DETAIL_STRATEGY_NAME);
const { recordDialog, detailDialog } = await openFirstRecordDetail(sharedPage);
await expect(recordDialog).toContainText("执行时间");
await expect(recordDialog).toContainText("执行结果");
await expect(recordDialog).toContainText("描述");
await expect(detailDialog).toContainText("基础信息");
await expect(detailDialog).toContainText("目标客户");
await expect(detailDialog).toContainText("触发条件");
await expect(detailDialog).toContainText("执行动作");
await detailDialog.getByRole("button", { name: "关闭" }).click();
await expect(detailDialog).toBeHidden({ timeout: 10000 });
await recordDialog.getByRole("button", { name: "关闭" }).click();
await expect(recordDialog).toBeHidden({ timeout: 10000 });
});
});