import type { Page } from '@playwright/test' /** * 用户旅程测试共享工具函数(可复用) * * 统一 uni-app H5 端的交互辅助逻辑,供 e2e/journeys/*.spec.ts 复用。 * 关键实现说明: * - uni-app 日期选择器在 H5 渲染为隐藏的 input[type=date],通过设置 value 并派发 change 触发组件更新 * - uni-app 输入框渲染为 包装,需对其内部 真实输入(pressSequentially)才能触发 @input */ /** 设置日期输入(隐藏的 input[type=date],派发 change 触发组件更新) */ export async function setDateInput(page: Page, selector: string, value: string): Promise { await page.evaluate(([sel, v]) => { const input = document.querySelector(sel) as HTMLInputElement | null if (!input) return input.value = v input.dispatchEvent(new Event('change', { bubbles: true })) }, [selector, value] as const) await page.waitForTimeout(400) } /** 输入文本到 uni-input 包装的输入框(真实输入以触发 @input) */ export async function typeIntoInput(page: Page, selector: string, value: string, waitMs = 900): Promise { await page.locator(selector).pressSequentially(value, { delay: 30 }) await page.waitForTimeout(waitMs) } /** 输入出生地(uni-input 包装,触发坐标自动识别) */ export async function setBirthPlace(page: Page, value: string): Promise { await typeIntoInput(page, 'uni-input.text-input input', value) } /** 点击指定文本的按钮(.button-text 内容匹配) */ export async function clickButton(page: Page, text: string | RegExp): Promise { await page.locator('.button-text', { hasText: text }).first().click() await page.waitForTimeout(500) } /** 打开 uni-app 自定义选择器并验证其弹出(返回是否打开) */ export async function openCustomPicker(page: Page): Promise { const opened = await page.evaluate(() => document.querySelectorAll('.uni-picker-item').length > 0) return opened } /** 关闭 uni-app 自定义选择器(Cancel,容错) */ export async function closeCustomPicker(page: Page): Promise { await page.locator('.uni-picker-action-cancel').click({ force: true, timeout: 3000 }).catch(() => {}) await page.waitForTimeout(300) } /** 直接导航到指定页面 */ export async function gotoPage(page: Page, path: string): Promise { await page.goto(`/#${path}`) await page.waitForLoadState('networkidle') await page.waitForTimeout(1000) }