import { Page, Locator } from '@playwright/test' export class TableHelper { private page: Page constructor(page: Page) { this.page = page } async getRowCount(tableSelector: string): Promise { const rows = await this.page.locator(`${tableSelector} tbody tr`).count() return rows } async getCellText(tableSelector: string, row: number, column: number): Promise { const cell = this.page.locator(`${tableSelector} tbody tr:nth-child(${row}) td:nth-child(${column})`) return await cell.textContent() || '' } async getRowData(tableSelector: string, row: number): Promise { const selector = `${tableSelector} tbody tr:nth-child(${row}) td` const cells = await this.page.locator(selector).allTextContents() return cells } async clickRowAction(tableSelector: string, row: number, actionSelector: string) { const actionButton = this.page.locator(`${tableSelector} tbody tr:nth-child(${row}) ${actionSelector}`); await actionButton.click(); } async sortByColumn(tableSelector: string, column: number) { const header = this.page.locator(`${tableSelector} thead tr th:nth-child(${column})`) await header.click() } async waitForTableToLoad(tableSelector: string, timeout: number = 5000) { await this.page.waitForSelector(`${tableSelector} tbody tr`, { state: 'visible', timeout }) } async getTableHeaders(tableSelector: string): Promise { const headers = await this.page.locator(`${tableSelector} thead tr th`).allTextContents() return headers } async findRowByText(tableSelector: string, text: string): Promise { const rows = await this.page.locator(`${tableSelector} tbody tr`).all() for (let i = 0; i < rows.length; i++) { const rowText = await rows[i].textContent() if (rowText?.includes(text)) { return i + 1 } } return -1 } async selectRow(tableSelector: string, row: number) { const checkbox = this.page.locator(`${tableSelector} tbody tr:nth-child(${row}) input[type="checkbox"]`) await checkbox.check() } async deselectRow(tableSelector: string, row: number) { const checkbox = this.page.locator(`${tableSelector} tbody tr:nth-child(${row}) input[type="checkbox"]`) await checkbox.uncheck() } async selectAllRows(tableSelector: string) { const checkbox = this.page.locator(`${tableSelector} thead input[type="checkbox"]`) await checkbox.check() } async getSelectedRows(tableSelector: string): Promise { const checkboxes = await this.page.locator(`${tableSelector} tbody input[type="checkbox"]:checked`).all() const selectedRows: number[] = [] for (let i = 0; i < checkboxes.length; i++) { const row = await checkboxes[i].locator('xpath=ancestor::tr').evaluate((el, index) => { const rows = el.closest('tbody')?.querySelectorAll('tr') return rows ? Array.from(rows).indexOf(el.closest('tr') as HTMLTableRowElement) + 1 : -1 }, i) if (row > 0) { selectedRows.push(row) } } return selectedRows } } export default TableHelper