import { type Locator, type Page } from '@playwright/test'; import { type reportData } from './reportPage'; export class PerformanceDetailReportPage { page: Page; private readonly _reportData: reportData[]; private subTables: { name: string; locator: Locator }[]; /** * 业绩明细表 */ constructor(page: Page) { this.page = page; this._reportData = [ { name: '项目销售', content: [ { name: '数量', value: 0, lastValue: 0, index: -1 }, { name: '金额', value: 0, lastValue: 0, index: -1 }, { name: '现金业绩', value: 0, lastValue: 0, index: -1 }, ], }, ]; this.subTables = [ { name: '项目销售', locator: this.page.locator('.SERVICE_SALE_table') }, { name: '项目消耗', locator: this.page.locator('.SERVICE_table') }, { name: '项目套餐包销售', locator: this.page.locator('.PACKAGE_table') }, { name: '项目卖品销售', locator: this.page.locator('.GOODS_table') }, { name: '卡销售', locator: this.page.locator('.CARD_table') }, ]; } get reportData(): reportData[] { return this._reportData; } /** * 更新各个表格的索引 */ updateReportDataIndex = async () => { for (const subReportData of this._reportData) { const subTable = this.subTables.find(subTable => subTable.name === subReportData.name); if (!subTable || !subTable.locator) { throw new Error(`${subTable}`); } const currentTableLocator = subTable.locator; await currentTableLocator.waitFor(); const allHeader = await currentTableLocator .locator('.m-table__header-wrapper tr') .nth(1) .locator('th') .allInnerTexts(); for (const content of subReportData.content!) { content.index = allHeader.findIndex(headerText => { return headerText === content.name; }); } } }; /** * 获取子表的合计 */ updateReportDataForTableTotal = async () => { for (const subReportData of this._reportData) { const subTable = this.subTables.find(subTable => subTable.name === subReportData.name); if (!subTable || !subTable.locator) { throw new Error(`${subTable}`); } const currentTableLocator = subTable.locator; await currentTableLocator.waitFor(); const allFooterText = await currentTableLocator .locator('.table_sub-box .m-table-footer td') .allInnerTexts(); for (const content of subReportData.content!) { // 第一个是合计,故+1 const currentIndex = content.index! + 1; content.lastValue = content.value; content.value = Number( allFooterText[currentIndex].trim() === '--' ? 0 : allFooterText[currentIndex].trim(), ); } } }; }