标准库 (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
215 lines
6.4 KiB
TypeScript
215 lines
6.4 KiB
TypeScript
/**
|
|
* _helpers/form.ts — 表单操作 helpers
|
|
*
|
|
* 核心模式:
|
|
* - **按 label 找 input/select/textarea** — jnpf 6.2.0 label 是稳定契约
|
|
* - **select 必用 mouse.click 触发** (page.evaluate click 不行, antd v-model 内部监听 mouse 事件)
|
|
* - **保存/取消走 modal 内主按钮** (优先级: confirm-btns primary > modal primary)
|
|
*
|
|
* 用法示例:
|
|
* await fillByLabel(page, '申请人姓名', '张三')
|
|
* await selectByLabel(page, '所在部门', 0) // 选第 1 个 option
|
|
* await fillDateByLabel(page, '起始日期', '2026-06-15')
|
|
* await fillTextareaByLabel(page, '请假事由', '...')
|
|
* await saveModal(page) // 点"确定"或"保存"
|
|
* await closeModal(page) // 取消
|
|
*/
|
|
|
|
import type { Page } from '@playwright/test'
|
|
import { SEL, formItemByLabel } from './selectors'
|
|
|
|
export interface FormOpts {
|
|
/** modal 选择器, 默认 .ant-modal */
|
|
scope?: string
|
|
/** 等待元素超时 (ms), 默认 5s */
|
|
timeout?: number
|
|
}
|
|
|
|
/**
|
|
* 按 label 文本找到 input 并填值
|
|
* 优先 placeholder 包含 label (jnpf 默认 "请输入xxx"), 兜底 form-item 找 input
|
|
*/
|
|
export async function fillByLabel(
|
|
page: Page,
|
|
label: string,
|
|
value: string,
|
|
opts: FormOpts = {},
|
|
) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
const timeout = opts.timeout ?? 5_000
|
|
|
|
// 1) 优先: placeholder 含 label
|
|
const byPh = page
|
|
.locator(`${scope} input[placeholder*="${label}"]`)
|
|
.first()
|
|
if ((await byPh.count()) > 0) {
|
|
await byPh.fill(value, { timeout })
|
|
return
|
|
}
|
|
|
|
// 2) 兜底: 找含 label 的 form-item 内的 input
|
|
const item = formItemByLabel(page, label, scope).first()
|
|
if ((await item.count()) === 0) {
|
|
throw new Error(`fillByLabel: 找不到 label="${label}" 的 input`)
|
|
}
|
|
const input = item.locator('input').first()
|
|
await input.fill(value, { timeout })
|
|
}
|
|
|
|
/**
|
|
* 按 label 找到 select 并选第 N 个 option (0=第一个)
|
|
* 关键: ant-select 必须 mouse.click 触发, page.evaluate click 不行
|
|
*/
|
|
export async function selectByLabel(
|
|
page: Page,
|
|
label: string,
|
|
optionIndex = 0,
|
|
opts: FormOpts = {},
|
|
) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
const item = formItemByLabel(page, label, scope).first()
|
|
if ((await item.count()) === 0) {
|
|
throw new Error(`selectByLabel: 找不到 label="${label}"`)
|
|
}
|
|
const select = item.locator(SEL.SELECT).first()
|
|
if ((await select.count()) === 0) {
|
|
throw new Error(`selectByLabel: "${label}" 不是 select 类型`)
|
|
}
|
|
const box = await select.boundingBox()
|
|
if (!box) {
|
|
throw new Error(`selectByLabel: "${label}" 无 bounding box`)
|
|
}
|
|
// mouse.click 触发 antd dropdown
|
|
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2)
|
|
await page.waitForSelector(SEL.SELECT_DROPDOWN, { timeout: 5_000 })
|
|
// 等 dropdown 动画 + 选项渲染
|
|
await page.waitForSelector(
|
|
`${SEL.SELECT_DROPDOWN} ${SEL.SELECT_ITEM}`,
|
|
{ timeout: 5_000 },
|
|
)
|
|
const options = page.locator(
|
|
`${SEL.SELECT_DROPDOWN} ${SEL.SELECT_ITEM}`,
|
|
)
|
|
const count = await options.count()
|
|
if (count <= optionIndex) {
|
|
throw new Error(
|
|
`selectByLabel: "${label}" 只有 ${count} 个 option, 索引 ${optionIndex} 越界`,
|
|
)
|
|
}
|
|
await options.nth(optionIndex).click()
|
|
await page.waitForTimeout(200)
|
|
}
|
|
|
|
/**
|
|
* 按 label 找到 date picker 并填日期 (YYYY-MM-DD)
|
|
* jnpf 用 .ant-picker, 内部 <input>, fill 后 Tab 触发 onChange
|
|
*/
|
|
export async function fillDateByLabel(
|
|
page: Page,
|
|
label: string,
|
|
date: string,
|
|
opts: FormOpts = {},
|
|
) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
const item = formItemByLabel(page, label, scope).first()
|
|
if ((await item.count()) === 0) {
|
|
throw new Error(`fillDateByLabel: 找不到 label="${label}"`)
|
|
}
|
|
const input = item.locator(SEL.PICKER).first()
|
|
if ((await input.count()) === 0) {
|
|
throw new Error(`fillDateByLabel: "${label}" 不是日期类型`)
|
|
}
|
|
await input.fill(date)
|
|
await page.keyboard.press('Tab')
|
|
await page.waitForTimeout(200)
|
|
}
|
|
|
|
/**
|
|
* 按 label 找到 textarea 并填值
|
|
*/
|
|
export async function fillTextareaByLabel(
|
|
page: Page,
|
|
label: string,
|
|
value: string,
|
|
opts: FormOpts = {},
|
|
) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
const item = formItemByLabel(page, label, scope).first()
|
|
if ((await item.count()) === 0) {
|
|
throw new Error(`fillTextareaByLabel: 找不到 label="${label}"`)
|
|
}
|
|
const ta = item.locator('textarea').first()
|
|
if ((await ta.count()) === 0) {
|
|
throw new Error(`fillTextareaByLabel: "${label}" 不是 textarea 类型`)
|
|
}
|
|
await ta.fill(value)
|
|
}
|
|
|
|
/**
|
|
* 按 label 找到 inputNumber (jnpf 用 .ant-input-number) 并填数值
|
|
*/
|
|
export async function fillNumberByLabel(
|
|
page: Page,
|
|
label: string,
|
|
value: number | string,
|
|
opts: FormOpts = {},
|
|
) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
// 优先 placeholder 含 label
|
|
const byPh = page
|
|
.locator(
|
|
`${scope} .ant-input-number input[placeholder*="${label}"]`,
|
|
)
|
|
.first()
|
|
if ((await byPh.count()) > 0) {
|
|
await byPh.fill(String(value))
|
|
await page.keyboard.press('Tab')
|
|
return
|
|
}
|
|
// 兜底: form-item
|
|
const item = formItemByLabel(page, label, scope).first()
|
|
if ((await item.count()) === 0) {
|
|
throw new Error(`fillNumberByLabel: 找不到 label="${label}"`)
|
|
}
|
|
const input = item.locator('.ant-input-number input').first()
|
|
await input.fill(String(value))
|
|
await page.keyboard.press('Tab')
|
|
}
|
|
|
|
/**
|
|
* 点击 modal 内的主按钮 (保存/确定)
|
|
* 优先级: confirm-btns primary > modal primary > "保存" > "确定"
|
|
*/
|
|
export async function saveModal(page: Page, opts: FormOpts = {}) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
const btn = page
|
|
.locator(
|
|
`${scope} button:has-text("保 存"), ${scope} button:has-text("确 定"), ${scope} button.ant-btn-primary`,
|
|
)
|
|
.first()
|
|
if ((await btn.count()) === 0) {
|
|
throw new Error(`saveModal: modal 内找不到保存按钮`)
|
|
}
|
|
await btn.click()
|
|
}
|
|
|
|
/**
|
|
* 关闭 modal (优先"关闭", 再"取消", 最后 Escape)
|
|
*/
|
|
export async function closeModal(page: Page, opts: FormOpts = {}) {
|
|
const scope = opts.scope ?? SEL.MODAL
|
|
for (const sel of [
|
|
`${scope} ${SEL.MODAL_CLOSE}`,
|
|
`${scope} ${SEL.MODAL_CANCEL}`,
|
|
]) {
|
|
const btn = page.locator(sel).first()
|
|
if ((await btn.count()) > 0) {
|
|
await btn.click()
|
|
await page.waitForTimeout(300)
|
|
return
|
|
}
|
|
}
|
|
await page.keyboard.press('Escape')
|
|
await page.waitForTimeout(300)
|
|
}
|