// apps/miniprogram/e2e/helpers/automator-client.ts import automator from 'miniprogram-automator'; const DEFAULT_CLI_PATH = 'C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat'; const DEFAULT_PROJECT_PATH = process.cwd(); export class AutomatorClient { private mini: automator.MiniProgram | null = null; async connect(cliPath?: string, projectPath?: string) { this.mini = await automator.launch({ cliPath: cliPath || DEFAULT_CLI_PATH, projectPath: projectPath || DEFAULT_PROJECT_PATH, }); } async disconnect() { if (this.mini) { await this.mini.close(); this.mini = null; } } private getMini(): automator.MiniProgram { if (!this.mini) throw new Error('AutomatorClient 未连接,请先调用 connect()'); return this.mini; } async currentPage(): Promise { return this.getMini().currentPage(); } async navigateTo(path: string, _query?: Record) { const page = await this.getMini().navigateTo(`/${path.replace(/^\//, '')}`); return page; } async navigateBack() { await this.getMini().navigateBack(); } async reLaunch(path: string) { await this.getMini().reLaunch(`/${path.replace(/^\//, '')}`); } async tap(selector: string) { const page = this.getMini().currentPage(); const element = await page.$(selector); if (!element) throw new Error(`元素未找到: ${selector}`); await element.tap(); } async inputText(selector: string, value: string) { const page = this.getMini().currentPage(); const element = await page.$(selector); if (!element) throw new Error(`元素未找到: ${selector}`); await element.setValue(value); } async getElement(selector: string) { const page = this.getMini().currentPage(); return page.$(selector); } async getElements(selector: string) { const page = this.getMini().currentPage(); return page.$$(selector); } async waitForElement(selector: string, timeout = 5000): Promise { const start = Date.now(); while (Date.now() - start < timeout) { const el = await this.getElement(selector); if (el) return el; await new Promise((r) => setTimeout(r, 200)); } throw new Error(`等待元素超时: ${selector} (${timeout}ms)`); } async getPageData(path?: string) { const page = this.getMini().currentPage(); return page.data(path); } async screenshot(path?: string): Promise { const page = this.getMini().currentPage(); return page.screenshot({ path }); } async callMethod(selector: string, method: string, ...args: unknown[]) { const page = this.getMini().currentPage(); const element = await page.$(selector); if (!element) throw new Error(`元素未找到: ${selector}`); return element.callMethod(method, ...args); } }