mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(targets): add target.opener() (#2641)
This adds a `.opener` property to a target so that its origin can be tracked. For now returns `null` when there's no `openerId`. Fixes #1830
This commit is contained in:
parent
0b94fa70eb
commit
f6356683cd
@ -260,6 +260,7 @@
|
||||
* [target.browser()](#targetbrowser)
|
||||
* [target.browserContext()](#targetbrowsercontext)
|
||||
* [target.createCDPSession()](#targetcreatecdpsession)
|
||||
* [target.opener()](#targetopener)
|
||||
* [target.page()](#targetpage)
|
||||
* [target.type()](#targettype)
|
||||
* [target.url()](#targeturl)
|
||||
@ -2808,6 +2809,11 @@ The browser context the target belongs to.
|
||||
|
||||
Creates a Chrome Devtools Protocol session attached to the target.
|
||||
|
||||
#### target.opener()
|
||||
- returns: <?[Target]>
|
||||
|
||||
Get the target that opened this target. Top-level targets return `null`.
|
||||
|
||||
#### target.page()
|
||||
- returns: <[Promise]<?[Page]>>
|
||||
|
||||
|
@ -76,6 +76,16 @@ class Target {
|
||||
return this._browserContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {Puppeteer.Target}
|
||||
*/
|
||||
opener() {
|
||||
const { openerId } = this._targetInfo;
|
||||
if (!openerId)
|
||||
return null;
|
||||
return this.browser()._targets.get(openerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Protocol.Target.TargetInfo} targetInfo
|
||||
*/
|
||||
|
9
test/assets/popup/popup.html
Normal file
9
test/assets/popup/popup.html
Normal file
@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Popup</title>
|
||||
</head>
|
||||
<body>
|
||||
I am a popup
|
||||
</body>
|
||||
</html>
|
11
test/assets/popup/window-open.html
Normal file
11
test/assets/popup/window-open.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Popup test</title>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
window.open('./popup.html');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -139,5 +139,15 @@ module.exports.addTests = function({testRunner, expect, puppeteer, browserWithEx
|
||||
// Cleanup.
|
||||
await newPage.close();
|
||||
});
|
||||
it('should have an opener', async({page, server, browser}) => {
|
||||
await page.goto(server.EMPTY_PAGE);
|
||||
const [createdTarget] = await Promise.all([
|
||||
new Promise(fulfill => browser.once('targetcreated', target => fulfill(target))),
|
||||
page.goto(server.PREFIX + '/popup/window-open.html')
|
||||
]);
|
||||
expect((await createdTarget.page()).url()).toBe(server.PREFIX + '/popup/popup.html');
|
||||
expect(createdTarget.opener()).toBe(page.target());
|
||||
expect(page.target().opener()).toBe(null);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user