Files
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +08:00

86 lines
2.5 KiB
TypeScript

/**
* _helpers/table.ts — vxe-table 操作 helpers
*
* jnpf 6.2.0 列表用 vxe-table 4.x:
* - 数据行: .vxe-body--row
* - 分页: .vxe-pager--total (footer, "共 N 条" 文字)
* - 行操作列: 通用 <a>编辑/删除 (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<number> {
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<string> {
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<number> {
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()
}