Skip to content
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

feat: Implement Locator.nth() #7137

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
9 changes: 9 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,15 @@ export abstract class Locator {
return this.elements().map(element => this.elementLocator(element))
}

public nth(index: number): Locator {
const element = this.elements()[index]
xeger marked this conversation as resolved.
Show resolved Hide resolved
if (!element) {
// TODO: better error message (misleading -- we _did_ find elements, just not enough of them)
throw getElementError(this._pwSelector || this.selector, this._container || document.body)
}
return this.elementLocator(element)
}

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
xeger marked this conversation as resolved.
Show resolved Hide resolved
}
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