$npx -y skills add anhtester/codex-testing-kit --skill smart-locator-agentSkill sinh locator ổn định và dễ bảo trì cho UI automation, hỗ trợ Playwright, Selenium và Appium.
| 1 | # Smart Locator Agent |
| 2 | |
| 3 | Purpose: Generate stable and maintainable locators for UI automation. |
| 4 | |
| 5 | Applicable frameworks: |
| 6 | |
| 7 | - Playwright |
| 8 | - Selenium |
| 9 | - Appium |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## When to Use |
| 14 | |
| 15 | Use this skill when: |
| 16 | |
| 17 | - Generating locators for new UI elements |
| 18 | - Reviewing existing locators for stability |
| 19 | - Migrating locators between frameworks |
| 20 | |
| 21 | --- |
| 22 | |
| 23 | ## Responsibilities |
| 24 | |
| 25 | The agent must: |
| 26 | |
| 27 | 1. Inspect the DOM or mobile UI hierarchy (NEVER guess) |
| 28 | 2. Identify stable attributes |
| 29 | 3. Generate a reliable locator |
| 30 | 4. Validate locator uniqueness |
| 31 | 5. Provide fallback locator if primary is fragile |
| 32 | |
| 33 | --- |
| 34 | |
| 35 | ## Locator Priority |
| 36 | |
| 37 | Use the following priority order: |
| 38 | |
| 39 | 1. Accessibility attributes (aria-label, role) |
| 40 | 2. `data-testid` / `data-test` / `data-qa` |
| 41 | 3. `id` |
| 42 | 4. `name` |
| 43 | 5. Framework semantic locator |
| 44 | 6. `css selector` |
| 45 | 7. `xpath` (last option) |
| 46 | |
| 47 | > **Note:** For detailed rules, refer to `.agents/rules/locator_strategy.md`. |
| 48 | |
| 49 | --- |
| 50 | |
| 51 | ## Playwright Locators |
| 52 | |
| 53 | Preferred locator methods: |
| 54 | |
| 55 | 1. `getByRole()` — Best for semantic elements |
| 56 | 2. `getByLabel()` — Best for form fields with labels |
| 57 | 3. `getByPlaceholder()` — Best for inputs with placeholder text |
| 58 | 4. `getByText()` — Best for text content |
| 59 | 5. `getByTestId()` — Best when data-testid is available |
| 60 | |
| 61 | Example: |
| 62 | ```typescript |
| 63 | page.getByRole("button", { name: "Submit" }) |
| 64 | page.getByLabel("Email") |
| 65 | page.getByPlaceholder("Enter your password") |
| 66 | ``` |
| 67 | |
| 68 | > **Note:** For detailed rules, refer to `.agents/rules/playwright_rules.md`. |
| 69 | |
| 70 | --- |
| 71 | |
| 72 | ## Selenium Locators |
| 73 | |
| 74 | Preferred order: |
| 75 | |
| 76 | 1. `id` |
| 77 | 2. `data-testid` |
| 78 | 3. `name` |
| 79 | 4. `cssSelector` |
| 80 | 5. `xpath` |
| 81 | |
| 82 | Example: |
| 83 | ```java |
| 84 | driver.findElement(By.id("login-button")); |
| 85 | driver.findElement(By.cssSelector("button[data-testid='submit-btn']")); |
| 86 | ``` |
| 87 | |
| 88 | > **Note:** For detailed rules, refer to `.agents/rules/selenium_rules.md`. |
| 89 | |
| 90 | --- |
| 91 | |
| 92 | ## Appium Locators |
| 93 | |
| 94 | Preferred order: |
| 95 | |
| 96 | 1. `accessibility id` |
| 97 | 2. `resource-id` |
| 98 | 3. `id` |
| 99 | 4. `iOS predicate string` |
| 100 | 5. `class chain` |
| 101 | 6. `xpath` |
| 102 | |
| 103 | Example: |
| 104 | ```java |
| 105 | driver.findElement(AppiumBy.accessibilityId("login_button")); |
| 106 | driver.findElement(AppiumBy.id("com.app:id/login_button")); |
| 107 | driver.findElement(AppiumBy.iOSNsPredicateString("label == 'Login'")); |
| 108 | ``` |
| 109 | |
| 110 | > **Note:** For detailed rules, refer to `.agents/rules/appium_rules.md`. |
| 111 | |
| 112 | --- |
| 113 | |
| 114 | ## Validation Rules |
| 115 | |
| 116 | Before using a locator, ensure: |
| 117 | |
| 118 | - [ ] It matches exactly one element |
| 119 | - [ ] The element is visible and interactable |
| 120 | - [ ] It is stable across page reloads |
| 121 | - [ ] It survives cosmetic DOM changes (layout, styling) |
| 122 | - [ ] It does NOT use dynamic class names or positional xpath |
| 123 | |
| 124 | --- |
| 125 | |
| 126 | ## Output Format |
| 127 | |
| 128 | When generating locators, provide: |
| 129 | |
| 130 | 1. **Primary locator** — The best, most stable option |
| 131 | 2. **Fallback locator** — Alternative if primary breaks |
| 132 | 3. **Reasoning** — Why this locator was chosen |
| 133 | |
| 134 | --- |
| 135 | |
| 136 | ## Rules References |
| 137 | |
| 138 | - `.agents/rules/locator_strategy.md` — Master locator priority map |
| 139 | - `.agents/rules/playwright_rules.md` — Playwright-specific locator rules |
| 140 | - `.agents/rules/selenium_rules.md` — Selenium-specific locator rules |
| 141 | - `.agents/rules/appium_rules.md` — Appium-specific locator rules |