-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Implement Locator.nth() #7137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
5798505
963e1c3
e622313
d2b4bb2
26506d6
79fbe4b
008bd67
5eb90d8
49774ef
e8c8201
48c2338
4f59685
1c8d42d
f37ac0f
339ca5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -387,6 +387,69 @@ It is recommended to use this only after the other locators don't work for your | |
|
|
||
| - [testing-library's `ByTestId`](https://testing-library.com/docs/queries/bytestid/) | ||
|
|
||
| ## nth | ||
|
|
||
| ```ts | ||
| function nth(index: number): Locator | ||
| ``` | ||
|
|
||
| This method returns a new locator that matches only a specific index within a multi-element query result. This is similar to calling `elements()[n]` but easier to use with asynchronous retry. | ||
|
|
||
| ```html | ||
| <div aria-label="one"><input/><input/><input/></div> | ||
| <div aria-label="two"><input/></div> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| page.getByRole('textbox').nth(0) // ✅ | ||
| page.getByRole('textbox').nth(4) // ❌ | ||
| ``` | ||
|
|
||
| ::: tip | ||
| Before resorting to `nth`, you may find it useful to use chained locators to narrow down your search. | ||
| Sometimes there is no better way to distinguish than by element position; although this can lead to flake, it's better than nothing. | ||
| ::: | ||
|
|
||
| ```tsx | ||
| page.getByLabel('two').getByRole('input') // ✅ better alternative to page.nth(3) | ||
xeger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| page.getByLabel('one').getByRole('input') // ❌ too ambiguous | ||
| page.getByLabel('one').getByRole('input').nth(1) // ✅ pragmatic compromise | ||
| ``` | ||
|
|
||
| ## first | ||
|
|
||
| ```ts | ||
| function first(): Locator | ||
| ``` | ||
|
|
||
| This method returns a new locator that matches only the first index of a multi-element query result. | ||
| It is sugar for `nth(0)`. | ||
|
|
||
| ```html | ||
| <input/> <input/> <input/> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| page.getByRole('textbox').first() // ✅ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am trying to use this in my project test succeeds but there is a type error Do I need to include some special types for this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. All public methods need to be defined also here: https://github.com/vitest-dev/vitest/blob/main/packages/browser/context.d.ts
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops; sorry about that. Addressed in #7176 |
||
| ``` | ||
|
|
||
| ## last | ||
|
|
||
| ```ts | ||
| function last(): Locator | ||
| ``` | ||
|
|
||
| This method returns a new locator that matches only the last index of a multi-element query result. | ||
| It is sugar for `nth(-1)`. | ||
|
|
||
| ```html | ||
| <input/> <input/> <input/> | ||
| ``` | ||
|
|
||
| ```tsx | ||
| page.getByRole('textbox').last() // ✅ | ||
| ``` | ||
|
|
||
| ## Methods | ||
|
|
||
| All methods are asynchronous and must be awaited. Since Vitest 3, tests will fail if a method is not awaited. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.