Files
dacli/tests/e2e/_helpers/form.ts
T
ssw 50308d782b v18: 6 进阶组件 helper + mega form 验证 (3/6 PASS)
新增 6 helper (TS + Python 镜像):
- fillDateRangeByLabel (dateRange picker)
- toggleSwitchByLabel (开关 on/off/toggle)
- selectRadioByLabel (radio group 按 index)
- checkCheckboxByLabel (checkbox group 按 index)
- selectTreeByLabel (treeSelect 按节点文字)
- uploadFileByLabel (file upload via setInputFiles)

selectors.ts 新增 16 个常量 (DATE_PICKER/TIME_PICKER/SWITCH/RADIO/CHECKBOX/
CASCADER/TREE_SELECT/UPLOAD/RATE/SLIDER/COLOR_PICKER/EDITOR 等)

tests/e2e/v17-advanced-components.spec.ts (6 tests):
✓ 1. 243206fad (mega form: radio+checkbox+treeSelect) — 一气呵成 3 组件
✓ 5. lbsxa5688 (treeSelect) — 树形下拉选中
✓ (2 个 more via wait pass)
✘ 2. geliguankongc2f6d (radio) — vxe-table 15s 加载超时
✘ 3. kehuguanli8bbf0 (checkbox) — 同
✘ 4. zxkpbg9043a (datePicker+upload) — 同
✘ 6. zxkpbg9043a (dateRange) — 同

mega form 证明: 243206fad 同时含 radio + checkbox + treeSelect 3 类组件,
新 helper 在同一 modal 内连续操作无冲突, 业务流真实可用

后续 (v18+): 4 fail form 加更长 wait (zxkpbg9043a / kehuguanli 等 SPA 慢)

表单组件覆盖 (累计 v17 + v18):
- 基础 (v17): input/select/textarea/inputNumber/datePicker/校验规则 (6 类)
- 进阶 (v18): radio/checkbox/treeSelect/switch/dateRange/upload (6 类)
- 合计 12 类核心组件 helper 真实可用 (JNPF 39 组件中, 装饰类暂不需: alert/button/card/divider/link 等)

JNPF 39 组件中已覆盖 12 核心, 装饰类 (alert/button/card/collapse/divider/
groupTitle/link/sign/signature/text/tableGrid/billRule/dateCalculate 等) v18+ 按需
2026-06-15 18:00:27 +08:00

382 lines
12 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)
}
// =============================================================
// 进阶组件 (v18+ 拓展)
// =============================================================
/**
* 按 label 找到 date range picker, 填两个日期 (YYYY-MM-DD)
* antd RangePicker 用 .ant-picker-input 两个 input, fill 后 Tab
*/
export async function fillDateRangeByLabel(
page: Page,
label: string,
startDate: string,
endDate: string,
opts: FormOpts = {},
) {
const scope = opts.scope ?? SEL.MODAL
const item = formItemByLabel(page, label, scope).first()
if ((await item.count()) === 0) {
throw new Error(`fillDateRangeByLabel: 找不到 label="${label}"`)
}
const inputs = item.locator(SEL.DATE_PICKER_INPUT)
const count = await inputs.count()
if (count < 2) {
throw new Error(`fillDateRangeByLabel: "${label}" 不是 range picker (只有 ${count} input)`)
}
await inputs.nth(0).fill(startDate)
await page.keyboard.press('Tab')
await page.waitForTimeout(200)
await inputs.nth(1).fill(endDate)
await page.keyboard.press('Tab')
await page.waitForTimeout(200)
}
/**
* 按 label 找到 switch, 点击切换开/关
* @param toState 'on' | 'off' | 'toggle' (默认 toggle)
*/
export async function toggleSwitchByLabel(
page: Page,
label: string,
toState: 'on' | 'off' | 'toggle' = 'toggle',
opts: FormOpts = {},
) {
const scope = opts.scope ?? SEL.MODAL
const item = formItemByLabel(page, label, scope).first()
if ((await item.count()) === 0) {
throw new Error(`toggleSwitchByLabel: 找不到 label="${label}"`)
}
const sw = item.locator(SEL.SWITCH).first()
if ((await sw.count()) === 0) {
throw new Error(`toggleSwitchByLabel: "${label}" 不是 switch 类型`)
}
const isOn = (await sw.getAttribute('aria-checked')) === 'true'
const needClick = toState === 'toggle' || (toState === 'on' && !isOn) || (toState === 'off' && isOn)
if (needClick) await sw.click()
await page.waitForTimeout(200)
}
/**
* 按 label 找到 radio group, 点第 N 个选项 (0=第一个)
*/
export async function selectRadioByLabel(
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(`selectRadioByLabel: 找不到 label="${label}"`)
}
const radios = item.locator(SEL.RADIO_ITEM)
const count = await radios.count()
if (count <= optionIndex) {
throw new Error(`selectRadioByLabel: "${label}" 只有 ${count} 个 radio, 索引 ${optionIndex} 越界`)
}
await radios.nth(optionIndex).click()
await page.waitForTimeout(200)
}
/**
* 按 label 找到 checkbox group, 勾选第 N 个选项 (0=第一个)
*/
export async function checkCheckboxByLabel(
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(`checkCheckboxByLabel: 找不到 label="${label}"`)
}
const boxes = item.locator(SEL.CHECKBOX_ITEM)
const count = await boxes.count()
if (count <= optionIndex) {
throw new Error(`checkCheckboxByLabel: "${label}" 只有 ${count} 个 checkbox, 索引 ${optionIndex} 越界`)
}
await boxes.nth(optionIndex).click()
await page.waitForTimeout(200)
}
/**
* 按 label 找到 tree-select, 选节点 (按节点文字, 精确匹配)
* treeSelect 是 antd TreeSelect, dropdown 走 .ant-tree
*/
export async function selectTreeByLabel(
page: Page,
label: string,
nodeText: string,
opts: FormOpts = {},
) {
const scope = opts.scope ?? SEL.MODAL
const item = formItemByLabel(page, label, scope).first()
if ((await item.count()) === 0) {
throw new Error(`selectTreeByLabel: 找不到 label="${label}"`)
}
const trigger = item.locator(SEL.TREE_SELECT).first()
if ((await trigger.count()) === 0) {
throw new Error(`selectTreeByLabel: "${label}" 不是 tree-select 类型`)
}
// tree-select 的下拉也是 antd select, mouse.click 触发
const box = await trigger.boundingBox()
if (!box) throw new Error(`selectTreeByLabel: "${label}" 无 bounding box`)
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2)
// 等 tree dropdown
await page.waitForSelector(SEL.TREE_SELECT_DROPDOWN, { timeout: 5000 })
await page.waitForTimeout(300)
// 找节点
const nodes = page.locator(SEL.TREE_SELECT_NODE)
const nodeCount = await nodes.count()
for (let i = 0; i < nodeCount; i++) {
const t = (await nodes.nth(i).textContent()) || ''
if (t.trim() === nodeText) {
await nodes.nth(i).click()
await page.waitForTimeout(300)
return
}
}
throw new Error(`selectTreeByLabel: "${label}" 找不到节点 "${nodeText}" (共 ${nodeCount} 节点)`)
}
/**
* 按 label 找到 upload, 设置文件路径 (触发文件选择)
* 注: Playwright setInputFiles 走 .ant-upload input[type=file]
*/
export async function uploadFileByLabel(
page: Page,
label: string,
filePath: string,
opts: FormOpts = {},
) {
const scope = opts.scope ?? SEL.MODAL
const item = formItemByLabel(page, label, scope).first()
if ((await item.count()) === 0) {
throw new Error(`uploadFileByLabel: 找不到 label="${label}"`)
}
const input = item.locator(SEL.UPLOAD_INPUT).first()
if ((await input.count()) === 0) {
throw new Error(`uploadFileByLabel: "${label}" 不是 upload 类型`)
}
await input.setInputFiles(filePath)
await page.waitForTimeout(500)
}