Skip to content

noPlaywrightNthMethods (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noPlaywrightNthMethods": "error"
}
}
}
}

Disallow selecting Playwright elements by position with first(), last(), and nth().

In Playwright, a locator describes how to find elements on the page. When a locator matches several elements, first(), last(), and nth() pick one of them by its position. The position of an element can change when matching elements are added, removed, or reordered, so the test can end up acting on a different element without any error. A locator that matches only the element you want keeps working when the rest of the page changes.

This rule reports every call to a method named first, last, or nth, whatever object the method is called on.

await page.locator("button").first().click();
code-block.js:1:30 lint/nursery/noPlaywrightNthMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ first() selects an element by its position.

> 1 │ await page.locator("button").first().click();
│ ^^^^^^^
2 │

ℹ When elements are added, removed, or reordered on the page, the test can act on a different element.

ℹ Use a locator that matches only the element you want, such as getByRole() with a name or getByTestId(), or narrow the matches with filter().

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

await page.locator("button").last().click();
code-block.js:1:30 lint/nursery/noPlaywrightNthMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ last() selects an element by its position.

> 1 │ await page.locator("button").last().click();
│ ^^^^^^
2 │

ℹ When elements are added, removed, or reordered on the page, the test can act on a different element.

ℹ Use a locator that matches only the element you want, such as getByRole() with a name or getByTestId(), or narrow the matches with filter().

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

await page.locator("button").nth(3).click();
code-block.js:1:30 lint/nursery/noPlaywrightNthMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ nth() selects an element by its position.

> 1 │ await page.locator("button").nth(3).click();
│ ^^^^^^
2 │

ℹ When elements are added, removed, or reordered on the page, the test can act on a different element.

ℹ Use a locator that matches only the element you want, such as getByRole() with a name or getByTestId(), or narrow the matches with filter().

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

await page.getByRole("button", { name: "Submit" }).click();
await page.getByTestId("submit-button").click();
await page.getByRole("listitem").filter({ hasText: "Product 2" }).click();