diff --git a/docs/src/api/class-locator.md b/docs/src/api/class-locator.md index 1c2bf4679b..64758085ef 100644 --- a/docs/src/api/class-locator.md +++ b/docs/src/api/class-locator.md @@ -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`]. diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.ts index 8db1258e0b..9c0b46fae7 100644 --- a/packages/playwright-core/src/client/locator.ts +++ b/packages/playwright-core/src/client/locator.ts @@ -59,6 +59,10 @@ export class Locator implements api.Locator { }); } + page() { + return this._frame.page(); + } + async boundingBox(options?: TimeoutOptions): Promise { return this._withElement(h => h.boundingBox(), options?.timeout); } diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts index f83af432a7..4bf7fe9ba4 100644 --- a/packages/playwright-core/types/types.d.ts +++ b/packages/playwright-core/types/types.d.ts @@ -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). diff --git a/tests/page/locator-convenience.spec.ts b/tests/page/locator-convenience.spec.ts index 9ebf21fbb2..2cba820a3b 100644 --- a/tests/page/locator-convenience.spec.ts +++ b/tests/page/locator-convenience.spec.ts @@ -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); +});