import { agentFromWebDriverAgent } from '@midscene/ios'; import * as dotenv from 'dotenv'; import * as path from 'path'; dotenv.config({ path: path.resolve(__dirname, '../.env') }); async function main() { console.log('=== Midscene.js iOS 连接测试 ==='); console.log(`Model: ${process.env.MIDSCENE_MODEL_NAME}`); console.log(`Base URL: ${process.env.MIDSCENE_MODEL_BASE_URL}`); console.log(`WDA: ${process.env.WDA_HOST}:${process.env.WDA_PORT}`); console.log(''); try { console.log('1. 连接 WebDriverAgent...'); const agent = await agentFromWebDriverAgent({ wdaHost: process.env.WDA_HOST || 'localhost', wdaPort: Number(process.env.WDA_PORT) || 8100, }); console.log(' ✓ WDA 连接成功'); console.log('2. 启动 SwitchBot APP...'); await agent.launch('com.wohand.wohand'); console.log(' ✓ APP 已启动'); console.log('3. 等待页面加载...'); await new Promise((r) => setTimeout(r, 3000)); console.log('4. AI 截图分析当前页面...'); const description = await agent.aiQuery( '描述当前屏幕上显示的内容,用一句话概括' ); console.log(` ✓ AI 识别结果: ${description}`); console.log('5. 查找 Bot 设备...'); const hasBotDevice = await agent.aiBoolean( '当前页面是否有Bot设备卡片显示' ); console.log(` ✓ Bot 设备存在: ${hasBotDevice}`); if (hasBotDevice) { const botName = await agent.aiQuery( '页面上Bot设备卡片显示的设备名称是什么?只返回名称文本' ); console.log(` ✓ Bot 设备名: ${botName}`); } console.log('\n=== 测试通过! ==='); await agent.destroy(); } catch (error: any) { console.error('\n✗ 测试失败:', error.message); process.exit(1); } } main();