Files
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

90 lines
3.1 KiB
TypeScript

import { Page, Locator } from '@playwright/test'
export class TableHelper {
private page: Page
constructor(page: Page) {
this.page = page
}
async getRowCount(tableSelector: string): Promise<number> {
const rows = await this.page.locator(`${tableSelector} tbody tr`).count()
return rows
}
async getCellText(tableSelector: string, row: number, column: number): Promise<string> {
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<string[]> {
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<string[]> {
const headers = await this.page.locator(`${tableSelector} thead tr th`).allTextContents()
return headers
}
async findRowByText(tableSelector: string, text: string): Promise<number> {
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<number[]> {
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