This repository has been archived on 2025-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
hlk_autotest/tests/pages/report/customerConsumptionAnalysisReport.ts
LingandRX 345e68a1b4 refactor(tests): 重构测试代码并优化环境变量配置
- 更新环境变量配置方式,使用 dotenv 库
- 重构员工登录和权限相关代码,提高可维护性
- 优化测试数据结构,增加类型注解
- 删除冗余代码,提高代码可读性
2025-01-01 13:02:27 +08:00

104 lines
3.7 KiB
TypeScript

import { Page } from '@playwright/test';
import { type reportData } from './reportPage';
import { Customer } from '@/utils/customer';
export class CustomerConsumptionAnalysisReportPage {
readonly page: Page;
private readonly _reportData: reportData[];
/**
* 顾客消费分析表
*/
constructor(page: Page) {
this.page = page;
this._reportData = [
{ name: '到店次数', value: 0, lastValue: 0, index: -1 },
{ name: '消费次数', value: 0, lastValue: 0, index: -1 },
{ name: '消费金额', value: 0, lastValue: 0, index: -1 },
{
name: '护理(购买)',
content: [
{ name: '现金', value: 0, lastValue: 0, index: -1 },
{ name: '划卡金', value: 0, lastValue: 0, index: -1 },
],
},
];
}
get reportData() {
return this._reportData;
}
/**
* 设置报表数据的index
*/
updateReportDataIndex = async () => {
const $firstTr = this.page.locator('.m-table__header-wrapper tr').nth(0).locator('th');
await $firstTr.first().waitFor();
const allFirstTrText = await $firstTr.allInnerTexts();
const $secondTr = this.page.locator('.m-table__header-wrapper tr').nth(1).locator('th');
const allSecondTrText = await $secondTr.allInnerTexts();
for (const category of this._reportData) {
// 找寻一级标题的index
if (typeof category.content === 'undefined') {
category.index = allFirstTrText.findIndex(hedader => hedader === category.name);
}
// 找寻二级标题的index
if (category.content) {
for (const content of category.content) {
content.index = allSecondTrText.findIndex(hedader => hedader === content.name);
}
}
}
};
/**
* 获取报表内容
* @param {Customer} customer 卡名称
*/
updateReportData = async (customer: Customer) => {
await this.page.locator('.main-table-body_tr').filter({ hasText: customer.phone }).waitFor();
const customerDataArray = await this.page
.locator('.main-table-body_tr')
.filter({ hasText: customer.phone })
.locator('td')
.allInnerTexts();
for (const category of this._reportData) {
let value: string;
// 找寻一级标题的index
if (typeof category.content === 'undefined') {
category.lastValue = category.value;
if (category.index) {
value =
customerDataArray[category.index].trim() === '--'
? '0'
: customerDataArray[category.index].trim();
category.value = Number(value);
} else {
throw new Error(`找不到${category.name}的index`);
}
}
if (category.content) {
// 找寻二级标题的index
for (const content of category.content) {
content.lastValue = content.value;
if (content.index) {
value =
customerDataArray[content.index].trim() === '--'
? '0'
: customerDataArray[content.index].trim();
content.value = Number(value);
} else {
throw new Error(`找不到${content.name}的index`);
}
}
}
}
};
}