feat: expose DevTools as a target (#10812)

This commit is contained in:
Alex Rudenko 2023-08-30 11:32:38 +02:00 committed by GitHub
parent 14f0ab7397
commit a540085176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View File

@ -45,6 +45,7 @@ import {
PageTarget,
CDPTarget,
WorkerTarget,
DevToolsTarget,
} from './Target.js';
import {TargetManager, TargetManagerEmittedEvents} from './TargetManager.js';
import {TaskQueue} from './TaskQueue.js';
@ -340,6 +341,18 @@ export class CDPBrowser extends BrowserBase {
this.#targetManager,
createSession
);
if (targetInfo.url?.startsWith('devtools://')) {
return new DevToolsTarget(
targetInfo,
session,
context,
this.#targetManager,
createSession,
this.#ignoreHTTPSErrors,
this.#defaultViewport ?? null,
this.#screenshotTaskQueue
);
}
if (this.#isPageTargetCallback(targetForFilter)) {
return new PageTarget(
targetInfo,

View File

@ -299,6 +299,11 @@ export class PageTarget extends CDPTarget {
}
}
/**
* @internal
*/
export class DevToolsTarget extends PageTarget {}
/**
* @internal
*/

View File

@ -340,6 +340,26 @@ const serviceWorkerExtensionPath = path.join(
]);
await browser.close();
});
it('should expose DevTools as a page', async () => {
const browser = await launchBrowser(
Object.assign({devtools: true}, headfulOptions)
);
const context = await browser.createIncognitoBrowserContext();
const [target] = await Promise.all([
browser.waitForTarget((target: {url: () => string | string[]}) => {
return target.url().includes('devtools://');
}),
context.newPage(),
]);
const page = await target.page();
expect(
await page?.evaluate(() => {
// @ts-expect-error wrong context.
return Boolean(DevToolsAPI);
})
).toBe(true);
await browser.close();
});
});
describe('Page.bringToFront', function () {