$npx -y skills add anhtester/codex-testing-kit --skill locator-healer-agentSkill tự động phát hiện và sửa chữa locators bị hỏng khi automation tests fail do thay đổi DOM.
| 1 | # Locator Healer Agent |
| 2 | |
| 3 | Purpose: Automatically repair broken locators when automation tests fail. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | Use this skill when: |
| 10 | |
| 11 | - A test fails with "element not found" or "element detached" errors |
| 12 | - UI has changed and existing locators no longer work |
| 13 | - After a frontend deployment that modifies DOM structure |
| 14 | |
| 15 | --- |
| 16 | |
| 17 | ## Responsibilities |
| 18 | |
| 19 | When a locator fails: |
| 20 | |
| 21 | 1. Inspect current DOM or UI hierarchy |
| 22 | 2. Compare old locator with current page structure |
| 23 | 3. Identify updated attributes |
| 24 | 4. Generate a replacement locator |
| 25 | 5. Re-run test to verify the fix |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | ## Detection Strategy |
| 30 | |
| 31 | Locator is considered broken when: |
| 32 | |
| 33 | - Element not found (NoSuchElementException / TimeoutError) |
| 34 | - Element detached from DOM |
| 35 | - Selector matches zero elements |
| 36 | - Selector matches wrong element (different text/position) |
| 37 | |
| 38 | --- |
| 39 | |
| 40 | ## Healing Workflow |
| 41 | |
| 42 | ### Step 1: Analyze the Error |
| 43 | - Read error log to identify which locator failed |
| 44 | - Identify the Page Object file and line number |
| 45 | |
| 46 | ### Step 2: Inspect Current DOM |
| 47 | - Open the page using MCP tools |
| 48 | - Navigate to the same state as the failing test |
| 49 | - Inspect the target area in DOM |
| 50 | |
| 51 | ### Step 3: Find Alternative Locator |
| 52 | Try the following in priority order: |
| 53 | |
| 54 | 1. Accessibility attributes (`aria-label`, `role`) |
| 55 | 2. `data-testid` / `data-test` |
| 56 | 3. `id` (if stable, not auto-generated) |
| 57 | 4. Semantic locator (Playwright `getByRole`, `getByLabel`) |
| 58 | 5. `css selector` (stable attributes) |
| 59 | 6. `xpath` (relative, not positional) |
| 60 | |
| 61 | ### Step 4: Validate & Replace |
| 62 | - Verify the new locator matches exactly one element |
| 63 | - Verify the element is the correct one (text, position, behavior) |
| 64 | - Replace the broken locator in the Page Object class |
| 65 | - Re-run the test |
| 66 | |
| 67 | --- |
| 68 | |
| 69 | ## Difference from Smart Locator Agent |
| 70 | |
| 71 | | Aspect | Locator Healer | Smart Locator | |
| 72 | |--------|---------------|---------------| |
| 73 | | **Trigger** | Test failure (reactive) | New element (proactive) | |
| 74 | | **Input** | Broken locator + error log | HTML/DOM element | |
| 75 | | **Goal** | Fix existing locator | Generate new locator | |
| 76 | | **Workflow** | Error → Inspect → Replace → Verify | Inspect → Generate → Validate | |
| 77 | |
| 78 | --- |
| 79 | |
| 80 | ## Verification |
| 81 | |
| 82 | After healing: |
| 83 | |
| 84 | - [ ] Locator must match exactly one element |
| 85 | - [ ] Element is the correct target (verify text/attributes) |
| 86 | - [ ] Test must pass successfully |
| 87 | - [ ] Locator is stable across page reloads |
| 88 | |
| 89 | --- |
| 90 | |
| 91 | ## Rules References |
| 92 | |
| 93 | - `.agents/rules/locator_strategy.md` — Master locator priority map |
| 94 | - `.agents/rules/automation_rules.md` — General automation best practices |