# iOS 应用自动化测试 将 Synapse 的记忆系统与 Computer Control API 结合,构建 LLM 驱动的 iOS 应用测试。LLM 会记住测试场景,从过往失败中学习,并适应 UI 变化。 ## 架构 ``` ┌──────────────┐ 命令 ┌──────────────┐ 截图 ┌──────────────┐ │ LLM Agent │ ─────────────▶│ Synapse │ ────────────────▶ │ iOS Sim │ │ (Claude) │ │ Computer │ ◀──────────────── │ (via agent) │ └──────────────┘ │ Control │ 结果 └──────────────┘ │ └──────────────┘ │ store/recall ▼ ┌──────────────┐ │ 记忆 │ (测试场景、过往失败、UI 模式) └──────────────┘ ``` ## 前置条件 - Synapse 账户 + Mind Key - 在 Claude Desktop 中配置 Synapse MCP Server - 已安装 `screen-remote-agent` 的 iOS Simulator - 在 Synapse 中注册计算机(参见 [Computer Control API](/docs/api/computers)) ## 第 1 步:注册 Simulator 计算机 在运行 iOS Simulator 的 Mac 上: ```bash # 从 Synapse 获取安装码 curl -X POST https://synapse.schaefer.zone/computers/install-code \ -H "Authorization: Bearer YOUR_MIND_KEY" \ -d '{"computer_name":"ios-sim"}' # → { "install_code": "ic_..." } # 在 Mac 上运行 screen-remote-agent # (使用安装码完成注册) ``` ## 第 2 步:在记忆中存储测试场景 把可复用的测试场景作为记忆存储: ```python import requests def store_test_scenario(name, steps, app): requests.post(f"{URL}/memory", headers={"Authorization": f"Bearer {MIND_KEY}"}, json={ "category": "skill", "key": f"test_scenario_{name}", "content": f"App: {app}\nSteps:\n" + "\n".join(steps), "tags": ["test", "ios", app], "priority": "high" }) store_test_scenario("login_flow", [ "Launch app", "Tap email field", "Type test@example.com", "Tap password field", "Type password123", "Tap Login button", "Verify home screen appears" ], "MyApp") ``` ## 第 3 步:LLM 驱动的测试执行 在 Claude Desktop 中(已配置 Synapse MCP): ``` Run the login_flow test scenario on the iOS Simulator. Take a screenshot after each step and verify the expected UI. If any step fails, store the failure as a memory so we can avoid it next time. ``` Claude 会: 1. 调用 `memory_search` 查找 `test_scenario_login_flow` 记忆 2. 调用 `computer_screenshot` 查看当前状态 3. 通过 `computer_command_queue` 执行每一步(点击、输入) 4. 通过截图验证结果 5. 把任何失败存储为 `mistake` 记忆 ## 第 4 步:自愈测试 当测试失败时,存储失败信息与恢复方案: ```python def store_test_failure(scenario, step, error, recovery): requests.post(f"{URL}/memory", headers={"Authorization": f"Bearer {MIND_KEY}"}, json={ "category": "mistake", "key": f"failure_{scenario}_{step}", "content": f"Scenario: {scenario}\nStep: {step}\nError: {error}\nRecovery: {recovery}", "tags": ["test", "failure", "ios", scenario], "priority": "high" }) # 示例 store_test_failure("login_flow", "tap_login", "Login button not found at expected coordinates", "Button moved due to new logo. Search by accessibility label instead.") ``` 下次 LLM 运行该测试时,会回放该失败记忆并自动应用恢复方案。 ## 第 5 步:测试结果跟踪 把测试运行记录为任务: ```python def track_test_run(scenario, status, duration): requests.post(f"{URL}/mind/task", headers={"Authorization": f"Bearer {MIND_KEY}", "Content-Type": "application/json"}, json={ "title": f"Test: {scenario}", "description": f"Status: {status}, Duration: {duration}s", "priority": "normal" }) ``` ## 常用命令 | 操作 | 命令 | |--------|---------| | 启动 Simulator | `xcrun simctl launch booted com.example.app` | | 截屏 | `computer_screenshot`(通过 Synapse MCP) | | 在 (x,y) 点击 | `computer_command_queue {type:"click", payload:{x,y}}` | | 输入文本 | `computer_command_queue {type:"type", payload:{text:"..."}}` | | 按 Home 键 | `computer_command_queue {type:"key", payload:{keys:["Cmd","Shift","H"]}}` | ## 最佳实践 > [!TIP] > - **把 UI 坐标作为记忆存储** — UI 会变,但 LLM 可以重新学习 > - **使用 accessibility 标签** — 比坐标更稳定 > - **把测试数据单独存储** — 用变量管理用户名、密码 > - **在干净状态运行测试** — 每次运行之间重置 Simulator > - **为失败保存截图** — 便于调试 ## 下一步 - [自愈测试](/docs/guides/self-healing-tests) - [Computer Control API](/docs/api/computers) - [记忆最佳实践](/docs/guides/memory-best-practices)