Files
dacli/tests/e2e/_helpers/table.ts
T
ssw 7cc1fb62bc v17: JNPF 浏览器验证标准库 (_helpers/) + p16 重构
标准库 (dacli/tests/e2e/_helpers/, 6 文件 ~800 行):
- selectors.ts: JNPF 6.2.0 通用选择器常量 (SEL/formItemByLabel/rowAction)
- wait.ts: 智能等待 (waitSpaReady/waitTableReady/waitFormModalReady/waitRowCountChange)
  不用 networkidle, 用具体元素 selector, 默认 15s (vs 30s networkidle)
- nav.ts: 菜单导航 (clickMenu 精确匹配 + expandCategory)
- form.ts: 表单操作 (fillByLabel/selectByLabel/fillDateByLabel/fillTextareaByLabel/
  fillNumberByLabel/saveModal/closeModal)
- table.ts: vxe-table 操作 (countRows/findRow/clickRowAction/getTotalText/clickAdd)
- modal.ts: 模态框 (waitOpen/waitClose/getErrors/isOpen/getTitle)
- index.ts: 聚合导出
- auth.ts: 已存在 (login + storageState 6h TTL)

p16 spec 重构:
- tests/e2e/p16-ui-real-crud.spec.ts: 226 → 180 行 (-20%)
- 用 _helpers 替代内联 selector, 智能等待替代 networkidle
- typecheck 通过

已知问题 (与重构无关):
- tds_leave 是 webType=1 (仅表单), 无 vxe-table 列表, p16 1st test 失败
- 原版 p16 同样 fail — 测试数据配置问题, 不是 refactor 引入

后续 (Python 镜像 + tds_leave webType 修复) 留 task #22
2026-06-15 13:05:39 +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()
}