--- name: selectors description: Selector strategy, exploration-first workflow, locator priority order (getByRole then getByLabel then getByPlaceholder then getByText then getByTestId), and feedback/validation-message selector rules for Playwright page objects. Use when creating page objects, writing or updating locators, generating UI tests, or deciding which selector strategy to use for a given element. Enforces mandatory live-app exploration via playwright-cli before any selector generation. For the page-object class structure, JSDoc rules, and fixture registration see the page-objects skill; for the exploration tool itself see the playwright-cli skill; for UI message strings used inside getByText see the enums skill. author: Ivan Davidov --- # Selector Strategy ## Critical - **Selector priority order is mandatory:** `getByRole` > `getByLabel` > `getByPlaceholder` > `getByText` > `getByTestId`. Move to the next option only when the previous one is not feasible. - **NEVER** use XPath (`page.locator('//...')` or `'xpath=...'`). - **NEVER** use CSS class or ID selectors as the primary strategy (`page.locator('.btn-primary')`, `page.locator('#submit')`). Acceptable only as an absolute last resort after ruling out every semantic option. - **Exploration with `playwright-cli` is mandatory** before writing any selectors. No guessing from wireframes, docs, or screenshots. Read the `playwright-cli` skill for the commands. - If the app cannot be reached or auth fails, **stop and notify the human** — never ship placeholder locators with guessed names. - String values inside `getByText(...)` come from `enums/{area}/*` (e.g. `Messages.LOGIN_ERROR`). **Never** hardcode repeated UI strings. See the `enums` skill. - **Every page object covering forms or CRUD must include feedback / validation message selectors** — success, error, field validation, toast, loading, empty state as applicable. A page object without them is incomplete. - **Locators are `get` accessors returning `Locator`** — this is a style/readability convention in the scaffold. Playwright's `Locator` is lazy, so `get` and a `readonly` field set in the constructor behave identically at runtime. ## Instructions ### Phase 1: Open and authenticate Never generate selectors from assumptions or documentation alone. Before writing any locators or page objects, explore the live application **by running `playwright-cli` in the terminal** (read the `playwright-cli` skill). **Do not** use IDE browser MCP, Cursor browser tools, or any substitute — orchestrator rule: **No Substitute UI Exploration**. If `playwright-cli` cannot run, stop and notify the human. ```bash playwright-cli open playwright-cli snapshot ``` **If the page fails to load or requires authentication:** 1. **Stop immediately** — do not guess selectors or proceed with placeholder locators. 2. **Notify the human** with the exact issue: _"The application at `` returned [error/login page/blank screen]. I need [credentials / a different URL / instructions to set up auth state] before I can proceed."_ 3. **Wait** for the human to provide remediation (login credentials, storage state file, environment variables, or manual login instructions). 4. After remediation, re-open and verify the page loads correctly before continuing. ### Phase 2: Explore like a user Navigate through the feature under test the way a real user would. At each page/state, take a snapshot and observe: - **Forms** — input fields, labels, dropdowns, checkboxes, radio buttons. - **Buttons and CTAs** — submit, cancel, delete, edit, create actions. - **Navigation** — links, menus, breadcrumbs, tabs. - **Feedback elements** — success banners, error messages, validation errors on fields, toast notifications, loading spinners. - **Dynamic content** — content that appears after actions (modals, expanded sections, new rows in tables). ```bash playwright-cli snapshot playwright-cli click playwright-cli snapshot ``` Trigger CRUD operations where possible to discover the actual validation messages and success/error feedback the application displays. Capture the **exact** text rendered — this will go into enums via the `enums` skill. ### Phase 3: Plan test coverage Based on what was discovered, draft a test plan covering the critical paths. The plan should identify: 1. **Happy paths** — the primary successful flows (create, read, update, delete). 2. **Validation paths** — what happens when required fields are empty, invalid data is submitted, etc. 3. **Error paths** — server errors, permission denied, resource not found. 4. **Edge cases** — boundary inputs, concurrent operations, empty states. If feature documentation exists (user stories, acceptance criteria, design specs), cross-reference it with the discovered UI to ensure coverage is complete. **No human approval is needed for this plan** — proceed directly to generating selectors and page objects. ### Phase 4: Generate selectors Now that the real UI is understood, generate selectors using the Priority Order below. Pay special attention to feedback / validation message selectors — these are the most commonly missed. ## Priority Order (Mandatory) Use semantic locators in this order. Move to the next option ONLY when the previous one is not feasible: 1. **`getByRole()`** — Accessibility-based. Always the first choice for buttons, links, headings, textboxes, checkboxes, etc. 2. **`getByLabel()`** — For form inputs that have associated `