fix: deprecate Page.prototype.target (#11872)

This commit is contained in:
jrandolf 2024-02-08 13:56:58 +01:00 committed by GitHub
parent 86c3988e26
commit 15c986c2bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 14 additions and 12 deletions

View File

@ -4,6 +4,10 @@ sidebar_label: Page.target
# Page.target() method # Page.target() method
> Warning: This API is now obsolete.
>
> Use [Page.createCDPSession()](./puppeteer.page.createcdpsession.md) directly.
A target this page was created from. A target this page was created from.
#### Signature: #### Signature:

View File

@ -757,6 +757,8 @@ export abstract class Page extends EventEmitter<PageEvents> {
/** /**
* A target this page was created from. * A target this page was created from.
*
* @deprecated Use {@link Page.createCDPSession} directly.
*/ */
abstract target(): Target; abstract target(): Target;

View File

@ -181,9 +181,9 @@ describe('BrowserContext', function () {
}); });
expect(context1.targets()).toHaveLength(1); expect(context1.targets()).toHaveLength(1);
expect(context1.targets()[0]).toBe(page1.target()); expect(await context1.targets()[0]?.page()).toBe(page1);
expect(context2.targets()).toHaveLength(1); expect(context2.targets()).toHaveLength(1);
expect(context2.targets()[0]).toBe(page2.target()); expect(await context2.targets()[0]?.page()).toBe(page2);
// Make sure pages don't share localstorage or cookies. // Make sure pages don't share localstorage or cookies.
expect( expect(

View File

@ -472,7 +472,7 @@ describe('OOPIF', function () {
const {server, page} = state; const {server, page} = state;
// Setup our session listeners to observe OOPIF activity. // Setup our session listeners to observe OOPIF activity.
const session = await page.target().createCDPSession(); const session = await page.createCDPSession();
const networkEvents: string[] = []; const networkEvents: string[] = [];
const otherSessions: CDPSession[] = []; const otherSessions: CDPSession[] = [];
await session.send('Target.setAutoAttach', { await session.send('Target.setAutoAttach', {

View File

@ -583,14 +583,10 @@ describe('Page', function () {
// 3. After that, remove the iframe. // 3. After that, remove the iframe.
frame.remove(); frame.remove();
}); });
const popupTarget = page // 4. The target should always be the last one.
.browserContext() const popupTarget = page.browserContext().targets().at(-1)!;
.targets() // 5. Connect to the popup and make sure it doesn't throw and is not the same page.
.find(target => { expect(await popupTarget.page()).not.toBe(page);
return target !== page.target();
})!;
// 4. Connect to the popup and make sure it doesn't throw.
await popupTarget.page();
}); });
}); });

View File

@ -118,7 +118,7 @@ describe('request interception', function () {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await page.setRequestInterception(true); await page.setRequestInterception(true);
const cdp = await page.target().createCDPSession(); const cdp = await page.createCDPSession();
await cdp.send('DOM.enable'); await cdp.send('DOM.enable');
const urls: string[] = []; const urls: string[] = [];
page.on('request', request => { page.on('request', request => {