Skip to content

Commit 2516a3f

Browse files
docs(browser): explain locator differences from testing-library (#9903)
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
1 parent 27ffbf2 commit 2516a3f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

api/browser/locators.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ The locator API uses a fork of [Playwright's locators](https://playwright.dev/do
1313
This page covers API usage. To better understand locators and their usage, read [Playwright's "Locators" documentation](https://playwright.dev/docs/locators).
1414
:::
1515

16+
::: tip Difference from `testing-library`
17+
Vitest's `page.getBy*` methods return a locator object, not a DOM element. This makes locator queries composable and allows Vitest to retry interactions and assertions when needed.
18+
19+
Compared to testing-library queries:
20+
21+
- Use locator chaining (`.getBy*`, `.filter`, `.nth`) instead of `within(...)`.
22+
- Keep locators around and interact with them later (`await locator.click()`), instead of resolving elements up front.
23+
- Single-element escape hatches like `.element()` and `.query()` are strict and throw if multiple elements match.
24+
25+
```ts
26+
import { expect } from 'vitest'
27+
import { page } from 'vitest/browser'
28+
29+
const deleteButton = page
30+
.getByRole('row')
31+
.filter({ hasText: 'Vitest' })
32+
.getByRole('button', { name: /delete/i })
33+
34+
await deleteButton.click()
35+
await expect.element(deleteButton).toBeEnabled()
36+
```
37+
:::
38+
1639
## getByRole
1740

1841
```ts

0 commit comments

Comments
 (0)