# Kiểm thử ứng dụng iOS tự động Kết hợp hệ thống bộ nhớ của Synapse với Computer Control API để xây dựng kiểm thử ứng dụng iOS điều khiển bởi LLM. LLM ghi nhớ kịch bản kiểm thử, học từ lỗi quá khứ và thích ứng với thay đổi UI. ## Kiến trúc ``` ┌──────────────┐ commands ┌──────────────┐ screenshots ┌──────────────┐ │ LLM Agent │ ─────────────▶│ Synapse │ ────────────────▶ │ iOS Sim │ │ (Claude) │ │ Computer │ ◀──────────────── │ (via agent) │ └──────────────┘ │ Control │ results └──────────────┘ │ └──────────────┘ │ store/recall ▼ ┌──────────────┐ │ Memories │ (test scenarios, past failures, UI patterns) └──────────────┘ ``` ## Điều kiện tiên quyết - Tài khoản Synapse + Mind Key - Synapse MCP server được cấu hình trong Claude Desktop - iOS Simulator với `screen-remote-agent` đã cài đặt - Máy tính đã đăng ký trong Synapse (xem [Computer Control API](/docs/api/computers)) ## Bước 1: Đăng ký máy tính Simulator Trên Mac chạy iOS Simulator: ```bash # Get install code from 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_..." } # Run screen-remote-agent on the Mac # (uses the install code to register) ``` ## Bước 2: Lưu kịch bản kiểm thử trong bộ nhớ Lưu kịch bản kiểm thử tái sử dụng dưới dạng bộ nhớ: ```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") ``` ## Bước 3: Thực thi kiểm thử điều khiển bởi LLM Trong Claude Desktop (với Synapse MCP đã cấu hình): ``` 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 sẽ: 1. Gọi `memory_search` để tìm bộ nhớ `test_scenario_login_flow` 2. Gọi `computer_screenshot` để xem trạng thái hiện tại 3. Thực thi mỗi bước qua `computer_command_queue` (click, type) 4. Xác minh kết quả qua ảnh chụp màn hình 5. Lưu bất kỳ lỗi nào dưới dạng bộ nhớ `mistake` ## Bước 4: Kiểm thử tự sửa chữa Khi kiểm thử thất bại, lưu lỗi và cách khôi phục: ```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" }) # Example 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.") ``` Lần tới LLM chạy kiểm thử, nó thu hồi lỗi và áp dụng khôi phục tự động. ## Bước 5: Theo dõi kết quả kiểm thử Theo dõi lần chạy kiểm thử dưới dạng tác vụ: ```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" }) ``` ## Lệnh phổ biến | Hành động | Lệnh | |--------|---------| | Khởi chạy Simulator | `xcrun simctl launch booted com.example.app` | | Ảnh chụp màn hình | `computer_screenshot` (qua Synapse MCP) | | Nhấp tại (x,y) | `computer_command_queue {type:"click", payload:{x,y}}` | | Nhập văn bản | `computer_command_queue {type:"type", payload:{text:"..."}}` | | Nhấn Home | `computer_command_queue {type:"key", payload:{keys:["Cmd","Shift","H"]}}` | ## Thực hành tốt nhất > [!TIP] > - **Lưu tọa độ UI dưới dạng bộ nhớ** — UI thay đổi, nhưng LLM có thể học lại > - **Sử dụng nhãn accessibility** — ổn định hơn tọa độ > - **Lưu dữ liệu kiểm thử riêng biệt** — sử dụng biến cho tên người dùng, mật khẩu > - **Chạy kiểm thử trong trạng thái sạch** — đặt lại Simulator giữa các lần chạy > - **Ghi ảnh chụp màn hình cho lỗi** — hữu ích cho gỡ lỗi ## Bước tiếp theo - [Kiểm thử tự sửa chữa](/docs/guides/self-healing-tests) - [Computer Control API](/docs/api/computers) - [Thực hành tốt nhất về bộ nhớ](/docs/guides/memory-best-practices)