purchasePage.ts
20.5 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import { Page, Locator, expect } from '@playwright/test';
import { BasePage } from './basePage';
/**
* 采购入库页面选择器
*/
const selectors = {
// 导航
moreMenu: 'text=更多 >',
purchaseInMenu: 'text=采购入库',
// 商品列表
goodsList: '.goods-list',
goodsItem: '.goods-item',
goodsName: '.goods-name span',
// 商品选择
productItem: (productName: string) => `uni-view:hasText("/^${productName}$/")`,
unitBox: 'uni-view:hasText("/^箱$/")',
quantityInput: "getByText('1', { exact: true })",
priceInput: "getByText('1', { exact: true })",
doneButton: 'text=完成',
// 供应商列表
supplierField: 'uni-view:nth-child(5) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask',
supplierList: '.zp-paging-container-content',
supplierItem: '.item-card',
supplierName: '.mainTitle span',
supplierOption: (supplierName: string) => `uni-view:hasText("/^${supplierName}$/")`,
// 仓库列表
warehouseField: 'uni-view:nth-child(6) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask',
warehouseList: '.zp-paging-container-content',
warehouseItem: '.list-item',
warehouseName: '.name',
warehouseOption: 'text=东区普通仓库',
// 草稿操作
saveDraftButton: 'text=存入草稿',
draftListButton: 'text=草稿单列表',
// 草稿箱搜索
draftSearchInput: '.draft-list-wrapper > .z-paging-content > .nut-searchbar > .nut-searchbar__search-input > .nut-searchbar__input-inner > .nut-searchbar__input-form > span > .nut-searchbar__input-bar > .uni-input-wrapper > .uni-input-input',
draftItem: 'span',
};
/**
* 采购入库页面类
* 处理采购入库草稿相关操作
*/
export class PurchasePage extends BasePage {
// 导航定位器
readonly moreMenu: Locator;
readonly purchaseInMenu: Locator;
// 商品列表
readonly goodsList: Locator;
readonly goodsItem: Locator;
readonly goodsName: Locator;
// 商品选择
readonly quantityInput: Locator;
readonly priceInput: Locator;
readonly doneButton: Locator;
// 供应商
readonly supplierField: Locator;
readonly supplierList: Locator;
readonly supplierItem: Locator;
readonly supplierName: Locator;
// 仓库
readonly warehouseField: Locator;
readonly warehouseList: Locator;
readonly warehouseItem: Locator;
readonly warehouseName: Locator;
// 草稿操作
readonly saveDraftButton: Locator;
readonly draftListButton: Locator;
// 草稿箱搜索
readonly draftSearchInput: Locator;
// 费用相关
readonly addExpenseButton: Locator;
readonly expenseItem: Locator;
readonly amountInput: Locator;
readonly costItem: Locator;
readonly settlementAccountField: Locator;
readonly settlementAccountOption: Locator;
// 采购员
readonly purchaserField: Locator;
readonly purchaserItem: Locator;
// 车牌号
readonly licensePlateInput: Locator;
// 备注
readonly remarkInput: Locator;
// 图片上传
readonly uploadImageButton: Locator;
// 付款
readonly payButton: Locator;
readonly settlementAccount: Locator;
readonly confirmDialogButton: Locator;
// 采购列表
readonly purchaseListMenu: Locator;
readonly purchaseSearchInput: Locator;
constructor(page: Page) {
super(page);
this.moreMenu = page.getByText('更多 >');
this.purchaseInMenu = page.getByText('采购入库').first();
this.goodsList = page.locator('.goods-list');
this.goodsItem = page.locator('.goods-item');
this.goodsName = page.locator('.goods-name span');
this.quantityInput = page.locator('[data-prop="num1"] .input');
this.priceInput = page.locator('[data-prop="unitPrice"] .input');
this.doneButton = page.getByText('完成');
this.supplierField = page.locator('uni-view:nth-child(5) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
this.supplierList = page.locator('.zp-paging-container-content').first();
this.supplierItem = page.locator('.item-card');
this.supplierName = page.locator('.mainTitle span');
this.warehouseField = page.locator('uni-view:nth-child(6) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
this.warehouseList = page.locator('.zp-paging-container-content').first();
this.warehouseItem = page.locator('.list-item');
this.warehouseName = page.locator('.name');
this.saveDraftButton = page.getByText('存入草稿', { exact: true });
this.draftListButton = page.getByText('草稿单列表', { exact: true });
this.draftSearchInput = page.locator('.draft-list-wrapper > .z-paging-content > .nut-searchbar > .nut-searchbar__search-input > .nut-searchbar__input-inner > .nut-searchbar__input-form > span > .nut-searchbar__input-bar > .uni-input-wrapper > .uni-input-input');
// 费用相关
this.addExpenseButton = page.getByText('入库费用添加费用');
this.expenseItem = page.locator('.account-item');
this.amountInput = page.locator('.cost-item > uni-view:nth-child(3) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
this.costItem = page.locator('.cost-item');
this.settlementAccountField = page.locator('.cost-item > uni-view:nth-child(6) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
this.settlementAccountOption = page.locator('uni-view').filter({ hasText: /^现金$/ }).first();
// 采购员
this.purchaserField = page.locator('uni-view:nth-child(10) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
this.purchaserItem = page.locator('.list > .item');
// 车牌号 - 使用nth-child(11)
this.licensePlateInput = page.locator('uni-view:nth-child(11) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__mask');
// 备注 - 使用nth-child(12)
this.remarkInput = page.locator('uni-view:nth-child(12) > .nut-cell__value > .nut-form-item__body__slots > .nut-input > .nut-input__value > .nut-input__input > .uni-input-wrapper > .uni-input-input');
// 图片上传
this.uploadImageButton = page.getByText('图片(最多上传9张)');
// 付款
this.payButton = page.getByText('付款', { exact: true });
this.settlementAccount = page.locator('uni-view').filter({ hasText: /^微信支付$/ }).first();
this.confirmDialogButton = page.getByText('确定').first();
// 采购列表
this.purchaseListMenu = page.getByText('采购列表');
this.purchaseSearchInput = page.getByRole('textbox');
}
/**
* 进入采购入库页面
*/
async openPurchaseInbound(): Promise<void> {
await this.navigate('/');
await this.moreMenu.click();
await this.page.waitForTimeout(500);
await this.purchaseInMenu.click();
await this.page.waitForLoadState('networkidle', { timeout: 30000 });
}
/**
* 从商品列表随机选择一个商品
* @returns 商品名称和索引
*/
async getRandomProduct(): Promise<{ name: string; index: number }> {
// 等待商品列表加载
await this.goodsList.waitFor({ state: 'visible', timeout: 10000 });
await this.page.waitForTimeout(500);
// 获取所有商品名称
const productNames = await this.goodsName.allTextContents();
if (productNames.length === 0) {
throw new Error('商品列表为空');
}
// 随机选择一个商品
const randomIndex = Math.floor(Math.random() * productNames.length);
const selectedProduct = productNames[randomIndex];
console.log('随机选择的商品:', selectedProduct, '索引:', randomIndex);
return { name: selectedProduct, index: randomIndex };
}
/**
* 选择商品(从商品列表随机选择)
* @returns 商品名称
*/
async selectRandomProduct(): Promise<string> {
const { name, index } = await this.getRandomProduct();
await this.goodsItem.nth(index).click();
await this.page.waitForTimeout(300);
return name;
}
/**
* 选择指定商品
* @param productName 商品名称
*/
async selectProduct(productName: string): Promise<void> {
await this.page.locator('uni-view').filter({ hasText: new RegExp(`^${productName}$`) }).first().click();
}
/**
* 点击数量输入框(通过data-prop属性定位)
*/
async clickQuantityInput(): Promise<void> {
await this.quantityInput.click();
}
/**
* 输入数量
* @param quantity 数量
*/
async enterQuantity(quantity: string): Promise<void> {
// 点击数量输入框(通过data-prop属性定位)
await this.page.locator('[data-prop="num1"]').click();
// 点击键盘上的数字
await this.page.locator(`.number-keyboard uni-view[data-key="${quantity}"]`).click();
}
/**
* 输入单价(点击单价输入框,然后点击键盘数字)
* @param price 单价
*/
async enterPrice(price: string): Promise<void> {
// 点击单价输入框(通过data-prop属性定位,不依赖单位文本)
await this.page.locator('[data-prop="unitPrice"]').click();
// 点击键盘上的数字
await this.page.locator(`.number-keyboard uni-view[data-key="${price}"]`).click();
}
/**
* 点击完成按钮
*/
async clickDone(): Promise<void> {
await this.doneButton.click();
await this.page.waitForTimeout(1000);
}
/**
* 从供应商列表随机选择供应商
* @returns 供应商名称
*/
async getRandomSupplier(): Promise<string> {
await this.supplierField.click();
await this.page.waitForTimeout(1000);
const supplierNames = await this.supplierName.allTextContents();
if (supplierNames.length === 0) {
throw new Error('供应商列表为空');
}
const randomIndex = Math.floor(Math.random() * supplierNames.length);
const selectedSupplier = supplierNames[randomIndex];
console.log('随机选择的供应商:', selectedSupplier, '索引:', randomIndex);
await this.supplierItem.nth(randomIndex).click();
await this.page.waitForTimeout(300);
return selectedSupplier;
}
/**
* 选择指定供应商
* @param supplierName 供应商名称
*/
async selectSupplier(supplierName: string): Promise<void> {
await this.supplierField.click();
await this.page.locator('uni-view').filter({ hasText: new RegExp(`^${supplierName}$`) }).nth(1).click();
}
/**
* 从仓库列表随机选择仓库
* @returns 仓库名称
*/
async getRandomWarehouse(): Promise<string> {
await this.warehouseField.click();
await this.page.waitForTimeout(1000);
const warehouseNames = await this.warehouseName.allTextContents();
if (warehouseNames.length === 0) {
throw new Error('仓库列表为空');
}
const randomIndex = Math.floor(Math.random() * warehouseNames.length);
const selectedWarehouse = warehouseNames[randomIndex];
console.log('随机选择的仓库:', selectedWarehouse, '索引:', randomIndex);
await this.warehouseItem.nth(randomIndex).click();
await this.page.waitForTimeout(300);
return selectedWarehouse;
}
/**
* 选择指定仓库
* @param warehouseName 仓库名称
*/
async selectWarehouse(warehouseName: string): Promise<void> {
await this.warehouseField.click();
await this.page.getByText(warehouseName).nth(2).click();
}
/**
* 点击存入草稿
*/
async clickSaveDraft(): Promise<void> {
await this.saveDraftButton.click();
}
/**
* 点击草稿单列表
*/
async clickDraftList(): Promise<void> {
await this.draftListButton.click();
await this.page.waitForLoadState('networkidle', { timeout: 30000 });
}
/**
* 在草稿箱搜索商品
* @param productName 商品名称
*/
async searchInDraftList(productName: string): Promise<void> {
await this.draftSearchInput.click();
await this.draftSearchInput.fill(productName);
}
/**
* 验证草稿列表中存在商品
* @param productName 商品名称
*/
async expectDraftContainsProduct(productName: string): Promise<void> {
// 等待搜索结果加载
await this.page.waitForTimeout(500);
// 获取列表中第一个商品进行断言
const firstItem = this.page.locator('span').filter({ hasText: productName }).first();
await expect(firstItem).toBeVisible();
}
/**
* 创建采购入库草稿(完整流程)
* @param productName 商品名称
* @param supplierName 供应商名称
* @param warehouseName 仓库名称
* @param quantity 数量
* @param price 单价
*/
async createPurchaseDraft(productName: string, supplierName: string, warehouseName: string, quantity: string = '1', price: string = '1'): Promise<void> {
// 选择商品
await this.selectProduct(productName);
// 输入数量
await this.enterQuantity(quantity);
// 输入单价
await this.enterPrice(price);
// 点击完成
await this.clickDone();
// 选择供应商
await this.selectSupplier(supplierName);
// 选择仓库
await this.selectWarehouse(warehouseName);
// 存入草稿
await this.clickSaveDraft();
// 打开草稿单列表
await this.clickDraftList();
// 搜索商品
await this.searchInDraftList(productName);
// 验证商品存在
await this.expectDraftContainsProduct(productName);
}
/**
* 添加入库费用
* @param expenseIndex 费用项索引(默认0)
* @param amount 金额(默认'1')
*/
async addExpense(expenseIndex: number = 0, amount: string = '1'): Promise<void> {
// 打开费用选择
await this.addExpenseButton.click();
// 等待费用列表加载
await this.page.waitForTimeout(500);
// 随机选择费用项
const expenseCount = await this.expenseItem.count();
const randomIndex = Math.floor(Math.random() * expenseCount);
await this.expenseItem.nth(randomIndex).click();
await this.page.getByText('确定').click();
// 输入金额 - 点击金额输入框,然后点击键盘数字
await this.amountInput.click();
await this.page.waitForTimeout(300);
await this.page.getByText('1', { exact: true }).click();
// 点击键盘上的完成按钮关闭键盘
await this.page.getByText('完成', { exact: true }).click();
// 点击结算账户并选择现金
await this.settlementAccountField.click();
await this.page.waitForTimeout(300);
await this.settlementAccountOption.click();
// 点击确定按钮
await this.page.getByText('确定').click();
}
/**
* 选择采购员
* @param purchaserName 采购员名称
*/
async selectPurchaser(purchaserName?: string): Promise<string> {
await this.purchaserField.click();
await this.page.waitForTimeout(500);
// 如果没有指定采购员,随机选择
if (!purchaserName) {
const purchasers = await this.purchaserItem.locator('.name').allTextContents();
if (purchasers.length === 0) {
throw new Error('采购员列表为空');
}
const randomIndex = Math.floor(Math.random() * purchasers.length);
purchaserName = purchasers[randomIndex];
await this.purchaserItem.nth(randomIndex).click();
} else {
await this.page.locator('uni-view').filter({ hasText: new RegExp(`^${purchaserName}`) }).first().click();
}
await this.page.waitForTimeout(300);
return purchaserName;
}
/**
* 输入车牌号
* @param licensePlate 车牌号
*/
async enterLicensePlate(licensePlate: string): Promise<void> {
await this.fillLicensePlate(licensePlate, this.licensePlateInput);
}
/**
* 输入备注
* @param remark 备注内容
*/
async enterRemark(remark: string): Promise<void> {
await this.remarkInput.click();
await this.remarkInput.fill(remark);
}
/**
* 上传图片
* @param imagePath 图片路径
*/
async uploadImage(imagePath: string): Promise<void> {
// 点击图片上传区域
await this.uploadImageButton.click();
await this.page.waitForTimeout(500);
// 点击上传按钮
await this.page.locator('span').filter({ hasText: '供应商*请选择供应商车牌号请选择车牌号入库日期*请选择入库日期入库仓库*请选择仓库批次别名*请输入批次别名备注请输入备注商品清单点击选择商品入库费用添加费用存储' }).locator('uni-button').click();
await this.page.waitForTimeout(300);
// 点击上传input
await this.page.locator('.nut-uploader__input').click();
// 使用 setInputFiles 方式上传
await this.page.locator('body').setInputFiles(imagePath);
}
/**
* 点击付款按钮
*/
async clickPay(): Promise<void> {
await this.payButton.click();
}
/**
* 选择结算账户
* @param accountName 账户名称(默认微信支付)
*/
async selectSettlementAccount(accountName: string = '微信支付'): Promise<void> {
await this.settlementAccount.filter({ hasText: new RegExp(`^${accountName}$`) }).click();
}
/**
* 确认付款弹窗
*/
async confirmPayment(): Promise<void> {
// 等待确认弹窗出现
await this.confirmDialogButton.waitFor({ state: 'visible', timeout: 10000 });
await this.confirmDialogButton.click();
await this.page.waitForTimeout(1000);
}
/**
* 进入采购列表
*/
async goToPurchaseList(): Promise<void> {
await this.purchaseListMenu.click();
await this.page.waitForLoadState('networkidle', { timeout: 30000 });
await this.page.waitForTimeout(2000);
}
/**
* 在采购列表搜索商品
* @param productName 商品名称
*/
async searchInPurchaseList(productName: string): Promise<void> {
// 使用 filter 精确定位到商品名称搜索框(避免匹配到供应商搜索框)
const searchInput = this.page.locator('uni-input').filter({ hasText: '供应商/商品名称' }).locator('input[type="text"]');
await searchInput.waitFor({ state: 'visible', timeout: 10000 });
await searchInput.click();
await this.page.waitForTimeout(300);
await searchInput.fill(productName);
await this.page.waitForTimeout(500);
await this.page.keyboard.press('Enter');
await this.page.waitForTimeout(1000);
}
/**
* 验证采购列表中存在商品
* @param productName 商品名称
*/
async expectPurchaseListContainsProduct(productName: string): Promise<void> {
// 虚拟列表中元素可能处于 hidden 状态,使用 toBeAttached 而非 toBeVisible
const firstItem = this.page.locator('.goods-item').filter({ hasText: productName }).first();
await expect(firstItem).toBeAttached();
}
/**
* 创建采购入库付款单(完整流程)
* @param productName 商品名称
* @param supplierName 供应商名称
* @param warehouseName 仓库名称
* @param quantity 数量
* @param price 单价
* @param expenseAmount 费用金额
* @param purchaserName 采购员名称
* @param licensePlate 车牌号
* @param remark 备注
* @param imagePath 图片路径
*/
async createPurchasePayment(
productName: string,
supplierName: string,
warehouseName: string,
quantity: string = '1',
price: string = '1',
expenseAmount: string = '1',
purchaserName?: string,
licensePlate?: string,
remark: string = '自动化测试',
imagePath?: string
): Promise<void> {
// 选择商品
await this.selectProduct(productName);
// 输入数量
await this.enterQuantity(quantity);
// 输入单价
await this.enterPrice(price);
// 点击完成
await this.clickDone();
// 添加入库费用
await this.addExpense(0, expenseAmount);
// 选择供应商
await this.selectSupplier(supplierName);
// 选择仓库
await this.selectWarehouse(warehouseName);
// 选择采购员
await this.selectPurchaser(purchaserName);
// 输入车牌号
if (licensePlate) {
await this.enterLicensePlate(licensePlate);
}
// 输入备注
await this.enterRemark(remark);
// 上传图片
if (imagePath) {
await this.uploadImage(imagePath);
}
// 点击付款
await this.clickPay();
// 选择结算账户
await this.selectSettlementAccount('微信支付');
// 确认付款
await this.confirmPayment();
// 进入采购列表
await this.goToPurchaseList();
// 搜索商品
await this.searchInPurchaseList(productName);
// 验证商品存在
await this.expectPurchaseListContainsProduct(productName);
}
}