import { Page } from '@playwright/test'; import { reportData } from './reportPage'; export class SpendingSummaryReportPage { page: Page; _reportData: reportData[]; /** * 开支汇总表 */ 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 }, ], }, { name: '日常开支', content: [{ name: '顾客餐', value: 0, lastValue: 0, index: -1 }], }, ]; } get reportData(): reportData[] { return this._reportData; } /** * 获取报表数据的列的index */ updateReportDataIndex = async (): Promise => { const thLocator = this.page.locator('.m-table__header-wrapper tr').last().locator('th'); await thLocator.first().waitFor(); const headers = await thLocator.allInnerTexts(); for (const category of this._reportData) { for (const content of category.content!) { // 拿取th.name列的列数 let index = -1; let occurrence = 0; index = headers.findIndex(headerText => { if (headerText === content.name) { occurrence++; // 每次找到相同的文本,计数加1 if (category.name === '卖品销售') { // 找到第二次出现的 content.name return occurrence === 2; } else { // 找到第一次出现的 content.name return occurrence === 1; } } return false; }); if (index !== -1) { content.index = index + 1; console.log(`${content.name} 列是第 ${index + 1} 列`); } else { throw new Error(`没有找到${content.name}列`); } } } }; /** * * @param {1|2} store */ updateReportData = async (store: number): Promise => { const trLocator = this.page.locator('.main-table-body_tr').nth(store); await trLocator.first().waitFor(); for (const category of this._reportData) { for (const content of category.content!) { // 保存上一次的数据 content.lastValue = content.value; // 拿取当前的数据 const tdLocator = trLocator.locator('td'); const headers = await tdLocator.allInnerTexts(); if (headers.length === 0) { throw new Error('没有获取到数据'); } content.value = Number(headers[content.index!].trim() === '--' ? 0 : headers[content.index!].trim()); } } }; }