import { createRequire } from 'module'; const require = createRequire(import.meta.url); const Launcher = require('miniprogram-automator/out/launcher').default; const WS = 'ws://127.0.0.1:9420'; const TIMEOUT = 8000; function withTimeout(promise, ms) { return Promise.race([ promise, new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), ms)) ]); } async function main() { console.log('Connecting to', WS); const launcher = new Launcher(); const mp = await withTimeout(launcher.connect({ wsEndpoint: WS }), TIMEOUT); console.log('Connected!'); // 1. Current page try { const page = await withTimeout(mp.currentPage(), TIMEOUT); console.log('[OK] Current page:', page.path); } catch (e) { console.log('[WARN] currentPage failed:', e.message); } // 2. Screenshot try { const screenshot = await withTimeout(mp.screenshot(), TIMEOUT); const fs = await import('fs'); const outPath = 'g:/hms/apps/miniprogram/verify-screenshot.png'; // screenshot could be buffer or base64 if (Buffer.isBuffer(screenshot)) { fs.writeFileSync(outPath, screenshot); } else { fs.writeFileSync(outPath, Buffer.from(screenshot, 'base64')); } console.log('[OK] Screenshot saved to verify-screenshot.png'); } catch (e) { console.log('[WARN] screenshot failed:', e.message); } // 3. Navigate and verify pages const pages = [ { path: 'pages/index/index', name: '首页' }, { path: 'pages/health/index', name: '健康中心' }, { path: 'pages/appointment/index', name: '预约列表' }, { path: 'pages/article/index', name: '资讯文章' }, { path: 'pages/profile/index', name: '个人中心' }, { path: 'pages/login/index', name: '登录页' }, ]; for (const p of pages) { try { await withTimeout(mp.reLaunch(`/pages/${p.path.includes('/') ? '' : ''}${p.path}`), TIMEOUT); await new Promise(r => setTimeout(r, 2000)); const page = await withTimeout(mp.currentPage(), TIMEOUT); console.log(`[OK] ${p.name} (${p.path}): loaded`); } catch (e) { console.log(`[FAIL] ${p.name} (${p.path}): ${e.message}`); } } // 4. System info try { const systemInfo = await withTimeout(mp.evaluate(() => { try { return wx.getSystemInfoSync(); } catch { return null; } }), TIMEOUT); if (systemInfo) { console.log('[OK] System info:', JSON.stringify({ model: systemInfo.model, system: systemInfo.system, SDKVersion: systemInfo.SDKVersion, })); } } catch (e) { console.log('[WARN] systemInfo failed:', e.message); } await mp.close(); console.log('\nVerification complete!'); } main().catch(err => { console.error('Fatal:', err.message); process.exit(1); });