import { AndroidAgent, AndroidDevice, getConnectedDevices } from '@midscene/android'; import * as dotenv from 'dotenv'; import { expect } from 'vitest'; import { execSync } from 'child_process'; // 加载环境变量 dotenv.config(); // 设置环境变量 process.env.OPENAI_API_KEY = "fd86bcee-d77a-4299-b4c5-6f2c5c204d9c"; process.env.MIDSCENE_MODEL_NAME = "doubao-1.5-ui-tars-250328"; process.env.MIDSCENE_USE_VLM_UI_TARS = "DOUBAO"; process.env.OPENAI_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3"; const sleep = (ms: number): Promise => new Promise((r) => setTimeout(r, ms)); async function main() { console.log('Starting test...'); try { console.log('Getting connected devices...'); const devices = await getConnectedDevices(); console.log('Found devices:', devices); const page = new AndroidDevice(devices[0].udid); console.log('Created AndroidDevice instance'); // 👀 初始化 Midscene agent console.log('Initializing AndroidAgent...'); const agent = new AndroidAgent(page, { aiActionContext: '如果出现位置、权限、用户协议等弹窗,点击同意。如果出现登录页面,关闭它。' }); console.log('AndroidAgent initialized'); console.log('Connecting to device...'); await page.connect(); console.log('Connected to device'); // 强制设置横屏 console.log('Setting landscape orientation...'); try { execSync(`adb -s ${devices[0].udid} shell settings put system accelerometer_rotation 0`); execSync(`adb -s ${devices[0].udid} shell settings put system user_rotation 1`); await sleep(2000); // 检查屏幕方向 const orientation = execSync(`adb -s ${devices[0].udid} shell dumpsys input | grep 'SurfaceOrientation'`).toString(); console.log('Current orientation:', orientation); if (!orientation.includes('SurfaceOrientation: 1')) { console.log('Retrying to set landscape orientation...'); execSync(`adb -s ${devices[0].udid} shell settings put system user_rotation 1`); await sleep(3000); } } catch (error) { console.error('Error setting orientation:', error); } // 先强制停止应用,清除缓存 console.log('Force stopping app...'); try { // 使用adb命令停止应用 console.log('Stopping app...'); execSync(`adb -s ${devices[0].udid} shell am force-stop cn.huohua.edustudent`); await sleep(2000); // 使用adb命令清除应用数据 console.log('Clearing app data...'); execSync(`adb -s ${devices[0].udid} shell pm clear cn.huohua.edustudent`); await sleep(2000); console.log('App stopped and cleared'); } catch (error) { console.error('Error executing commands:', error); } // 启动应用到指定页面 console.log('Launching app...'); try { console.log('Attempting to launch app using aiAction...'); await agent.aiAction('启动火花思维学生端app'); console.log('aiAction completed successfully'); await sleep(5000); // 增加等待时间,确保应用完全启动 // 处理权限和协议弹窗 await agent.aiAction('我在一个横屏显示的用户协议弹窗中。这个弹窗有一个标题"用户注册协议与隐私政策",在弹窗底部有两个按钮。右边的按钮是"同意并使用",它是一个蓝色的TextView按钮,resource-id是"cn.huohua.edustudent:id/btn_yes"。请点击这个"同意并使用"按钮。注意:屏幕是横向旋转的。'); await sleep(5000); // 切换到密码登录 await agent.aiAction('在屏幕上查找"密码登录"标签或按钮,通常在登录界面顶部。点击它切换到密码登录模式。'); await sleep(2000); // 输入手机号 await agent.aiAction('在屏幕上查找手机号输入框,它通常显示"请输入手机号"的提示文字。点击输入框,然后输入: 18202810506'); await sleep(2000); // 输入密码 await agent.aiAction('在屏幕上查找密码输入框,它通常显示"请输入密码"的提示文字。点击输入框,然后输入: A123456'); await sleep(2000); // 勾选协议 await agent.aiAction('在屏幕底部查找用户协议勾选框,它通常是一个小方框,旁边有"我已阅读并同意"的文字。如果没有被勾选,点击它来勾选。'); await sleep(2000); // 点击登录 await agent.aiAction('在屏幕上查找"登录"或"立即登录"按钮,它通常是一个醒目的大按钮,位于输入框下方。点击这个按钮。'); await sleep(2000); } catch (error) { console.error('Error during app launch:', error); throw error; } console.log('App launched'); } catch (error) { console.error('Error occurred:', error); throw error; } } // 运行主函数 main().catch(console.error);