microsoft / microsoft/playwright

[Feature]: contextual role-based locators (before and after)

Open
#39,929 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P3-collecting-feedback
Dominant language
TypeScript
Stars
96.3k
Forks
6.5k
Avg merge
1d 6h
Merged PRs (30d)
180

Description

### 🚀 Feature Request

I don't want to drop to CSS Selectors or XPath bu use the Accessiblity Tree and search for elements before and/or after other elements.

### Example

```ts
// find an element only if it appears before another in the accessiblity tree
const searchByZipCode = page.getByRole("textbox", {
before: page.getByRole("button", { name: "Submit" }),
});

// find an element only if it appears after another in the accessiblity tree
const childInfoTable = page.getByRole("table", {
after: page.getByRole("heading", { name: "Child Info" }),
});

// find an element only if it appears between two others in the accessibility tree
const parentInfoTable = page.getByRole("table", {
after: page.getByRole("heading", { name: "Parent Info" }),
before: page.getByRole("heading", { name: "Dietary Restrictions" )),
});
```

### Motivation

A page can contain multiple matches for a selector (e.g. multiple tables) and to be less dependent on page implementations it is desirable to not use test IDs, class names, etc. but rely on queues for real users such as contextual information (e.g. preceding headings to a table where there are multiple tables on a page each with their own headings).

This is possible with CSS Selectors and XPath but not yet possible with role-based locators.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation files, tests, or entry points. Start by reviewing Playwright's role-based locator API and accessibility-tree behavior, then define how before, after, and combined constraints should work and add coverage for the three example cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.