Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/guide/browser/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,17 @@ Internally, this method calls `.elements` and wraps every element using [`page.e

- [See `locator.elements()`](#elements)

### nth

```ts
function nth(index: number): Locator
```

This method returns a new locator that matches only a specific element within a multi-element locator.

Internally, this method calls `.elements`, verifies that the element exists, and wraps it using [`page.elementLocator`](/guide/browser/context#page). It's useful because it will throw a descriptive error
instead of returning `undefined` and allowing chained methods or matchers to fail mysteriously.

## Properties

### selector
Expand Down
4 changes: 4 additions & 0 deletions packages/browser/src/client/tester/locators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ export abstract class Locator {
return this.elements().map(element => this.elementLocator(element))
}

public nth(index: number): Locator {
return this.locator(`${this.selector} >> nth=${index}`)
}

public toString(): string {
return this.selector
}
Expand Down
10 changes: 10 additions & 0 deletions test/browser/fixtures/locators/blog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ test('renders blog posts', async () => {
expect(firstPost.element()).toHaveTextContent(/molestiae ut ut quas/)
expect(firstPost.getByRole('heading').element()).toHaveTextContent(/occaecati excepturi/)

expect(screen.getByRole('listitem').nth(0).element()).toHaveTextContent(/molestiae ut ut quas/)
var thrown
try {
screen.getByRole('listitem').nth(666)
} catch(err) {
thrown = err
}
expect(thrown).toBeInstanceOf(Error);
expect(thrown.message).toMatch(/^Cannot find element/)

await expect.element(secondPost.getByRole('heading')).toHaveTextContent('qui est esse')

await userEvent.click(secondPost.getByRole('button', { name: 'Delete' }))
Expand Down
Loading