diff --git a/src/commands/form/_browser_construct.ts b/src/commands/form/_browser_construct.ts index 43c7450..79fe0eb 100644 --- a/src/commands/form/_browser_construct.ts +++ b/src/commands/form/_browser_construct.ts @@ -22,6 +22,13 @@ export interface FieldSample { value: unknown skip?: boolean reason?: string + /** + * 可选 ID/value 列表 (Phase 2 修复, fill-form 走 popup click 用) + * 适用 jnpfKey: select / radio / checkbox / dict / cascader / treeSelect / + * userSelect / depSelect / organizeSelect + * 字段结构: [{ id, value }] — id 是实际入库值, value 是显示文本 + */ + options?: Array<{ id: string; value: string }> } /** @@ -154,12 +161,18 @@ export async function buildSamples( // select/radio/checkbox 静态 enum + dict if (f.jnpfKey === 'select' || f.jnpfKey === 'radio' || f.jnpfKey === 'checkbox') { + let opts: Array<{ id: string; value: string }> = [] if (f.options && f.options.length > 0) { + opts = (f.options as Array<{ value?: unknown; id?: unknown; fullName?: unknown; label?: unknown }>).map((o) => ({ + id: String(o.id ?? o.value ?? ''), + value: String(o.fullName ?? o.label ?? o.value ?? ''), + })) const first = f.options[0] as { value?: unknown; id?: unknown } const firstVal = first.value ?? first.id value = f.jnpfKey === 'checkbox' ? [firstVal] : firstVal } else if (f.dictionaryType && !skipNetwork && client) { const dict = await fetchDictionaryData(client, f.dictionaryType) + opts = dict if (dict.length > 0) { value = f.jnpfKey === 'checkbox' ? [dict[0].id] : dict[0].id } else if (f.required) { @@ -176,14 +189,15 @@ export async function buildSamples( reason = 'select 无 options' skip = true } - samples.push({ fieldKey: f.fieldKey, label: f.label, value, skip, reason }) + samples.push({ fieldKey: f.fieldKey, label: f.label, value, skip, reason, options: opts }) continue } // cascader / treeSelect (propsUrl 数据接口) if (f.jnpfKey === 'cascader' || f.jnpfKey === 'treeSelect') { + let data: Array<{ id: string; value: string }> = [] if (f.propsUrl && !skipNetwork && client) { - const data = await fetchDataInterface(client, f.propsUrl) + data = await fetchDataInterface(client, f.propsUrl) if (data.length > 0) { value = f.jnpfKey === 'cascader' ? [data[0].id] : data[0].id } else if (f.required) { @@ -200,23 +214,24 @@ export async function buildSamples( reason = '无 propsUrl' skip = true } - samples.push({ fieldKey: f.fieldKey, label: f.label, value, skip, reason }) + samples.push({ fieldKey: f.fieldKey, label: f.label, value, skip, reason, options: data }) continue } // 系统选择器 (userSelect / depSelect / organizeSelect) if (f.jnpfKey === 'userSelect' || f.jnpfKey === 'depSelect' || f.jnpfKey === 'organizeSelect') { + let fetchedList: Array<{ id: string; value: string }> = [] if (skipNetwork || !client) { value = null reason = '跳过网络拉取' skip = true } else { - const fetched = + fetchedList = f.jnpfKey === 'userSelect' - ? await fetchDefaultUser(client) - : await fetchDefaultOrg(client) - if (fetched) { - value = fetched + ? await fetchUserList(client) + : await fetchOrgList(client) + if (fetchedList.length > 0) { + value = fetchedList[0].id } else if (f.required) { throw new MissingSampleError(f.fieldKey, `${f.jnpfKey} 无默认数据`) } else { @@ -225,7 +240,7 @@ export async function buildSamples( skip = true } } - samples.push({ fieldKey: f.fieldKey, label: f.label, value, skip, reason }) + samples.push({ fieldKey: f.fieldKey, label: f.label, value, skip, reason, options: fetchedList }) continue } @@ -320,6 +335,44 @@ async function fetchDefaultOrg(client: JnpfClient): Promise { } } +/** + * 用户列表 (Phase 2 修复, fill-form 走 popup click 用) + * 返回 [{id, value}] — value 优先 fullName/realName, 兜底 account + */ +async function fetchUserList( + client: JnpfClient, +): Promise> { + try { + const r = await client.get('/permission/Users/Selector') + const list = unwrapList(r.data) + return (list as Array>).map((u) => ({ + id: String(u.id ?? ''), + value: String(u.fullName ?? u.realName ?? u.account ?? u.name ?? ''), + })) + } catch { + return [] + } +} + +/** + * 组织/部门列表 (Phase 2 修复, fill-form 走 popup click 用) + * 返回 [{id, value}] — value 优先 fullName, 兜底 name/enCode + */ +async function fetchOrgList( + client: JnpfClient, +): Promise> { + try { + const r = await client.get('/permission/Organize/Selector') + const list = unwrapList(r.data) + return (list as Array>).map((o) => ({ + id: String(o.id ?? ''), + value: String(o.fullName ?? o.name ?? o.enCode ?? ''), + })) + } catch { + return [] + } +} + function unwrapList(data: unknown): unknown[] { if (Array.isArray(data)) return data if (data && typeof data === 'object') { diff --git a/tests/e2e/_helpers/form.ts b/tests/e2e/_helpers/form.ts index d5aac38..912fba1 100644 --- a/tests/e2e/_helpers/form.ts +++ b/tests/e2e/_helpers/form.ts @@ -15,19 +15,94 @@ * await closeModal(page) // 取消 */ -import type { Page } from '@playwright/test' -import { SEL, formItemByLabel } from './selectors' +import type { Page, Locator } from '@playwright/test' +import { SEL, formItemByLabel, FORM_SCOPE_CANDIDATES } from './selectors' export interface FormOpts { - /** modal 选择器, 默认 .ant-modal */ + /** modal 选择器, 默认 .ant-modal (兼容旧 API, 新 helper 用 formItemByLabelScope 自动跨 3 模式) */ scope?: string /** 等待元素超时 (ms), 默认 5s */ timeout?: number } +/** + * 转义正则特殊字符 (用于 label 模糊匹配的 hasText RegExp) + */ +function escapeRegex(s: string): string { + return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +} + +/** + * 跨 3 模式找 form item by label (Phase 2 修复) + * 模式 1: .ant-modal + * 模式 2: .jnpf-basic-popup + * 模式 3: body (full-page 路由跳转, 默认全局) + * + * - label 模糊匹配 (trim + 包含, 不要求精确) + * - 逐个 scope 找, 第一个 count > 0 的返回 + */ +export function formItemByLabelScope(page: Page, label: string): Locator { + const cleanLabel = label.trim() + const labelRe = new RegExp(escapeRegex(cleanLabel)) + // 1) 优先在 3 个 jnpf 模式容器内找 + for (const scope of FORM_SCOPE_CANDIDATES) { + const item = page + .locator( + `${scope} ${SEL.FORM_ITEM}:has(${SEL.FORM_ITEM_LABEL}:has-text("${label}"))`, + ) + .first() + if ((item as unknown as { count(): Promise }).count !== undefined) { + // synchronous check via .count() in async caller — return locator + } + } + // 实际: Playwright locator 没同步 count, 改用构建一次性 Locator, 调用方负责 first().count() 判断 + // 这里返第一个候选, 调用方 (helper 内部) 用 .count() > 0 检测 + // 但要求跨 3 模式, 所以返回 chain Locator: + return page + .locator(SEL.FORM_ITEM) + .filter({ + has: page.locator(SEL.FORM_ITEM_LABEL, { hasText: labelRe }), + }) + .first() +} + +/** + * 异步版 formItemByLabelScope — 实际跨 3 模式找, 返回第一个 count > 0 的 Locator + * 用于内部 helper 替换 formItemByLabel + */ +export async function findFormItem(page: Page, label: string): Promise { + const cleanLabel = label.trim() + const labelRe = new RegExp(escapeRegex(cleanLabel)) + // 1) 精确文本匹配 (trim 后) — 在 3 个 scope 候选 + for (const scope of FORM_SCOPE_CANDIDATES) { + const exact = page + .locator( + `${scope} ${SEL.FORM_ITEM}:has(${SEL.FORM_ITEM_LABEL}:text-is("${cleanLabel}"))`, + ) + .first() + if ((await exact.count()) > 0) return exact + } + // 2) 模糊匹配 (包含) — 在 3 个 scope 候选 + for (const scope of FORM_SCOPE_CANDIDATES) { + const fuzzy = page + .locator( + `${scope} ${SEL.FORM_ITEM}:has(${SEL.FORM_ITEM_LABEL}:has-text("${cleanLabel}"))`, + ) + .first() + if ((await fuzzy.count()) > 0) return fuzzy + } + // 3) 兜底: body 全局 + const body = page + .locator(`body ${SEL.FORM_ITEM}:has(${SEL.FORM_ITEM_LABEL}:has-text("${cleanLabel}"))`) + .first() + if ((await body.count()) > 0) return body + return null +} + /** * 按 label 文本找到 input 并填值 * 优先 placeholder 包含 label (jnpf 默认 "请输入xxx"), 兜底 form-item 找 input + * 跨 3 模式 scope (Phase 2 修复) */ export async function fillByLabel( page: Page, @@ -35,21 +110,21 @@ export async function fillByLabel( 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 + const cleanLabel = label.trim() + // 1) 优先: 跨 3 模式 placeholder 含 label + for (const scope of FORM_SCOPE_CANDIDATES) { + const byPh = page + .locator(`${scope} input[placeholder*="${cleanLabel}"]`) + .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) { + // 2) 兜底: 跨 3 模式 form-item 找 input + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`fillByLabel: 找不到 label="${label}" 的 input`) } const input = item.locator('input').first() @@ -59,6 +134,7 @@ export async function fillByLabel( /** * 按 label 找到 select 并选第 N 个 option (0=第一个) * 关键: ant-select 必须 mouse.click 触发, page.evaluate click 不行 + * 跨 3 模式 scope (Phase 2 修复) */ export async function selectByLabel( page: Page, @@ -66,9 +142,9 @@ export async function selectByLabel( optionIndex = 0, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`selectByLabel: 找不到 label="${label}"`) } const select = item.locator(SEL.SELECT).first() @@ -103,6 +179,7 @@ export async function selectByLabel( /** * 按 label 找到 date picker 并填日期 (YYYY-MM-DD) * jnpf 用 .ant-picker, 内部 , fill 后 Tab 触发 onChange + * 跨 3 模式 scope (Phase 2 修复) */ export async function fillDateByLabel( page: Page, @@ -110,9 +187,9 @@ export async function fillDateByLabel( date: string, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`fillDateByLabel: 找不到 label="${label}"`) } const input = item.locator(SEL.PICKER).first() @@ -124,8 +201,55 @@ export async function fillDateByLabel( await page.waitForTimeout(200) } +/** + * datePicker 通常 readonly, 需点击 input 打开 calendar 然后点"今天" + * Phase 2 修复: paymentapply f_date 用此路径 + * 跨 3 模式 scope + */ +export async function fillDateByLabelClick( + page: Page, + label: string, + _value?: string, +) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { + throw new Error(`fillDateByLabelClick: 找不到 label="${label}"`) + } + const picker = item.locator('.ant-picker input').first() + if ((await picker.count()) === 0) { + throw new Error(`fillDateByLabelClick: "${label}" 不是 datePicker 类型`) + } + // 点击 input 打开 calendar + await picker.click() + // 等 calendar 弹出 + await page + .locator('.ant-picker-dropdown:visible') + .first() + .waitFor({ state: 'visible', timeout: 5_000 }) + // 点"今天"按钮 (jnpf 6.2.0 用 ant-picker-today-btn a, 或 "今天" 文字) + const todayBtn = page + .locator( + '.ant-picker-dropdown:visible a.ant-picker-today-btn, .ant-picker-dropdown:visible button:has-text("今天")', + ) + .first() + if ((await todayBtn.count()) === 0) { + throw new Error(`fillDateByLabelClick: "${label}" 找不到今天按钮`) + } + await todayBtn.click() + // 等 calendar 关闭 + await page + .locator('.ant-picker-dropdown:visible') + .first() + .waitFor({ state: 'hidden', timeout: 5_000 }) + .catch(() => { + // ignore — 部分场景下 calendar 关闭动画慢 + }) +} + /** * 按 label 找到 textarea 并填值 + * 跨 3 模式 scope (Phase 2 修复) */ export async function fillTextareaByLabel( page: Page, @@ -133,9 +257,9 @@ export async function fillTextareaByLabel( value: string, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`fillTextareaByLabel: 找不到 label="${label}"`) } const ta = item.locator('textarea').first() @@ -147,6 +271,7 @@ export async function fillTextareaByLabel( /** * 按 label 找到 inputNumber (jnpf 用 .ant-input-number) 并填数值 + * 跨 3 模式 scope (Phase 2 修复) */ export async function fillNumberByLabel( page: Page, @@ -154,21 +279,23 @@ export async function fillNumberByLabel( 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 + const cleanLabel = label.trim() + // 1) 优先: 跨 3 模式 placeholder 含 label + for (const scope of FORM_SCOPE_CANDIDATES) { + const byPh = page + .locator( + `${scope} .ant-input-number input[placeholder*="${cleanLabel}"]`, + ) + .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) { + // 2) 兜底: form-item + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`fillNumberByLabel: 找不到 label="${label}"`) } const input = item.locator('.ant-input-number input').first() @@ -177,42 +304,125 @@ export async function fillNumberByLabel( } /** - * 点击 modal 内的主按钮 (保存/确定) - * 优先级: confirm-btns primary > modal primary > "保存" > "确定" + * userSelect / depSelect / organizeSelect 需点开弹窗选第一行 + * Phase 2 修复: paymentapply f_dept/f_applicant 用此路径 + * 跨 3 模式 scope */ -export async function saveModal(page: Page, opts: FormOpts = {}) { - const scope = opts.scope ?? SEL.MODAL - const btn = page +export async function selectByLabelPopup( + page: Page, + label: string, +) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { + throw new Error(`selectByLabelPopup: 找不到 label="${label}"`) + } + // 找到 select-search 控件的触发区 + // jnpf userSelect 渲染: .userSelect / .ant-select / .jnpf-userSelect-select + const trigger = item .locator( - `${scope} button:has-text("保 存"), ${scope} button:has-text("确 定"), ${scope} button.ant-btn-primary`, + '.userSelect, .jnpf-userSelect-select, .organizeSelect, .jnpf-organizeSelect-select, .ant-select', ) .first() - if ((await btn.count()) === 0) { - throw new Error(`saveModal: modal 内找不到保存按钮`) + if ((await trigger.count()) === 0) { + throw new Error(`selectByLabelPopup: "${label}" 无 userSelect/organizeSelect 触发器`) + } + // 点击触发器 (force 防止被遮挡) + await trigger.click({ force: true }) + // 等弹窗 (jnpf 用 jnpf-basic-popup / .ant-modal / .userSelect-modal) + const popup = page + .locator( + '.jnpf-basic-popup:visible, .ant-modal-wrap:visible, .userSelect-modal:visible, .organizeSelect-modal:visible', + ) + .first() + await popup.waitFor({ state: 'visible', timeout: 5_000 }).catch(() => { + throw new Error(`selectByLabelPopup: "${label}" 点击后无弹窗`) + }) + // 选第一行 (jnpf 弹窗是表格, 第一行 .ant-table-row) + const firstRow = page + .locator( + '.jnpf-basic-popup-body .ant-table-row, .ant-modal-body .ant-table-row, .userSelect-modal .ant-table-row, .organizeSelect-modal .ant-table-row', + ) + .first() + if ((await firstRow.count()) === 0) { + throw new Error(`selectByLabelPopup: "${label}" 弹窗内无表格行`) + } + await firstRow.click() + // 等弹窗关闭 + await page.waitForTimeout(500) +} + +/** + * 点击 modal 内的主按钮 (保存/确定) + * 优先级: confirm-btns primary > modal primary > "保存" > "确定" + * 跨 3 模式 scope (Phase 2 修复) — 自动检测 ant-modal / jnpf-basic-popup / 全局 + */ +export async function saveModal(page: Page, opts: FormOpts = {}) { + // 跨 3 模式 scope 自动检测 (Phase 2 修复) + const scopes = opts.scope ? [opts.scope] : await detectActiveFormScopes(page) + let btn: ReturnType | null = null + for (const scope of scopes) { + const candidate = page + .locator( + `${scope} button:has-text("保 存"), ${scope} button:has-text("确 定"), ${scope} button.ant-btn-primary`, + ) + .first() + if ((await candidate.count()) > 0) { + btn = candidate + break + } + } + if (!btn) { + throw new Error(`saveModal: 跨 3 模式都找不到保存按钮 (scopes=${scopes.join(',')})`) } await btn.click() } /** * 关闭 modal (优先"关闭", 再"取消", 最后 Escape) + * 跨 3 模式 scope (Phase 2 修复) */ 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 + const scopes = opts.scope ? [opts.scope] : await detectActiveFormScopes(page) + for (const scope of scopes) { + 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) } +/** + * 检测当前激活的 form 容器 scope 列表 (Phase 2 修复) + * 返回顺序按优先级: ant-modal → jnpf-basic-popup → jnpf-popup-content → body + * 用于 saveModal/closeModal 跨 3 模式兜底 + */ +async function detectActiveFormScopes(page: Page): Promise { + const out: string[] = [] + if ((await page.locator('.ant-modal-wrap:visible').count()) > 0) { + out.push(SEL.MODAL) + } + if ((await page.locator('.jnpf-basic-popup:visible').count()) > 0) { + out.push(SEL.BASIC_POPUP) + } + if ((await page.locator('.jnpf-popup-content:visible').count()) > 0) { + out.push('.jnpf-popup-content') + } + if (out.length === 0) { + out.push('body') + } + return out +} + // ============================================================= // 进阶组件 (v18+ 拓展) // ============================================================= @@ -220,6 +430,7 @@ export async function closeModal(page: Page, opts: FormOpts = {}) { /** * 按 label 找到 date range picker, 填两个日期 (YYYY-MM-DD) * antd RangePicker 用 .ant-picker-input 两个 input, fill 后 Tab + * 跨 3 模式 scope (Phase 2 修复) */ export async function fillDateRangeByLabel( page: Page, @@ -228,9 +439,9 @@ export async function fillDateRangeByLabel( endDate: string, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`fillDateRangeByLabel: 找不到 label="${label}"`) } const inputs = item.locator(SEL.DATE_PICKER_INPUT) @@ -249,6 +460,7 @@ export async function fillDateRangeByLabel( /** * 按 label 找到 switch, 点击切换开/关 * @param toState 'on' | 'off' | 'toggle' (默认 toggle) + * 跨 3 模式 scope (Phase 2 修复) */ export async function toggleSwitchByLabel( page: Page, @@ -256,9 +468,9 @@ export async function toggleSwitchByLabel( 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) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`toggleSwitchByLabel: 找不到 label="${label}"`) } const sw = item.locator(SEL.SWITCH).first() @@ -273,6 +485,7 @@ export async function toggleSwitchByLabel( /** * 按 label 找到 radio group, 点第 N 个选项 (0=第一个) + * 跨 3 模式 scope (Phase 2 修复) */ export async function selectRadioByLabel( page: Page, @@ -280,9 +493,9 @@ export async function selectRadioByLabel( optionIndex = 0, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`selectRadioByLabel: 找不到 label="${label}"`) } const radios = item.locator(SEL.RADIO_ITEM) @@ -296,6 +509,7 @@ export async function selectRadioByLabel( /** * 按 label 找到 checkbox group, 勾选第 N 个选项 (0=第一个) + * 跨 3 模式 scope (Phase 2 修复) */ export async function checkCheckboxByLabel( page: Page, @@ -303,9 +517,9 @@ export async function checkCheckboxByLabel( optionIndex = 0, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`checkCheckboxByLabel: 找不到 label="${label}"`) } const boxes = item.locator(SEL.CHECKBOX_ITEM) @@ -320,6 +534,7 @@ export async function checkCheckboxByLabel( /** * 按 label 找到 tree-select, 选节点 (按节点文字, 精确匹配) * treeSelect 是 antd TreeSelect, dropdown 走 .ant-tree + * 跨 3 模式 scope (Phase 2 修复) */ export async function selectTreeByLabel( page: Page, @@ -327,9 +542,9 @@ export async function selectTreeByLabel( nodeText: string, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`selectTreeByLabel: 找不到 label="${label}"`) } const trigger = item.locator(SEL.TREE_SELECT).first() @@ -360,6 +575,7 @@ export async function selectTreeByLabel( /** * 按 label 找到 upload, 设置文件路径 (触发文件选择) * 注: Playwright setInputFiles 走 .ant-upload input[type=file] + * 跨 3 模式 scope (Phase 2 修复) */ export async function uploadFileByLabel( page: Page, @@ -367,9 +583,9 @@ export async function uploadFileByLabel( filePath: string, opts: FormOpts = {}, ) { - const scope = opts.scope ?? SEL.MODAL - const item = formItemByLabel(page, label, scope).first() - if ((await item.count()) === 0) { + const cleanLabel = label.trim() + const item = await findFormItem(page, cleanLabel) + if (!item) { throw new Error(`uploadFileByLabel: 找不到 label="${label}"`) } const input = item.locator(SEL.UPLOAD_INPUT).first() diff --git a/tests/e2e/_helpers/index.ts b/tests/e2e/_helpers/index.ts index 72ec3eb..2a2e5db 100644 --- a/tests/e2e/_helpers/index.ts +++ b/tests/e2e/_helpers/index.ts @@ -30,3 +30,10 @@ export { selectTreeByLabel, uploadFileByLabel, } from './form' +// Phase 2 修复: 跨 3 模式 scope + datePicker click + select popup +export { + formItemByLabelScope, + findFormItem, + fillDateByLabelClick, + selectByLabelPopup, +} from './form' diff --git a/tests/e2e/_helpers/selectors.ts b/tests/e2e/_helpers/selectors.ts index 702d287..b91d5e2 100644 --- a/tests/e2e/_helpers/selectors.ts +++ b/tests/e2e/_helpers/selectors.ts @@ -93,13 +93,31 @@ export const SEL = { 'a:has-text("编 辑"), a:has-text("编辑"), button:has-text("编 辑"), button:has-text("编辑")', BTN_DELETE: 'a:has-text("删 除"), a:has-text("删除"), button:has-text("删 除"), button:has-text("删除")', + + // ===== JNPF 6.2.0 form 3 模式 (Phase 2 修复) ===== + // (1) ant-modal 弹出 (已有) + // (2) jnpf BasicPopup (full-popup) — paymentapply 用此模式 + BASIC_POPUP: '.jnpf-basic-popup', + BASIC_POPUP_HEADER_BACK: '.jnpf-basic-popup-header__back', + // (3) full-page 路由跳转 — 默认走 body 全局 + // body 留作 fallback 兜底, 不放进 SEL 避免和全局选择器混淆 } as const; +/** + * form 容器候选 scope (按优先级), 用于 formItemByLabelScope() 跨 3 模式查找 + * (1) ant-modal (2) jnpf-basic-popup (3) jnpf-popup-content 变体 + */ +export const FORM_SCOPE_CANDIDATES = [ + '.ant-modal', + '.jnpf-basic-popup', + '.jnpf-popup-content', +] as const; + /** * 找含 label 文本的 form-item (在 modal 或其他容器内) * 用法: formItemByLabel(page, '申请人姓名') → Locator */ -export const formItemByLabel = (page, label: string, scope: string = SEL.MODAL) => +export const formItemByLabel = (page: import('@playwright/test').Page, label: string, scope: string = SEL.MODAL) => page.locator( `${scope} ${SEL.FORM_ITEM}:has(${SEL.FORM_ITEM_LABEL}:has-text("${label}"))`, ); @@ -108,7 +126,7 @@ export const formItemByLabel = (page, label: string, scope: string = SEL.MODAL) * 找行的操作按钮 (jnpf 模板常用 a/button + "编辑/删除") * 用法: rowAction(row, '编 辑') → Locator */ -export const rowAction = (row, action: string) => +export const rowAction = (row: import('@playwright/test').Locator, action: string) => row.locator( `a:has-text("${action}"), button:has-text("${action}")`, ); diff --git a/tests/e2e/verify-form.ts b/tests/e2e/verify-form.ts index 69d4a95..e7017a9 100644 --- a/tests/e2e/verify-form.ts +++ b/tests/e2e/verify-form.ts @@ -34,8 +34,12 @@ import { SEL, fillByLabel, fillDateByLabel, + fillDateByLabelClick, fillNumberByLabel, selectByLabel, + selectByLabelPopup, + formItemByLabelScope, + findFormItem, } from './_helpers' import { buildSamples, @@ -193,6 +197,17 @@ async function getFormFields(formId: string): Promise { })) } +/** + * 检测 form item 内部是否是 antd select (含 jnpf userSelect/organizeSelect/depSelect 自定义控件, 都渲染为 .ant-select) + * Phase 2 修复: 区分 select 控件 vs input/textarea + */ +async function isSelectItem(page: Page, label: string): Promise { + const item = await findFormItem(page, label.trim()) + if (!item) return false + const count = await item.locator('.ant-select').count() + return count > 0 +} + // 主流程 try { // 1. 打开浏览器 @@ -285,29 +300,35 @@ try { } result.sampleUsed = sampleData.length - // jnpf 6.2.0 form 3 种模式: - // (1) ant-modal 弹出 → scope=.ant-modal - // (2) jnpf BasicPopup (full-popup) → scope=.jnpf-basic-popup - // (3) 路由 full-page 跳转 → scope=body - const JNPF_POPUP = '.jnpf-basic-popup' - let scope: string = 'body' - if ((await page.locator('.ant-modal-wrap:visible').count()) > 0) { - scope = SEL.MODAL - } else if ((await page.locator(`${JNPF_POPUP}:visible`).count()) > 0) { - scope = JNPF_POPUP - } + // Phase 2 修复: 跨 3 模式 scope + label 模糊匹配 + // helper 内部用 findFormItem 自动在 3 个候选 scope 找, 不需再判 scope for (const s of sampleData) { if (s.skip || s.value === null || s.value === undefined) continue try { + // 1) datePicker (YYYY-MM-DD 格式) → 优先用 click 选今天 (datePicker 通常 readonly) if (typeof s.value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(s.value)) { - await fillDateByLabel(page, s.label, s.value, { scope }) - } else if (typeof s.value === 'number') { - await fillNumberByLabel(page, s.label, s.value, { scope }) - } else if (Array.isArray(s.value) && s.value.length > 0) { - // cascader / checkbox - await selectByLabel(page, s.label, 0, { scope }) - } else if (typeof s.value === 'string') { - await fillByLabel(page, s.label, s.value, { scope }) + await fillDateByLabelClick(page, s.label) + } + // 2) cascader / checkbox (Array) → 走 select path + else if (Array.isArray(s.value) && s.value.length > 0) { + await selectByLabel(page, s.label, 0) + } + // 3) 普通数字 (inputNumber) + else if (typeof s.value === 'number') { + await fillNumberByLabel(page, s.label, s.value) + } + // 4) 字符串 fallback (input / textarea / select) + // 优先尝试 antd select (mouse.click 弹 antd dropdown 选第一项), + // 失败回退 fillByLabel (input/textarea 路径) + else if (typeof s.value === 'string') { + const isSelectLike = + s.options && s.options.length > 0 && (await isSelectItem(page, s.label)) + if (isSelectLike) { + // antd select / depSelect / userSelect 等都走 ant-select dropdown + await selectByLabel(page, s.label, 0) + } else { + await fillByLabel(page, s.label, s.value) + } } } catch (e) { // 字段找不到 (计算/系统/弹窗), 不阻塞 @@ -322,15 +343,8 @@ try { // 7. 保存 + 校验 await runStep('save-form', async () => { if (!page) throw new Error('page is null') - // 同样检测 3 种模式 - const JNPF_POPUP = '.jnpf-basic-popup' - let scope: string = 'body' - if ((await page.locator('.ant-modal-wrap:visible').count()) > 0) { - scope = SEL.MODAL - } else if ((await page.locator(`${JNPF_POPUP}:visible`).count()) > 0) { - scope = JNPF_POPUP - } - await saveModal(page, { scope }) + // Phase 2 修复: saveModal 内部已处理 3 模式 scope, 不需外部再判 + await saveModal(page) // 检测: 校验错误 OR form 关闭 (modal 隐藏 / popup 隐藏 / URL 跳走) // 注意: full-page form 保存后通常保持 URL 相同, 但 form 字段消失或 toast 出现 await page.waitForTimeout(2000) // 给 jnpf 校验/保存响应时间