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`); } } } } }; }