customerGroup.spec.ts
8.41 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
import { test, expect } from '@playwright/test';
import * as allure from 'allure-js-commons';
/**
* 客户分组测试
*/
test.describe('客户分组', () => {
// 使用已保存的认证状态
test.use({ storageState: 'auth.json' });
// 强制测试串行执行,避免并行测试之间的干扰
test.describe.configure({ mode: 'serial' });
/**
* 生成随机分组名称(5个字以内)
*/
function generateGroupName(): string {
const prefixes = ['测试', '客户', '优质', '普通', '会员', 'VIP', '大', '小', '新', '老'];
const suffixes = ['组', '户', '客', '户', '户'];
const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
const suffix = suffixes[Math.floor(Math.random() * suffixes.length)];
const timestamp = Date.now().toString().slice(-2);
const name = `${prefix}${suffix}${timestamp}`;
return name.slice(0, 5); // 确保不超过5个字
}
/**
* 生成随机排序号
*/
function generateSortNumber(): string {
return String(Math.floor(Math.random() * 999) + 1);
}
test('新增客户分组', async ({ page }, testInfo) => {
// 添加allure元素
await allure.epic('客户管理');
await allure.feature('客户分组');
await allure.story('新增客户分组');
// 生成随机分组名称
const groupName = generateGroupName();
console.log('新增分组名称:', groupName);
// 步骤1:进入客户分组页面
await allure.step('进入客户分组页面', async () => {
await page.goto('/');
await page.waitForLoadState('networkidle', { timeout: 30000 });
await page.getByText('更多 >').click();
await page.waitForTimeout(500);
await page.getByText('客户分组').first().click();
await page.waitForLoadState('networkidle', { timeout: 30000 });
});
// 步骤2:点击新建按钮
await allure.step('点击新建按钮', async () => {
await page.getByText('新建').click();
await page.waitForTimeout(500);
});
// 步骤3:填写分组信息
await allure.step('填写分组信息', async () => {
// 填写分组名称
await page.getByRole('textbox').nth(1).click();
await page.getByRole('textbox').nth(1).fill(groupName);
// 填写备注/排序
await page.getByRole('textbox').nth(2).click();
await page.getByRole('textbox').nth(2).fill('自动化测试分组');
});
// 步骤4:保存分组
await allure.step('保存分组', async () => {
await page.getByText('确定').click();
await page.waitForTimeout(1000);
});
// 步骤5:验证分组创建成功 - 使用正则匹配检查页面是否出现新增的内容
await allure.step('验证分组创建成功', async () => {
await page.waitForTimeout(1000); // 等待页面刷新
// 使用正则匹配分组名称
const isGroupVisible = await page.locator('uni-view').filter({ hasText: new RegExp(`^${groupName}$`) }).first().isVisible({ timeout: 5000 }).catch(() => false);
expect(isGroupVisible).toBeTruthy();
});
});
test('修改客户分组', async ({ page }, testInfo) => {
// 添加allure元素
await allure.epic('客户管理');
await allure.feature('客户分组');
await allure.story('修改客户分组');
// 先生成一个分组用于修改
const originalGroupName = generateGroupName();
const newGroupName = generateGroupName();
console.log('原分组名称:', originalGroupName);
console.log('新分组名称:', newGroupName);
// 步骤1:进入客户分组页面并创建分组
await allure.step('进入客户分组页面并创建分组', async () => {
await page.goto('/');
await page.waitForLoadState('networkidle', { timeout: 30000 });
await page.getByText('更多 >').click();
await page.waitForTimeout(500);
await page.getByText('客户分组').first().click();
await page.waitForLoadState('networkidle', { timeout: 30000 });
// 创建分组
await page.getByText('新建').click();
await page.waitForTimeout(500);
// 点击分组名称输入框
await page.locator('uni-view').filter({ hasText: /^分组名称\*$/ }).first().click();
await page.getByRole('textbox').nth(1).fill(originalGroupName);
await page.getByText('确定').click();
await page.waitForTimeout(1000);
});
// 步骤2:搜索并修改分组
await allure.step('搜索并修改分组', async () => {
// 在搜索框中输入原分组名称
await page.getByRole('textbox').click();
await page.getByRole('textbox').fill(originalGroupName);
// 点击搜索结果中的分组
await page.locator('uni-view').filter({ hasText: new RegExp(`^${originalGroupName}$`) }).first().click();
await page.waitForTimeout(500);
// 点击编辑按钮
await page.getByText('编辑').click();
await page.waitForTimeout(500);
// 清空分组名称并填写新名称
await page.getByRole('textbox').nth(1).click();
await page.locator('.nut-input__clear-icon').first().click();
await page.getByRole('textbox').nth(1).click();
await page.getByRole('textbox').nth(1).fill(newGroupName);
// 直接填写新排序号(新增时未填写,无需清除)
const newSortNumber = generateSortNumber();
await page.getByRole('textbox').nth(2).click();
await page.getByRole('textbox').nth(2).fill(newSortNumber);
await page.getByText('确定').click();
await page.waitForTimeout(1000);
});
// 步骤3:验证分组修改成功 - 使用搜索验证修改后的内容
await allure.step('验证分组修改成功', async () => {
// 在搜索框中输入新分组名称进行验证
await page.waitForTimeout(500);
await page.getByRole('textbox').click();
await page.getByRole('textbox').fill(newGroupName);
await page.waitForTimeout(500);
const isNewGroupVisible = await page.locator('uni-view').filter({ hasText: new RegExp(`^${newGroupName}$`) }).first().isVisible({ timeout: 5000 }).catch(() => false);
expect(isNewGroupVisible).toBeTruthy();
});
});
test('删除客户分组', async ({ page }, testInfo) => {
// 添加allure元素
await allure.epic('客户管理');
await allure.feature('客户分组');
await allure.story('删除客户分组');
// 先生成一个分组用于删除
const groupName = generateGroupName();
console.log('待删除分组名称:', groupName);
// 步骤1:进入客户分组页面并创建分组
await allure.step('进入客户分组页面并创建分组', async () => {
await page.goto('/');
await page.waitForLoadState('networkidle', { timeout: 30000 });
await page.getByText('更多 >').click();
await page.waitForTimeout(500);
await page.getByText('客户分组').first().click();
await page.waitForLoadState('networkidle', { timeout: 30000 });
// 创建分组
await page.getByText('新建').click();
await page.waitForTimeout(500);
await page.getByRole('textbox').nth(1).click();
await page.getByRole('textbox').nth(1).fill(groupName);
await page.getByText('确定').click();
await page.waitForTimeout(1000);
});
// 步骤2:搜索并删除分组
await allure.step('搜索并删除分组', async () => {
// 在搜索框中输入分组名称
await page.getByRole('textbox').click();
await page.getByRole('textbox').fill(groupName);
// 搜索后自动选中,直接点击删除按钮
await page.getByText('删除').click();
await page.waitForTimeout(500);
// 确认删除
await page.getByText('确定', { exact: true }).click();
// await page.waitForTimeout(1000);
// 点击清除按钮
await page.locator('.nut-searchbar__search-icon').click();
// 清除后等待页面刷新
await page.waitForTimeout(1000);
});
// 步骤3:验证分组删除成功
await allure.step('验证分组删除成功', async () => {
// 搜索已删除的分组名称
await page.getByRole('textbox').fill(groupName);
await page.waitForTimeout(1000);
// 验证搜索结果中是否还能找到该分组(找不到才是删除成功)
const count = await page.locator('uni-view').filter({ hasText: new RegExp(`^${groupName}$`) }).count();
expect(count).toBe(0);
});
});
});