Choosing Shaft Locators
Testing & Qualitychoosing-shaft-locatorsChoosing Shaft Locators is an Agent Skill for testing & quality published in the ShaftHQ/SHAFT_ENGINE repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.
Use when creating, reviewing, refactoring, repairing, or generating SHAFT web/mobile locators, smart locators, ARIA locators, XPath/CSS replacements, or codegen element identifiers.
How to use this skill
Bring this guide into your coding agent with a prompt tailored to the tool you use.
- Open your project in Codex.
- Copy and paste the prompt below.
- Review files and risks before approving any action.
Inspect the Agent Skill at https://github.com/ShaftHQ/SHAFT_ENGINE/blob/HEAD/shaft-skills/choosing-shaft-locators/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/choosing-shaft-locators, but wait for my approval before writing files or running scripts.
Copying this prompt does not install or run the skill. Review third-party files before use.
Choosing SHAFT Locators
Overview
Choose locators that express user intent first and DOM mechanics last. A locator is not ready for generated code until it has been checked against the current page, app tree, or official guide pattern.
Locator Ladder
Stop at the first rung that uniquely identifies the element:
- Smart locator:
SHAFT.GUI.Locator.inputField("Email"),clickableField("Sign in"). - Semantic/ARIA locator:
SHAFT.GUI.Locator.hasRole(Role.BUTTON).hasText("Submit").build(). - Stable product-owned attribute:
data-testid, stableid,name, or mobileaccessibilityId. - Composed
SHAFT.GUI.Locatorbuilder with tag, text, attributes, parent/shadow/iframe context. - Stable CSS only when the app exposes no semantic signal.
- Native
By.xpath(...)only when required, never absolute XPath. Do not generateSHAFT.GUI.Locator.xpath(...).
MCP Checks
- Call
shaft-mcp:shaft_guide_searchforSmart Locators,SHAFT Locator Builder, orweb locator strategy. - For live web work, use
shaft-mcp:browser_open_intent,shaft-mcp:browser_get_page_dom, and screenshots when needed. - For Playwright projects, use the matching
shaft-mcp:playwright_*DOM and element tools. - For mobile, use
shaft-mcp:mobile_get_accessibility_treeand prefer accessibility IDs before XPath. - For repo insertion, call
shaft-mcp:shaft_coding_partner_planto see existing locator fields and page methods before adding a new locator. - Run
shaft-mcp:test_code_guardrails_checkon final Java snippets.
Codegen Rules
- Verify login, form, and navigation locators with real MCP actions before publishing them.
- Keep generated
SHAFT.GUI.Locator.*locators inline only for throwaway snippets; move stable locators into page objects for repo insertion. - Reuse locator summaries returned by
shaft_coding_partner_planand add only missing fields that the current DOM proves are needed. - Preserve user-provided locator choices from Capture when the recorder marks them as intentional.
- For complex XPath, first try Smart Locators, ARIA, and the SHAFT locator builder; use a native Selenium
Byobject only when those fail. - For SHAFT Playwright code, use native Playwright locators only as the same last fallback.
- Do not use coordinate-only actions while a locator candidate exists.
- Do not paste raw DOM snapshots into source code.
Examples
By email = SHAFT.GUI.Locator.inputField("Email");
By submit = SHAFT.GUI.Locator.clickableField("Create Account");
By alert = SHAFT.GUI.Locator.hasRole(Role.ALERT).containsText("error").build();
By checkout = SHAFT.GUI.Locator.hasAnyTagName()
.hasAttribute("data-testid", "checkout")
.build();
Tool Catalog
Every shaft-mcp tool name and description is cached in
../references/shaft-mcp-tools.md. Read it to pick exact tool names instead of
listing tools at runtime, and load only the schemas you need — on clients that
defer tool schemas, batch the load in one lookup. When a shaft-cli launcher
is installed, prefer running the same tools as shell commands per
../references/shaft-cli-commands.md (shaft-cli call <tool>), falling back
to shaft-mcp:<tool> MCP calls otherwise.
Official Guide Routes
- Locator strategy:
https://shafthq.github.io/docs/testing/web#locator-strategy - Smart Locators:
https://shafthq.github.io/docs/reference/actions/GUI/didYouKnow/Smart_Locators - Locator Builder:
https://shafthq.github.io/docs/reference/actions/GUI/didYouKnow/Shaft_Locator_Builder - Element identification:
https://shafthq.github.io/docs/reference/actions/GUI/Element_Identification - Mobile testing:
https://shafthq.github.io/docs/testing/mobile
Common Mistakes
| Mistake | Fix |
|---|---|
By.xpath("/html/body/...") | Use Smart Locator, ARIA, or builder context |
| Generated ID chosen blindly | Prefer visible label or app-owned test attribute |
| Multiple Smart Locator matches | Add parent/container context with SHAFT.GUI.Locator |
| Locator repaired from old report only | Inspect current DOM/tree before changing source |
Selenium @FindBy | Use By fields and SHAFT page object methods |