{"title":"iOS 应用自动化测试","slug":"automated-testing-ios","category":"guides","summary":"使用 Synapse + Computer Control API 通过 Simulator 自动化 iOS 应用测试。","audience":["human","llm"],"tags":["guide","ios","testing","automation"],"difficulty":"advanced","updated":"2026-06-27","word_count":189,"read_minutes":1,"lang":"zh","translated":true,"requested_lang":"zh","content_markdown":"\n# iOS 应用自动化测试\n\n将 Synapse 的记忆系统与 Computer Control API 结合，构建 LLM 驱动的 iOS 应用测试。LLM 会记住测试场景，从过往失败中学习，并适应 UI 变化。\n\n## 架构\n\n```\n┌──────────────┐    命令     ┌──────────────┐    截图    ┌──────────────┐\n│  LLM Agent   │ ─────────────▶│  Synapse     │ ────────────────▶ │  iOS Sim     │\n│  (Claude)    │               │  Computer    │ ◀──────────────── │  (via agent) │\n└──────────────┘               │  Control     │    结果           └──────────────┘\n       │                       └──────────────┘\n       │ store/recall\n       ▼\n┌──────────────┐\n│  记忆        │ (测试场景、过往失败、UI 模式)\n└──────────────┘\n```\n\n## 前置条件\n\n- Synapse 账户 + Mind Key\n- 在 Claude Desktop 中配置 Synapse MCP Server\n- 已安装 `screen-remote-agent` 的 iOS Simulator\n- 在 Synapse 中注册计算机（参见 [Computer Control API](/docs/api/computers)）\n\n## 第 1 步：注册 Simulator 计算机\n\n在运行 iOS Simulator 的 Mac 上：\n\n```bash\n# 从 Synapse 获取安装码\ncurl -X POST https://synapse.schaefer.zone/computers/install-code \\\n  -H \"Authorization: Bearer YOUR_MIND_KEY\" \\\n  -d '{\"computer_name\":\"ios-sim\"}'\n# → { \"install_code\": \"ic_...\" }\n\n# 在 Mac 上运行 screen-remote-agent\n# (使用安装码完成注册)\n```\n\n## 第 2 步：在记忆中存储测试场景\n\n把可复用的测试场景作为记忆存储：\n\n```python\nimport requests\n\ndef store_test_scenario(name, steps, app):\n    requests.post(f\"{URL}/memory\",\n        headers={\"Authorization\": f\"Bearer {MIND_KEY}\"},\n        json={\n            \"category\": \"skill\",\n            \"key\": f\"test_scenario_{name}\",\n            \"content\": f\"App: {app}\\nSteps:\\n\" + \"\\n\".join(steps),\n            \"tags\": [\"test\", \"ios\", app],\n            \"priority\": \"high\"\n        })\n\nstore_test_scenario(\"login_flow\", [\n    \"Launch app\",\n    \"Tap email field\",\n    \"Type test@example.com\",\n    \"Tap password field\",\n    \"Type password123\",\n    \"Tap Login button\",\n    \"Verify home screen appears\"\n], \"MyApp\")\n```\n\n## 第 3 步：LLM 驱动的测试执行\n\n在 Claude Desktop 中（已配置 Synapse MCP）：\n\n```\nRun the login_flow test scenario on the iOS Simulator.\nTake a screenshot after each step and verify the expected UI.\nIf any step fails, store the failure as a memory so we can\navoid it next time.\n```\n\nClaude 会：\n\n1. 调用 `memory_search` 查找 `test_scenario_login_flow` 记忆\n2. 调用 `computer_screenshot` 查看当前状态\n3. 通过 `computer_command_queue` 执行每一步（点击、输入）\n4. 通过截图验证结果\n5. 把任何失败存储为 `mistake` 记忆\n\n## 第 4 步：自愈测试\n\n当测试失败时，存储失败信息与恢复方案：\n\n```python\ndef store_test_failure(scenario, step, error, recovery):\n    requests.post(f\"{URL}/memory\",\n        headers={\"Authorization\": f\"Bearer {MIND_KEY}\"},\n        json={\n            \"category\": \"mistake\",\n            \"key\": f\"failure_{scenario}_{step}\",\n            \"content\": f\"Scenario: {scenario}\\nStep: {step}\\nError: {error}\\nRecovery: {recovery}\",\n            \"tags\": [\"test\", \"failure\", \"ios\", scenario],\n            \"priority\": \"high\"\n        })\n\n# 示例\nstore_test_failure(\"login_flow\", \"tap_login\",\n    \"Login button not found at expected coordinates\",\n    \"Button moved due to new logo. Search by accessibility label instead.\")\n```\n\n下次 LLM 运行该测试时，会回放该失败记忆并自动应用恢复方案。\n\n## 第 5 步：测试结果跟踪\n\n把测试运行记录为任务：\n\n```python\ndef track_test_run(scenario, status, duration):\n    requests.post(f\"{URL}/mind/task\",\n        headers={\"Authorization\": f\"Bearer {MIND_KEY}\",\n                 \"Content-Type\": \"application/json\"},\n        json={\n            \"title\": f\"Test: {scenario}\",\n            \"description\": f\"Status: {status}, Duration: {duration}s\",\n            \"priority\": \"normal\"\n        })\n```\n\n## 常用命令\n\n| 操作 | 命令 |\n|--------|---------|\n| 启动 Simulator | `xcrun simctl launch booted com.example.app` |\n| 截屏 | `computer_screenshot`（通过 Synapse MCP） |\n| 在 (x,y) 点击 | `computer_command_queue {type:\"click\", payload:{x,y}}` |\n| 输入文本 | `computer_command_queue {type:\"type\", payload:{text:\"...\"}}` |\n| 按 Home 键 | `computer_command_queue {type:\"key\", payload:{keys:[\"Cmd\",\"Shift\",\"H\"]}}` |\n\n## 最佳实践\n\n> [!TIP]\n> - **把 UI 坐标作为记忆存储** — UI 会变，但 LLM 可以重新学习\n> - **使用 accessibility 标签** — 比坐标更稳定\n> - **把测试数据单独存储** — 用变量管理用户名、密码\n> - **在干净状态运行测试** — 每次运行之间重置 Simulator\n> - **为失败保存截图** — 便于调试\n\n## 下一步\n\n- [自愈测试](/docs/guides/self-healing-tests)\n- [Computer Control API](/docs/api/computers)\n- [记忆最佳实践](/docs/guides/memory-best-practices)\n","content_html":"<h1>iOS 应用自动化测试</h1>\n<p>将 Synapse 的记忆系统与 Computer Control API 结合，构建 LLM 驱动的 iOS 应用测试。LLM 会记住测试场景，从过往失败中学习，并适应 UI 变化。</p>\n<h2>架构</h2>\n<pre><code class=\"hljs language-plaintext\">┌──────────────┐    命令     ┌──────────────┐    截图    ┌──────────────┐\n│  LLM Agent   │ ─────────────▶│  Synapse     │ ────────────────▶ │  iOS Sim     │\n│  (Claude)    │               │  Computer    │ ◀──────────────── │  (via agent) │\n└──────────────┘               │  Control     │    结果           └──────────────┘\n       │                       └──────────────┘\n       │ store/recall\n       ▼\n┌──────────────┐\n│  记忆        │ (测试场景、过往失败、UI 模式)\n└──────────────┘</code></pre><h2>前置条件</h2>\n<ul>\n<li>Synapse 账户 + Mind Key</li>\n<li>在 Claude Desktop 中配置 Synapse MCP Server</li>\n<li>已安装 <code>screen-remote-agent</code> 的 iOS Simulator</li>\n<li>在 Synapse 中注册计算机（参见 <a href=\"/docs/api/computers\">Computer Control API</a>）</li>\n</ul>\n<h2>第 1 步：注册 Simulator 计算机</h2>\n<p>在运行 iOS Simulator 的 Mac 上：</p>\n<pre><code class=\"hljs language-bash\"><span class=\"hljs-comment\"># 从 Synapse 获取安装码</span>\ncurl -X POST https://synapse.schaefer.zone/computers/install-code \\\n  -H <span class=\"hljs-string\">&quot;Authorization: Bearer YOUR_MIND_KEY&quot;</span> \\\n  -d <span class=\"hljs-string\">&#x27;{&quot;computer_name&quot;:&quot;ios-sim&quot;}&#x27;</span>\n<span class=\"hljs-comment\"># → { &quot;install_code&quot;: &quot;ic_...&quot; }</span>\n\n<span class=\"hljs-comment\"># 在 Mac 上运行 screen-remote-agent</span>\n<span class=\"hljs-comment\"># (使用安装码完成注册)</span></code></pre><h2>第 2 步：在记忆中存储测试场景</h2>\n<p>把可复用的测试场景作为记忆存储：</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import</span> requests\n\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">store_test_scenario</span>(<span class=\"hljs-params\">name, steps, app</span>):\n    requests.post(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/memory&quot;</span>,\n        headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{MIND_KEY}</span>&quot;</span>},\n        json={\n            <span class=\"hljs-string\">&quot;category&quot;</span>: <span class=\"hljs-string\">&quot;skill&quot;</span>,\n            <span class=\"hljs-string\">&quot;key&quot;</span>: <span class=\"hljs-string\">f&quot;test_scenario_<span class=\"hljs-subst\">{name}</span>&quot;</span>,\n            <span class=\"hljs-string\">&quot;content&quot;</span>: <span class=\"hljs-string\">f&quot;App: <span class=\"hljs-subst\">{app}</span>\\nSteps:\\n&quot;</span> + <span class=\"hljs-string\">&quot;\\n&quot;</span>.join(steps),\n            <span class=\"hljs-string\">&quot;tags&quot;</span>: [<span class=\"hljs-string\">&quot;test&quot;</span>, <span class=\"hljs-string\">&quot;ios&quot;</span>, app],\n            <span class=\"hljs-string\">&quot;priority&quot;</span>: <span class=\"hljs-string\">&quot;high&quot;</span>\n        })\n\nstore_test_scenario(<span class=\"hljs-string\">&quot;login_flow&quot;</span>, [\n    <span class=\"hljs-string\">&quot;Launch app&quot;</span>,\n    <span class=\"hljs-string\">&quot;Tap email field&quot;</span>,\n    <span class=\"hljs-string\">&quot;Type test@example.com&quot;</span>,\n    <span class=\"hljs-string\">&quot;Tap password field&quot;</span>,\n    <span class=\"hljs-string\">&quot;Type password123&quot;</span>,\n    <span class=\"hljs-string\">&quot;Tap Login button&quot;</span>,\n    <span class=\"hljs-string\">&quot;Verify home screen appears&quot;</span>\n], <span class=\"hljs-string\">&quot;MyApp&quot;</span>)</code></pre><h2>第 3 步：LLM 驱动的测试执行</h2>\n<p>在 Claude Desktop 中（已配置 Synapse MCP）：</p>\n<pre><code class=\"hljs language-plaintext\">Run the login_flow test scenario on the iOS Simulator.\nTake a screenshot after each step and verify the expected UI.\nIf any step fails, store the failure as a memory so we can\navoid it next time.</code></pre><p>Claude 会：</p>\n<ol>\n<li>调用 <code>memory_search</code> 查找 <code>test_scenario_login_flow</code> 记忆</li>\n<li>调用 <code>computer_screenshot</code> 查看当前状态</li>\n<li>通过 <code>computer_command_queue</code> 执行每一步（点击、输入）</li>\n<li>通过截图验证结果</li>\n<li>把任何失败存储为 <code>mistake</code> 记忆</li>\n</ol>\n<h2>第 4 步：自愈测试</h2>\n<p>当测试失败时，存储失败信息与恢复方案：</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">store_test_failure</span>(<span class=\"hljs-params\">scenario, step, error, recovery</span>):\n    requests.post(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/memory&quot;</span>,\n        headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{MIND_KEY}</span>&quot;</span>},\n        json={\n            <span class=\"hljs-string\">&quot;category&quot;</span>: <span class=\"hljs-string\">&quot;mistake&quot;</span>,\n            <span class=\"hljs-string\">&quot;key&quot;</span>: <span class=\"hljs-string\">f&quot;failure_<span class=\"hljs-subst\">{scenario}</span>_<span class=\"hljs-subst\">{step}</span>&quot;</span>,\n            <span class=\"hljs-string\">&quot;content&quot;</span>: <span class=\"hljs-string\">f&quot;Scenario: <span class=\"hljs-subst\">{scenario}</span>\\nStep: <span class=\"hljs-subst\">{step}</span>\\nError: <span class=\"hljs-subst\">{error}</span>\\nRecovery: <span class=\"hljs-subst\">{recovery}</span>&quot;</span>,\n            <span class=\"hljs-string\">&quot;tags&quot;</span>: [<span class=\"hljs-string\">&quot;test&quot;</span>, <span class=\"hljs-string\">&quot;failure&quot;</span>, <span class=\"hljs-string\">&quot;ios&quot;</span>, scenario],\n            <span class=\"hljs-string\">&quot;priority&quot;</span>: <span class=\"hljs-string\">&quot;high&quot;</span>\n        })\n\n<span class=\"hljs-comment\"># 示例</span>\nstore_test_failure(<span class=\"hljs-string\">&quot;login_flow&quot;</span>, <span class=\"hljs-string\">&quot;tap_login&quot;</span>,\n    <span class=\"hljs-string\">&quot;Login button not found at expected coordinates&quot;</span>,\n    <span class=\"hljs-string\">&quot;Button moved due to new logo. Search by accessibility label instead.&quot;</span>)</code></pre><p>下次 LLM 运行该测试时，会回放该失败记忆并自动应用恢复方案。</p>\n<h2>第 5 步：测试结果跟踪</h2>\n<p>把测试运行记录为任务：</p>\n<pre><code class=\"hljs language-python\"><span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">track_test_run</span>(<span class=\"hljs-params\">scenario, status, duration</span>):\n    requests.post(<span class=\"hljs-string\">f&quot;<span class=\"hljs-subst\">{URL}</span>/mind/task&quot;</span>,\n        headers={<span class=\"hljs-string\">&quot;Authorization&quot;</span>: <span class=\"hljs-string\">f&quot;Bearer <span class=\"hljs-subst\">{MIND_KEY}</span>&quot;</span>,\n                 <span class=\"hljs-string\">&quot;Content-Type&quot;</span>: <span class=\"hljs-string\">&quot;application/json&quot;</span>},\n        json={\n            <span class=\"hljs-string\">&quot;title&quot;</span>: <span class=\"hljs-string\">f&quot;Test: <span class=\"hljs-subst\">{scenario}</span>&quot;</span>,\n            <span class=\"hljs-string\">&quot;description&quot;</span>: <span class=\"hljs-string\">f&quot;Status: <span class=\"hljs-subst\">{status}</span>, Duration: <span class=\"hljs-subst\">{duration}</span>s&quot;</span>,\n            <span class=\"hljs-string\">&quot;priority&quot;</span>: <span class=\"hljs-string\">&quot;normal&quot;</span>\n        })</code></pre><h2>常用命令</h2>\n<table>\n<thead>\n<tr>\n<th>操作</th>\n<th>命令</th>\n</tr>\n</thead>\n<tbody><tr>\n<td>启动 Simulator</td>\n<td><code>xcrun simctl launch booted com.example.app</code></td>\n</tr>\n<tr>\n<td>截屏</td>\n<td><code>computer_screenshot</code>（通过 Synapse MCP）</td>\n</tr>\n<tr>\n<td>在 (x,y) 点击</td>\n<td><code>computer_command_queue {type:&quot;click&quot;, payload:{x,y}}</code></td>\n</tr>\n<tr>\n<td>输入文本</td>\n<td><code>computer_command_queue {type:&quot;type&quot;, payload:{text:&quot;...&quot;}}</code></td>\n</tr>\n<tr>\n<td>按 Home 键</td>\n<td><code>computer_command_queue {type:&quot;key&quot;, payload:{keys:[&quot;Cmd&quot;,&quot;Shift&quot;,&quot;H&quot;]}}</code></td>\n</tr>\n</tbody></table>\n<h2>最佳实践</h2>\n<div class=\"callout callout-ok\"></div><h2>下一步</h2>\n<ul>\n<li><a href=\"/docs/guides/self-healing-tests\">自愈测试</a></li>\n<li><a href=\"/docs/api/computers\">Computer Control API</a></li>\n<li><a href=\"/docs/guides/memory-best-practices\">记忆最佳实践</a></li>\n</ul>\n","urls":{"html":"/docs/guides/automated-testing-ios","text":"/docs/guides/automated-testing-ios?format=text","json":"/docs/guides/automated-testing-ios?format=json","llm":"/docs/guides/automated-testing-ios?format=llm"},"translations_available":["en","zh","hi","es","fr","ar","pt","ru","ja","de","it","ko","nl","pl","tr","sv","vi","th","id","uk"]}