productCategoryPage.ts
6.87 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
import { Page, Locator, expect } from '@playwright/test';
import { BasePage } from './basePage';
/**
* 商品分类页面选择器
*/
const selectors = {
// 导航
moreMenu: 'text=更多 >',
categoryMenu: 'text=商品分类',
// 功能按钮
addCategoryButton: 'text=新增分类',
editCategoryButton: 'text=修改分类',
confirmButton: 'text=确定',
clearSearchButton: '.nut-searchbar__search-icon',
// 表单
categoryNameInput: 'textbox',
categoryDescriptionInput: 'textbox',
categoryStatusSwitch: '.nut-switch > .nut-switch-button',
// 搜索
searchInput: 'textbox',
// 列表
categoryItem: (categoryName: string) => `span:has-text("${categoryName}")`,
};
/**
* 商品分类页面类
* 处理商品分类相关操作
*/
export class ProductCategoryPage extends BasePage {
// 导航定位器
readonly moreMenu: Locator;
readonly categoryMenu: Locator;
// 功能按钮
readonly addCategoryButton: Locator;
readonly editCategoryButton: Locator;
readonly deleteCategoryButton: Locator;
readonly confirmButton: Locator;
readonly clearSearchButton: Locator;
// 表单定位器
readonly categoryNameInput: Locator;
readonly categoryDescriptionInput: Locator;
readonly categoryStatusSwitch: Locator;
// 搜索
readonly searchInput: Locator;
constructor(page: Page) {
super(page);
this.moreMenu = page.getByText('更多 >');
this.categoryMenu = page.getByText('商品分类').first();
this.addCategoryButton = page.getByText('新增分类');
this.editCategoryButton = page.getByText('修改分类');
this.deleteCategoryButton = page.getByText('删除分类');
this.confirmButton = page.getByText('确定', { exact: true });
this.clearSearchButton = page.locator('uni-view:nth-child(7) > .default-container > uni-view > .z-paging-content > .nut-searchbar > .nut-searchbar__search-input > .nut-searchbar__input-inner-icon > .nut-searchbar__search-icon > .nut-icon');
this.categoryNameInput = page.getByRole('textbox').nth(1);
this.categoryDescriptionInput = page.getByRole('textbox').nth(2);
this.categoryStatusSwitch = page.locator('.uni-scroll-view-content > uni-view:nth-child(3) > .nut-cell__value > .nut-form-item__body__slots > .nut-switch > .nut-switch-button');
this.searchInput = page.getByRole('textbox').first();
}
/**
* 打开商品分类管理页面
* 完整流程:首页 -> 更多 -> 商品分类
*/
async openProductCategoryManagement(): Promise<void> {
await this.navigate('/');
await this.moreMenu.click();
await this.page.waitForTimeout(500);
await this.categoryMenu.click();
await this.page.waitForLoadState('networkidle', { timeout: 30000 });
}
/**
* 点击新增分类按钮
*/
async clickAddCategory(): Promise<void> {
await this.addCategoryButton.click();
}
/**
* 输入分类名称
* @param categoryName 分类名称
*/
async enterCategoryName(categoryName: string): Promise<void> {
await this.categoryNameInput.click();
await this.categoryNameInput.fill(categoryName);
}
/**
* 输入分类描述
* @param description 分类描述
*/
async enterCategoryDescription(description: string): Promise<void> {
await this.categoryDescriptionInput.click();
await this.categoryDescriptionInput.fill(description);
}
/**
* 点击确定按钮
*/
async clickConfirm(): Promise<void> {
await this.confirmButton.click();
}
/**
* 搜索分类
* @param categoryName 分类名称
*/
async searchCategory(categoryName: string): Promise<void> {
await this.searchInput.click();
await this.searchInput.fill(categoryName);
}
/**
* 验证分类是否存在
* @param categoryName 分类名称
*/
async expectCategoryVisible(categoryName: string): Promise<void> {
await expect(this.page.locator('span').filter({ hasText: categoryName })).toBeVisible();
}
/**
* 创建新商品分类(完整流程)
* @param categoryName 分类名称
* @param description 分类描述
*/
async createCategory(categoryName: string, description: string): Promise<void> {
await this.openProductCategoryManagement();
await this.clickAddCategory();
await this.enterCategoryName(categoryName);
await this.enterCategoryDescription(description);
await this.clickConfirm();
await this.searchCategory(categoryName);
await this.expectCategoryVisible(categoryName);
}
/**
* 点击修改分类按钮
*/
async clickEditCategory(): Promise<void> {
await this.editCategoryButton.click();
}
/**
* 清除搜索框内容
*/
async clearSearch(): Promise<void> {
await this.searchInput.click();
await this.searchInput.selectText();
await this.searchInput.fill('');
await this.page.waitForTimeout(300);
}
/**
* 切换分类状态
*/
async toggleCategoryStatus(): Promise<void> {
await this.categoryStatusSwitch.click();
}
/**
* 修改商品分类(完整流程)
* @param oldCategoryName 原分类名称
* @param newCategoryName 新分类名称
* @param newDescription 新描述
* @param toggleStatus 是否切换状态
*/
async updateCategory(oldCategoryName: string, newCategoryName: string, newDescription: string, toggleStatus: boolean = false): Promise<void> {
// 搜索原分类
await this.searchCategory(oldCategoryName);
await this.expectCategoryVisible(oldCategoryName);
// 点击修改分类
await this.clickEditCategory();
// 编辑分类信息
await this.enterCategoryName(newCategoryName);
await this.enterCategoryDescription(newDescription);
// 切换状态(可选)
if (toggleStatus) {
await this.toggleCategoryStatus();
}
// 点击保存
await this.clickConfirm();
// 清除搜索框并搜索新分类名
await this.clearSearch();
await this.searchCategory(newCategoryName);
await this.expectCategoryVisible(newCategoryName);
}
/**
* 点击删除分类按钮
*/
async clickDeleteCategory(): Promise<void> {
await this.deleteCategoryButton.click();
}
/**
* 验证分类已删除(不存在)
* @param categoryName 分类名称
*/
async expectCategoryNotVisible(categoryName: string): Promise<void> {
await expect(this.page.locator('span').filter({ hasText: categoryName })).not.toBeVisible();
}
/**
* 删除商品分类(完整流程)
* @param categoryName 分类名称
*/
async deleteCategory(categoryName: string): Promise<void> {
// 搜索要删除的分类
await this.searchCategory(categoryName);
await this.expectCategoryVisible(categoryName);
// 点击删除分类
await this.clickDeleteCategory();
// 确认删除
await this.clickConfirm();
// 等待删除完成
await this.page.waitForTimeout(1000);
// 验证分类已删除
await this.expectCategoryNotVisible(categoryName);
}
}