api(page.frame): allow looking up frames by name (#1228)

This commit is contained in:
Dmitry Gozman 2020-03-04 17:53:39 -08:00 committed by GitHub
parent 6fb5168fb2
commit 5ee744cd26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View file

@ -614,6 +614,7 @@ page.removeListener('request', logRequest);
- [page.exposeFunction(name, playwrightFunction)](#pageexposefunctionname-playwrightfunction)
- [page.fill(selector, value[, options])](#pagefillselector-value-options)
- [page.focus(selector[, options])](#pagefocusselector-options)
- [page.frame(options)](#pageframeoptions)
- [page.frames()](#pageframes)
- [page.goBack([options])](#pagegobackoptions)
- [page.goForward([options])](#pagegoforwardoptions)
@ -1170,6 +1171,14 @@ If there's no element matching `selector`, the method throws an error.
Shortcut for [page.mainFrame().focus(selector)](#framefocusselector).
#### page.frame(options)
- `options` <[Object]>
- `name` <[string]> frame name specified in the `iframe`'s `name` attribute
- `url` <[string]|[RegExp]|[Function]> A glob pattern, regex pattern or predicate receiving frame's `url` as a [URL] object.
- returns: <[Frame]> frame matching the criteria.
Returns frame matching the specified criteria. Either `name` or `url` must be specified.
#### page.frames()
- returns: <[Array]<[Frame]>> An array of all frames attached to the page.

View file

@ -206,6 +206,15 @@ export class Page extends platform.EventEmitter {
return this._frameManager.mainFrame();
}
frame(options: { name?: string, url?: types.URLMatch }): frames.Frame | null {
assert(options.name || options.url, 'Either name or url matcher should be specified');
return this.frames().find(f => {
if (options.name)
return f.name() === options.name;
return platform.urlMatches(f.url(), options.url);
}) || null;
}
frames(): frames.Frame[] {
return this._frameManager.frames();
}

View file

@ -1120,4 +1120,29 @@ module.exports.describe = function({testRunner, expect, headless, playwright, FF
expect(page.context()).toBe(context);
});
});
describe('Page.frame', function() {
it('should respect name', async function({page, server}) {
await page.setContent(`
<a href="${server.EMPTY_PAGE}" target=target>empty.html</a>
<iframe name=target></iframe>
`);
expect(page.frame({ name: 'bogus' })).toBe(null);
const frame = page.frame({ name: 'target' });
expect(frame).toBeTruthy();
await Promise.all([
frame.waitForNavigation(),
page.click('a')
]);
expect(frame.url()).toBe(server.EMPTY_PAGE);
});
it('should respect url', async function({page, server}) {
await page.setContent(`
<a href="${server.EMPTY_PAGE}">empty.html target=target>empty.html</a>
<iframe src="${server.EMPTY_PAGE}"></iframe>
`);
expect(page.frame({ url: /bogus/ })).toBe(null);
expect(page.frame({ url: /empty/ }).url()).toBe(server.EMPTY_PAGE);
});
});
};