🔥 Midscene 重磅更新:支持 AI 驱动的 Android 自动化

原文: midscenejs.com/zh/blog-sup...

我们很高兴地宣布:从 Midscene v0.15 开始,我们开始支持 Android 自动化。AI 驱动的 Android 自动化时代已经到来!

案例展示

地图导航到景点

打开地图 App,搜索目的地,然后发起导航。

Twitter 自动点赞

打开 Twitter,自动点赞 @midscene_ai 的第一个推文。

适配所有的应用

对于我们的开发者来说,你只需要 adb 和一个视觉语言(visual language, VL)模型服务。所有的准备工作就做好了!

在运行期,我们利用 VL 模型的视觉定位能力来定位屏幕上的目标元素。因此,无论是原生 App ,Lynx 页面还是 Hybrid App 中的 webview,它都无关紧要。开发者可以编写自动化脚本,而无需担心 App 本身的技术栈。

引入 Midscene 的全部特性

当使用 Midscene 进行 Web 自动化时,我们的用户喜欢使用我们的 Playground 和运行报告能力。现在,我们已经将这些特性引入到 Android 自动化中!

使用 Playground 来试用 Android 自动化,而不需要写任何代码

使用报告重放整个过程

使用 YAML 文件编写自动化脚本

yaml 复制代码
# search headphone on ebay, extract the items info into a json file, and assert the shopping cart icon

android:
  deviceId: s4ey59

tasks:
  - name: search headphones
    flow:
      - aiAction: open browser and navigate to ebay.com
      - aiAction: type 'Headphones' in ebay search box, hit Enter
      - sleep: 5000
      - aiAction: scroll down the page for 800px

  - name: extract headphones info
    flow:
      - aiQuery: >
          {name: string, price: number, subTitle: string}[], return item name, price and the subTitle on the lower right corner of each item
        name: headphones

  - name: assert Filter button
    flow:
      - aiAssert: There is a Filter button on the page

使用 JavaScript SDK 来编写自动化脚本

ts 复制代码
import { AndroidAgent, AndroidDevice, getConnectedDevices } from '@midscene/android';
import "dotenv/config"; // read environment variables from .env file

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
Promise.resolve(
  (async () => {
    const devices = await getConnectedDevices();
    const page = new AndroidDevice(devices[0].udid);

    // 👀 init Midscene agent
    const agent = new AndroidAgent(page,{
      aiActionContext:
        'If any location, permission, user agreement, etc. popup, click agree. If login page pops up, close it.',
    });
    await page.connect();
    await page.launch('https://www.ebay.com');

    await sleep(5000);

    // 👀 type keywords, perform a search
    await agent.aiAction('type "Headphones" in search box, hit Enter');

    // 👀 wait for the loading
    await agent.aiWaitFor("there is at least one headphone item on page");
    // or you may use a plain sleep:
    // await sleep(5000);

    // 👀 understand the page content, find the items
    const items = await agent.aiQuery(
      "{itemTitle: string, price: Number}[], find item in list and corresponding price"
    );
    console.log("headphones in stock", items);

    // 👀 assert by AI
    await agent.aiAssert("There is a category filter on the left");
  })()
);

使用两种风格的 API 来执行交互

自动规划(Auto-planning)风格:

javascript 复制代码
await agent.ai('input "Headphones" in search box, hit Enter');

即时操作(Instant Actions)风格:

javascript 复制代码
await agent.aiInput('Headphones', 'search box');
await agent.aiKeyboardPress('Enter');

示例项目

我们为 JavaScript SDK 准备了一个示例项目:

JavaScript 示例项目

如果你想要使用自动化进行测试,你可以使用 JavaScript 和 vitest。我们为你准备了一个示例项目,来看看它是如何工作的:

Vitest demo project

此外,你也可以使用 yaml 文件来编写自动化脚本:

YAML 示例项目

限制

  1. 无法使用元素定位的缓存功能。由于没有收集视图树,我们无法缓存元素标识符并重用它。
  2. 目前只支持一些已知的 VL 模型。如果你想要引入其他 VL 模型,请让我们知道。
  3. 运行性能还不够好。我们还在努力改进它。
  4. VL 模型在 .aiQuery.aiAssert 中表现不佳。我们将在未来提供一种方法来切换模型以适应不同的任务。
  5. 由于某些安全限制,你可能会在密码输入时得到一个空白截图,Midscene 此时将无法工作。

致谢

我们想要感谢以下项目:

  • scrcpyyume-chan 允许我们使用浏览器控制 Android 设备。
  • appium-adb 为 adb 提供了 javascript 桥接。
  • YADB 为文本输入提供了性能提升。
相关推荐
web守墓人2 小时前
【gpt生成-其一】以go语言为例,详细描述一下 :语法规范BNF/EBNF形式化描述
前端·gpt·golang
_一条咸鱼_4 小时前
Android Picasso 请求构建模块深度剖析(一)
android·面试·android jetpack
_一条咸鱼_4 小时前
Android Picasso 显示模块深度剖析(六)
android·面试·android jetpack
pink大呲花4 小时前
使用 Axios 进行 API 请求与接口封装:打造高效稳定的前端数据交互
前端·vue.js·交互
氦客4 小时前
kotlin知识体系(六) : Flow核心概念与与操作符指南
android·开发语言·kotlin·协程·flow·冷流·热流
samuel9184 小时前
uniapp通过uni.addInterceptor实现路由拦截
前端·javascript·uni-app
泯泷5 小时前
JavaScript随机数生成技术实践 | 为什么Math.random不是安全的随机算法?
前端·javascript·安全
benben0445 小时前
Unity3D仿星露谷物语开发35之锄地动画
前端·游戏·游戏引擎
八了个戒5 小时前
「数据可视化 D3系列」入门第八章:动画效果详解(让图表动起来)
开发语言·前端·javascript·数据可视化