import { type Locator, type Page } from 'playwright'; export class TablePage { page: Page; private readonly _$headerTable: Locator; private readonly _$bodyTable: Locator; private readonly _$fixedLeftTable: Locator; private readonly _$fixedRightTable: Locator; private readonly _$$bodyTrTable: Locator; /** * Initializes a new instance of the TablePage class. * * @param page - The Playwright Page object used to interact with the table elements on the page. */ constructor(page: Page) { this.page = page; this._$headerTable = page.locator('.m-table__header-wrapper'); this._$bodyTable = page.locator('.m-table__body-wrapper'); this._$fixedLeftTable = page.locator('.m-table__fixed-left'); this._$fixedRightTable = page.locator('.m-table__fixed-right'); this._$$bodyTrTable = this._$bodyTable.locator('.main-table-body_tr'); } get fixedRightTable() { return this._$fixedRightTable; } get fixedLeftTable() { return this._$fixedLeftTable; } get bodyTable() { return this._$bodyTable; } get bodyTrTable() { return this._$$bodyTrTable; } get headerTable() { return this._$headerTable; } /** * * @param text * @param options * @returns */ getFirstHeaderTableIndex = async (text: string, options?: { repeat: number }) => { const $tr = this._$headerTable.locator('tr').first(); const $$th = $tr.locator('th'); await $$th.first().waitFor(); const firstHeaderText = await $$th.allInnerTexts(); if (options?.repeat) { return this.findNthIndex(firstHeaderText, text, options.repeat); } return firstHeaderText.findIndex(item => item === text); }; /** * * @param text * @param options * @returns */ getSecondHeaderTableIndex = async (text: string, options?: { repeat: number }) => { const $tr = this._$headerTable.locator('tr').nth(1); const $$th = $tr.locator('th'); await $$th.first().waitFor(); const secondHeaderText = await $$th.allInnerTexts(); if (options?.repeat) { return this.findNthIndex(secondHeaderText, text, options.repeat); } return secondHeaderText.findIndex(item => item === text); }; /** * * @param text * @param options * @returns */ getLeftTableHeaderIndex = async (text: string, options?: { repeat: number }) => { const $tr = this._$fixedLeftTable.locator('.m-table-fixed-header').locator('thead tr').first(); const $$th = $tr.locator('th'); await $$th.first().waitFor(); const leftTableHeaderText = await $$th.allInnerTexts(); if (options?.repeat) { return this.findNthIndex(leftTableHeaderText, text, options.repeat); } return leftTableHeaderText.findIndex(item => item === text); }; /** * 异步获取左侧固定列中指定文本的行索引 * 此函数用于在页面中,根据文本内容查找对应的行索引 * 它首先定位到左侧固定列的
元素,然后遍历