|
1 | | -# Playwright Locator Healing Logic |
| 1 | +# Playwright Locator Healing Logic (with MCP) |
2 | 2 |
|
3 | | -## 1. Diagnose the Brittle Locator |
4 | | -Identify if the current locator uses any of these "Anti-Patterns": |
5 | | -- Long XPath strings (e.g., `/html/body/div[1]/...`) |
6 | | -- CSS classes that look autogenerated (e.g., `.css-19v7kv`) |
7 | | -- Locators relying on specific DOM nesting that is likely to change. |
| 3 | +## 1. Diagnose via MCP |
| 4 | +When using the `@playwright` tool: |
| 5 | +- Use `playwright_click` with a generic selector to see if the element responds. |
| 6 | +- Use the tool's output to find **Aria Roles** and **Labels** that aren't visible in the raw source code. |
8 | 7 |
|
9 | | -## 2. Healing Strategy (Priority Order) |
10 | | -Refactor the broken locator using the following Playwright priority list: |
11 | | -1. **Role (Preferred):** `page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login"))` |
12 | | -2. **Label:** `page.getByLabel("Username")` |
13 | | -3. **Placeholder:** `page.getByPlaceholder("Enter password")` |
14 | | -4. **Text:** `page.getByText("Welcome back")` |
15 | | -5. **Alt Text:** `page.getByAltText("Company Logo")` |
16 | | -6. **TestID:** `page.getByTestId("submit-button")` (If `data-testid` is present in your HTML). |
| 8 | +## 2. Healing Strategy (Java SDK mapping) |
| 9 | +Refactor using the live data from MCP into these Java patterns: |
17 | 10 |
|
18 | | -## 3. Example Transformation |
19 | | -**Broken:** |
20 | | -`private Locator loginBtn = page.locator("//div[@id='login-form']//button[2]");` |
| 11 | +| MCP Finding | Java SDK Implementation | |
| 12 | +| :--- | :--- | |
| 13 | +| Button with name "Submit" | `page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit"))` | |
| 14 | +| Input with label "Email" | `page.getByLabel("Email")` | |
| 15 | +| Image with alt "Logo" | `page.getByAltText("Logo")` | |
21 | 16 |
|
22 | | -**Healed:** |
23 | | -`private Locator loginBtn = page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Login"));` |
| 17 | +## 3. The "Double-Check" Rule |
| 18 | +Before finalizing the response, the agent should mentally (or via MCP) verify that the proposed locator is unique on the page to avoid "strict mode" violations in Playwright Java. |
0 commit comments