支持 iOS 自动化

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

案例展示

Twitter 自动点赞

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

适配所有的应用

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

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

引入 Midscene 的全部特性

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

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

使用报告重放整个过程

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

# 在 iOS 设备上打开 Safari 浏览器,搜索内容并提取信息

ios:
  deviceId: "iPhone"
  bundleId: "com.apple.mobilesafari"

tasks:
  - name: 搜索内容
    flow:
      - aiAction: 点击地址栏
      - aiAction: 输入 'Midscene AI automation'
      - aiAction: 点击搜索按钮
      - sleep: 3000
      - aiAction: 向下滚动 500px

  - name: 提取搜索结果
    flow:
      - aiQuery: >
          {title: string, url: string, description: string}[],
          返回搜索结果的标题、链接和描述
        name: searchResults

  - name: 验证页面元素
    flow:
      - aiAssert: 页面上有搜索结果列表

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

import { IOSAgent, IOSDevice } from '@midscene/ios';
import "dotenv/config"; // 从 .env 文件读取环境变量

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
Promise.resolve(
  (async () => {
    // 👀 初始化 iOS 设备
    const device = new IOSDevice({
      deviceId: 'iPhone',
      bundleId: 'com.apple.mobilesafari'
    });

    // 👀 初始化 Midscene agent
    const agent = new IOSAgent(device, {
      aiActionContext:
        '如果出现任何权限弹窗,点击允许。如果出现登录页面,跳过它。',
    });

    await device.connect();
    await device.launchApp();

    await sleep(3000);

    // 👀 点击地址栏并输入搜索关键词
    await agent.aiAction('点击地址栏并输入 "Midscene automation"');

    // 👀 执行搜索
    await agent.aiAction('点击搜索按钮');

    // 👀 等待加载完成
    await agent.aiWaitFor("页面上至少有一个搜索结果");
    // 或者你可以使用简单的等待:
    // await sleep(5000);

    // 👀 理解页面内容,找到搜索结果
    const results = await agent.aiQuery(
      "{title: string, url: string}[], 找到搜索结果列表中的标题和链接"
    );
    console.log("搜索结果", results);

    // 👀 使用 AI 进行断言
    await agent.aiAssert("页面上显示了相关的搜索结果");
  })()
);

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

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

await agent.ai('点击地址栏并输入 "Midscene automation",然后搜索');

即时操作(Instant Actions)风格:

await agent.aiTap('地址栏');
await agent.aiInput('Midscene automation', '地址栏');
await agent.aiTap('搜索按钮');

快速开始

你可以使用 Playground 工具来零代码快速体验 iOS 自动化的过程,详情参阅 使用 iOS Playground 快速体验

在体验完成后,你可以通过 Javascript 代码与 iOS 设备进行集成,请参阅 与 iOS 设备集成

如果你更偏爱 Yaml 文件形式的自动化脚本,请参阅 使用 YAML 格式的自动化脚本