# simulator_batch

> 这个 MCP 工具在一次调用中，按顺序在团队的模拟器上执行一系列点按、等待、输入和其他步骤。

Source: https://air.build/cn/docs/mcp-tools/simulator_batch/

`simulator_batch` 在一次调用中，按顺序在团队正在运行的模拟器上执行 1 到 1000 个步骤。把它用于已知的流程，例如填写表单并按“下一步”。每个指定元素的步骤都会重新读取屏幕。整个列表会在第一步运行前检查，所以写错的步骤会让调用失败而不触碰设备。运行期间，批处理会在第一个失败的步骤处停止。已经完成的步骤保持完成状态。失败会在结果中报告（`ok: false`、`failedStep`），而不是作为工具错误。一个批处理中等待和暂停的总时长应远低于 30 秒；更长的流程请拆成多次调用，否则调用可能因连接断开而失败。

| 只读 | 破坏性 | 幂等 |
| --- | --- | --- |
| 否 | 否 | 否 |

## 步骤

每个步骤都是一个对象，包含 `action` 以及该操作的字段；任何其他字段都会被拒绝。`selector` 是一个对象，字段见 [选择器](https://air.build/cn/docs/mcp-tools/simulator_find/#selector)：`text`、`id`、`value`、`role`、`exact`、`strict`、`nodeId`。点使用 [屏幕坐标](https://air.build/cn/docs/mcp-tools/simulator_screen/#coordinates)。

| `action` | 字段 | 作用 |
| --- | --- | --- |
| `tap` | `selector`；`timeoutMs`（0 到 600000，默认 10000）；`duration`（0 到 60 秒，可选） | 等待元素出现并点按它，与使用选择器的 [simulator_tap](https://air.build/cn/docs/mcp-tools/simulator_tap/) 相同 |
| `find` | `selector` | 列出匹配项，与 [simulator_find](https://air.build/cn/docs/mcp-tools/simulator_find/) 相同，不等待 |
| `wait-visible` | `selector`；`timeoutMs`（0 到 600000，默认 10000） | 等待元素出现且可用 |
| `wait-hidden` | `selector`；`timeoutMs`（0 到 600000，默认 10000） | 等待直到没有可用的元素匹配 |
| `slider` | `selector`；`percent`（0 到 100） | 与 [simulator_slider](https://air.build/cn/docs/mcp-tools/simulator_slider/) 相同 |
| `adjust` | `selector`；`steps`（整数，不能为 0） | 与 [simulator_adjust](https://air.build/cn/docs/mcp-tools/simulator_adjust/) 相同；只完成部分调整时，该步骤失败 |
| `drag` | `start` 和 `end`（各含 `x`、`y`）；`duration`（0.05 到 60 秒）；`steps`（2 到 1000）。全部必填 | 与 [simulator_drag](https://air.build/cn/docs/mcp-tools/simulator_drag/) 相同 |
| `input` | `event` | 一个输入事件，与 [simulator_input](https://air.build/cn/docs/mcp-tools/simulator_input/) 中的相同 |
| `sleep` | `ms`（0 到 60000） | 暂停 |

## 输入

| 输入 | 类型 | 必填 | 默认值 | 作用 |
| --- | --- | --- | --- | --- |
| `teamId` | 字符串（UUID） | 是 | — | 团队，来自 [list_teams](https://air.build/cn/docs/mcp-tools/list_teams/) |
| `simulator` | string | 否 | 团队的默认设备 | 哪种设备，使用 [list_simulators](https://air.build/cn/docs/mcp-tools/list_simulators/#result) 中的 `id` |
| `steps` | 1 到 1000 个对象的列表 | 是 | — | [步骤](https://air.build/cn/docs/mcp-tools/simulator_batch/#steps)，按顺序排列 |

## 结果

| 字段 | 说明 |
| --- | --- |
| `ok` | 每一步都成功时为 `true` |
| `results` | 每个已运行的步骤一项，按顺序排列 |
| `results[].step` | 步骤的索引，从 0 开始 |
| `results[].ok` | 是否成功 |
| `results[].elapsedMs` | 耗时，以毫秒计 |
| `results[].result` | 步骤返回的内容：对于 `tap` 和 `wait-visible`，为元素、`point`、`exact`、`matchCount` 和 `excluded`，与 [simulator_tap](https://air.build/cn/docs/mcp-tools/simulator_tap/#result) 中相同；对于 `find`，为 `matches` 和 `excluded`；对于 `slider` 和 `adjust`，为它们对应工具的结果；其他情况为空 |
| `results[].error` | 步骤失败的原因（当它带着消息失败时） |
| `failedStep` | 失败步骤的索引；仅在 `ok` 为 `false` 时出现 |

```json
{
  "ok": false,
  "results": [
    { "step": 0, "ok": true, "elapsedMs": 412, "result": { "element": { "label": "Next", "identifier": "next", "role": "AXButton" }, "point": { "x": 201, "y": 780 }, "exact": true, "matchCount": 1, "excluded": [] } },
    { "step": 1, "ok": false, "elapsedMs": 10004, "error": "no usable element matching id=\"done\" within 10.0s" }
  ],
  "failedStep": 1
}
```

## 错误

以下错误会在任何步骤运行前以工具错误返回：

| 消息 | 何时出现 | 怎么办 |
| --- | --- | --- |
| steps is a list of 1 to 1000 `{"action": …}` objects. | 缺少 `steps`、它为空、过长，或某一项没有 `action` | 发送 1 到 1000 个步骤对象 |
| Failed to deserialize the JSON body into the target type: _…_ | 未知的 `action`、缺少或多出字段，或字段类型错误 | 修正消息中指出的步骤 |
| selector needs `nodeId`, `text`, `id`, `value`, or `role` | 某个步骤的 `selector` 没有指定任何内容 | 添加一个字段 |
| batch timeoutMs exceeds 600000 | 某个 `timeoutMs` 超过 600000 | 最多发送 600000 |
| batch sleep exceeds 60000 ms | 某个 `sleep` 超过 60000 | 最多发送 60000，或拆成多个暂停 |
| invalid tap duration | 某个 `tap` 的 `duration` 不在 0 到 60 之间 | 发送 0 到 60 |
| invalid slider percent | 某个 `slider` 的 `percent` 不在 0 到 100 之间 | 发送 0 到 100 |
| adjust steps must be nonzero | 某个 `adjust` 的 `steps: 0` | 发送一个非零的数 |
| drag requires finite coordinates, duration 0.05..=60 seconds and steps 2..=1000 | 某个 `drag` 超出范围 | 修正 `duration` 或 `steps` |

另外还有每个模拟器工具都可能返回的错误：见 [start_simulator](https://air.build/cn/docs/mcp-tools/start_simulator/#device-errors)。

## 示例

```json
{"name": "simulator_batch", "arguments": {"teamId": "00000000-0000-0000-0000-000000000000", "steps": [
  {"action": "tap", "selector": {"id": "title-field"}},
  {"action": "input", "event": {"type": "type", "text": "Groceries"}},
  {"action": "tap", "selector": {"text": "Save", "exact": true}},
  {"action": "wait-hidden", "selector": {"id": "title-field"}, "timeoutMs": 5000}
]}}
```
