Skip to content
Back to skills

Selectors

ASecurity

Use when choosing a locator for an element, writing or reviewing locators in a page object, or exploring the live app before any UI code. Not for the page-object class shape or registration (see `page-objects`) or for the CLI commands themselves (see `playwright-cli`).

  • 312 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 22, 2026
testingtypescript

Works with

  • cli

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned November 29, 2026

npx -y skills add idavidov13/agentic-playwright --skill selectors --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Selectors?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Selectors
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/idavidov13-selectors/badge)](https://www.skillsdirectory.com/skills/idavidov13-selectors)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: selectors
description: Use when choosing a locator for an element, writing or reviewing locators in a page object, or exploring the live app before any UI code. Not for the page-object class shape or registration (see `page-objects`) or for the CLI commands themselves (see `playwright-cli`).
---

# Selectors

## Critical

- Priority order: `getByRole` > `getByLabel` > `getByPlaceholder` > `getByText` > `getByTestId`. Move to the next option only when the previous one is not feasible. One exception: a labelled form field takes `getByLabel('Email')` over `getByRole('textbox', { name: 'Email' })`.

    ```typescript
    // CORRECT
    page.getByRole('button', { name: 'Submit' });
    // WRONG
    page.locator('.btn-primary');
    ```

- Never XPath (`page.locator('//...')`, `'xpath=...'`). When no semantic option fits, ask for a test id and use `getByTestId`.
- A CSS locator is an exception, not a tier: it ships only after the human confirms the app cannot take a test id, with the reason on the line (`references/locator-choice.md`).

    ```typescript
    // eslint-disable-next-line playwright/no-raw-locators -- vendor grid: no role, name or test id
    page.getByRole('region', { name: 'Orders' }).locator('[data-row-id]');
    ```

- Exploration with `playwright-cli` is mandatory before any selector is written. No guessing from wireframes, docs or screenshots. If the app cannot be reached or auth fails, stop and notify the human; never ship placeholder locators.
- Strings inside `getByText(...)` come from `enums/{area}/*` (`Messages.LOGIN_ERROR`); the text is captured from the live app, never guessed (`enums`).
- Every page object for a form or CRUD screen has feedback selectors: success, error, field validation, and toast, loading or empty state where the app shows them.

## Procedure

1. Read the URL: `grep '^APP_URL' env/.env.${ENVIRONMENT:-dev}`. No env file → ask the human for the URL.
2. `playwright-cli open <APP_URL>`, then `playwright-cli snapshot`; commands in `playwright-cli`. Explore in the auth state the spec will run in: the `chromium` project starts signed in, so sign in through the login page first unless the spec calls `resetStorageState()`. The page fails to load or needs auth → stop per `references/exploration-workflow.md`.
3. Walk the feature as a user: snapshot, act, snapshot. Trigger each form submit and CRUD action. Record every element's role, accessible name, label, placeholder or test id, and the exact text of every message.
4. List the paths the page must support (happy, validation, error, edge); each path names the feedback locator that proves it.
5. Choose each locator by the priority order (`references/locator-choice.md`), add the feedback locators (`references/feedback-selectors.md`), and write them into the class per `page-objects`.

## References

- `references/exploration-workflow.md`: what to observe at each state and the stop-and-notify protocol when the app is unreachable.
- `references/locator-choice.md`: correct form per tier, exact and scoped matches, the CSS exception, multi-match and "stale" symptoms, elements with no accessible name.
- `references/feedback-selectors.md`: the feedback types to capture and the locator for each.

Files in this skill

  • SKILL.md10.8 KB
  • references/examples.md2.5 KB
  • references/feedback-selectors-example.md2.2 KB
  • references/troubleshooting.md2.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in testing →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.