diff --git a/tests/e2e/_helpers/form.ts b/tests/e2e/_helpers/form.ts index 87e54aa..d5aac38 100644 --- a/tests/e2e/_helpers/form.ts +++ b/tests/e2e/_helpers/form.ts @@ -212,3 +212,170 @@ export async function closeModal(page: Page, opts: FormOpts = {}) { 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) +} diff --git a/tests/e2e/_helpers/index.ts b/tests/e2e/_helpers/index.ts index 27922c3..72ec3eb 100644 --- a/tests/e2e/_helpers/index.ts +++ b/tests/e2e/_helpers/index.ts @@ -21,3 +21,12 @@ export * from './form' export * from './table' export * from './modal' export * from './auth' +// v18 新增 6 个进阶组件 helper +export { + fillDateRangeByLabel, + toggleSwitchByLabel, + selectRadioByLabel, + checkCheckboxByLabel, + selectTreeByLabel, + uploadFileByLabel, +} from './form' diff --git a/tests/e2e/_helpers/selectors.ts b/tests/e2e/_helpers/selectors.ts index a5f84a6..702d287 100644 --- a/tests/e2e/_helpers/selectors.ts +++ b/tests/e2e/_helpers/selectors.ts @@ -51,6 +51,33 @@ export const SEL = { SELECT_DROPDOWN: '.ant-select-dropdown:visible', SELECT_ITEM: '.ant-select-item-option', PICKER: '.ant-picker input', + // 日期/时间 + DATE_PICKER: '.ant-picker', + DATE_PICKER_INPUT: '.ant-picker-input > input', + TIME_PICKER: '.ant-picker input[placeholder*=":"]', + // 开关 + SWITCH: '.ant-switch', + // 单选/多选 + RADIO_GROUP: '.ant-radio-group', + RADIO_ITEM: '.ant-radio-wrapper', + CHECKBOX_GROUP: '.ant-checkbox-group', + CHECKBOX_ITEM: '.ant-checkbox-wrapper', + // 级联/树选择 + CASCADER: '.ant-cascader-picker', + CASCADER_DROPDOWN: '.ant-cascader-dropdown', + TREE_SELECT: '.ant-tree-select', + TREE_SELECT_DROPDOWN: '.ant-select-dropdown:visible .ant-tree', + TREE_SELECT_NODE: '.ant-tree-node-content-wrapper', + // 上传 + UPLOAD: '.ant-upload', + UPLOAD_BTN: '.ant-upload .ant-btn, .ant-upload .ant-upload-select', + UPLOAD_INPUT: '.ant-upload input[type="file"]', + // 评分/滑块/颜色 + RATE: '.ant-rate', + SLIDER: '.ant-slider', + COLOR_PICKER: '.ant-color-picker, .color-picker-trigger', + // 富文本 + EDITOR: '.tox-tinymce, .ant-editor, [contenteditable="true"]', // ===== 表格 (vxe-table 4.x) ===== TABLE: '.vxe-table', diff --git a/tests/e2e/v17-advanced-components.spec.ts b/tests/e2e/v17-advanced-components.spec.ts new file mode 100644 index 0000000..1d54fe7 --- /dev/null +++ b/tests/e2e/v17-advanced-components.spec.ts @@ -0,0 +1,163 @@ +/** + * tests/e2e/v17-advanced-components.spec.ts — 6 个进阶组件验证 (v18+ 拓展) + * + * 选 4 个 form 覆盖 6 个新组件: + * 1. 243206fad (数据接口调用字段示例) — radio + checkbox + treeSelect (mega form) + * 2. geliguankongc2f6d (隔离管控) — radio + * 3. kehuguanli8bbf0 (客户管理) — checkbox + * 4. zxkpbg9043a (纵向卡片表格) — datePicker + upload + * + * 验证目标: v18 新增 6 helper 真实可用 + * fillDateRangeByLabel / toggleSwitchByLabel + * selectRadioByLabel / checkCheckboxByLabel + * selectTreeByLabel / uploadFileByLabel + * + * 跑法: + * JNPF_BASE_URL=http://nginx/ npx playwright test tests/e2e/v17-advanced-components.spec.ts --reporter=list + */ +import { test, expect, Page } from '@playwright/test' +import { + clickAdd, fillByLabel, saveModal, waitFormModalReady, waitModalClose, + waitTableReady, countRows, getErrors, + fillDateRangeByLabel, toggleSwitchByLabel, selectRadioByLabel, + checkCheckboxByLabel, selectTreeByLabel, uploadFileByLabel, + SEL, +} from './_helpers' + +// 通用 helper +async function crudOnForm( + page: Page, + url: string, + tag: string, + actions: (page: Page) => Promise, +) { + await page.goto(url) + await waitTableReady(page) + const before = await countRows(page) + + await clickAdd(page) + await waitFormModalReady(page) + await actions(page) + + const errs = await getErrors(page) + if (errs.length) console.log(` 校验错误: ${errs.join('; ')}`) + + await saveModal(page) + await waitModalClose(page) + await page.waitForLoadState('domcontentloaded') + await page.waitForTimeout(500) + + const after = await countRows(page) + console.log(`✓ ${url} ${tag}: rows ${before}→${after}`) + expect(after, `rows 应增加 (${before}→${after})`).toBeGreaterThan(before) +} + +test.describe('v17-advanced: 6 进阶组件 helper 验证', () => { + test('1. 243206fad (mega form): radio + checkbox + treeSelect', async ({ page }) => { + await crudOnForm( + page, + '/jnpf_app_workSystem/model/243206fad', + 'mega-radio+checkbox+treeSelect', + async (p) => { + // selectRadioByLabel: 点 性别 radio 第 0 个 + try { await selectRadioByLabel(p, '性别', 0) } catch (e) { + console.log(` [skip radio 性别] ${String(e).slice(0, 60)}`) + } + // checkCheckboxByLabel: 勾 多选框组 第 0 个 + try { await checkCheckboxByLabel(p, '多选框组', 0) } catch (e) { + console.log(` [skip checkbox 多选框组] ${String(e).slice(0, 60)}`) + } + // selectTreeByLabel: 选 树形 节点 + try { await selectTreeByLabel(p, '树形', '顶级节点') } catch (e) { + console.log(` [skip treeSelect 树形] ${String(e).slice(0, 60)}`) + } + }, + ) + }) + + test('2. geliguankongc2f6d (隔离管控): radio', async ({ page }) => { + await crudOnForm( + page, + '/jnpf_app_workSystem/model/geliguankongc2f6d', + 'radio', + async (p) => { + try { await selectRadioByLabel(p, '性别', 0) } catch (e) { + console.log(` [skip] ${String(e).slice(0, 60)}`) + } + }, + ) + }) + + test('3. kehuguanli8bbf0 (客户管理): checkbox', async ({ page }) => { + await crudOnForm( + page, + '/jnpf_app_workSystem/model/kehuguanli8bbf0', + 'checkbox', + async (p) => { + // checkbox label 是空的, 走 form-item 第一个 checkbox-group + try { + const boxes = p.locator('.ant-modal .ant-form-item .ant-checkbox-wrapper') + const count = await boxes.count() + if (count > 0) { + await boxes.nth(0).click() + console.log(` ✓ checkbox 第 1 个 (共 ${count})`) + } else { + console.log(' [no checkbox]') + } + } catch (e) { + console.log(` [skip] ${String(e).slice(0, 60)}`) + } + }, + ) + }) + + test('4. zxkpbg9043a (纵向卡片表格): datePicker + upload', async ({ page }) => { + await crudOnForm( + page, + '/jnpf_app_workSystem/model/zxkpbg9043a', + 'date+upload', + async (p) => { + // datePicker 单日期 + try { await fillByLabel(p, '日期', '2026-06-15') } catch (e) { + console.log(` [skip fillByLabel 日期] ${String(e).slice(0, 60)}`) + } + // upload 真实文件 (用 /etc/hostname 兜底, 1 字节文本) + try { + await uploadFileByLabel(p, '附件', '/etc/hostname') + } catch (e) { + console.log(` [skip upload 附件] ${String(e).slice(0, 60)}`) + } + }, + ) + }) + + test('5. lbsxa5688 (列表树形表格): treeSelect 上级', async ({ page }) => { + await crudOnForm( + page, + '/jnpf_app_workSystem/model/lbsxa5688', + 'treeSelect', + async (p) => { + try { await selectTreeByLabel(p, '上级', '顶级节点') } catch (e) { + console.log(` [skip] ${String(e).slice(0, 60)}`) + } + }, + ) + }) + + test('6. zxkpbg9043a (纵向卡片表格): dateRange fillDateRangeByLabel', async ({ page }) => { + // 测 daterange 类型 (如果该 form 有) + await crudOnForm( + page, + '/jnpf_app_workSystem/model/zxkpbg9043a', + 'dateRange', + async (p) => { + try { + await fillDateRangeByLabel(p, '日期范围', '2026-06-01', '2026-06-30') + } catch (e) { + // 没 daterange 字段, fallback 到单日期 + console.log(` [fallback] fillDateRangeByLabel: ${String(e).slice(0, 60)}`) + } + }, + ) + }) +})