/** * _helpers/table.ts — vxe-table 操作 helpers * * jnpf 6.2.0 列表用 vxe-table 4.x: * - 数据行: .vxe-body--row * - 分页: .vxe-pager--total (footer, "共 N 条" 文字) * - 行操作列: 通用 编辑/删除 (jnpf 模板生成) * * 用法: * const before = await countRows(page) * await clickAdd(page) * await waitFormModalReady(page) * await fillByLabel(page, '名称', 'xxx') * await saveModal(page) * await waitRowCountChange(page, before, +1) // 自动等行数变化 */ import type { Page, Locator } from '@playwright/test' import { SEL } from './selectors' /** 数 vxe-table 数据行数 */ export async function countRows(page: Page): Promise { return await page.locator(SEL.TABLE_ROW).count() } /** 找含文本的行 (返回 Locator, 链式 .first()/.nth()) */ export function findRow(page: Page, text: string): Locator { return page.locator(`${SEL.TABLE_ROW}:has-text("${text}")`) } /** * 在指定行点操作 (编辑/删除) * 找不到行或按钮抛错 (不静默, 显式失败) */ export async function clickRowAction( page: Page, rowText: string, action: string, ) { const row = findRow(page, rowText).first() if ((await row.count()) === 0) { throw new Error(`clickRowAction: 找不到含 "${rowText}" 的行`) } const btn = row .locator(`a:has-text("${action}"), button:has-text("${action}")`) .first() if ((await btn.count()) === 0) { throw new Error( `clickRowAction: 行 "${rowText}" 无 "${action}" 按钮`, ) } await btn.click() } /** 拿分页总数文字 (e.g. "共 3 条") */ export async function getTotalText(page: Page): Promise { const footer = page.locator(SEL.TABLE_PAGER_TOTAL).first() if ((await footer.count()) > 0) { return (await footer.textContent()) ?? '' } // fallback: 找含"共"和"条" 的文字 return ( (await page .locator('text=/共\\s*\\d+\\s*条/') .first() .textContent() .catch(() => '')) ?? '' ) } /** 解析分页总数文字里的数字, e.g. "共 3 条" → 3 */ export async function getTotalCount(page: Page): Promise { const text = await getTotalText(page) const m = text.match(/(\d+)/) return m ? Number(m[1]) : 0 } /** 点列表页"新增"按钮 (列表顶部) */ export async function clickAdd(page: Page) { const btn = page.locator(SEL.BTN_NEW).first() if ((await btn.count()) === 0) { throw new Error(`clickAdd: 列表页找不到"新增"按钮`) } await btn.click() }