Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions docs/src/api/class-locator.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,11 @@ Returns locator to the n-th matching element.
### param: Locator.nth.index
- `index` <[int]>

## method: Locator.page
- returns: <[Page]>

A page this locator belongs to.

## async method: Locator.press

Focuses the element, and then uses [`method: Keyboard.down`] and [`method: Keyboard.up`].
Expand Down
4 changes: 4 additions & 0 deletions packages/playwright-core/src/client/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class Locator implements api.Locator {
});
}

page() {
return this._frame.page();
}

async boundingBox(options?: TimeoutOptions): Promise<Rect | null> {
return this._withElement(h => h.boundingBox(), options?.timeout);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9279,6 +9279,11 @@ export interface Locator {
*/
nth(index: number): Locator;

/**
* A page this locator belongs to.
*/
page(): Page;

/**
* Focuses the element, and then uses [keyboard.down(key)](https://playwright.dev/docs/api/class-keyboard#keyboard-down)
* and [keyboard.up(key)](https://playwright.dev/docs/api/class-keyboard#keyboard-up).
Expand Down
12 changes: 12 additions & 0 deletions tests/page/locator-convenience.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,15 @@ it('isVisible and isHidden should work with details', async ({ page }) => {

await expect(page.locator('ul')).toBeHidden();
});

it('should return page', async ({ page, server }) => {
await page.goto(server.PREFIX + '/frames/two-frames.html');
const outer = page.locator('#outer');
expect(outer.page()).toBe(page);

const inner = outer.locator('#inner');
expect(inner.page()).toBe(page);

const inFrame = page.frames()[1].locator('div');
expect(inFrame.page()).toBe(page);
});