From 512a0f2a9debac9b8090be66dd0cfd93aa6e3fc2 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Fri, 4 Aug 2023 15:09:10 +0200 Subject: [PATCH] chore: enable _client for Chrome BiDi (#10688) --- .../src/common/bidi/BrowsingContext.ts | 13 +++++----- .../puppeteer-core/src/common/bidi/Frame.ts | 5 ++++ .../puppeteer-core/src/common/bidi/Page.ts | 4 ++++ test/TestExpectations.json | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts b/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts index 15322a09b0a..cc2a44e6c2b 100644 --- a/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts +++ b/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts @@ -4,9 +4,8 @@ import ProtocolMapping from 'devtools-protocol/types/protocol-mapping.js'; import {WaitForOptions} from '../../api/Page.js'; import {assert} from '../../util/assert.js'; import {Deferred} from '../../util/Deferred.js'; -import type {CDPSession, Connection as CDPConnection} from '../Connection.js'; +import {CDPSession, Connection as CDPConnection} from '../Connection.js'; import {ProtocolError, TargetCloseError, TimeoutError} from '../Errors.js'; -import {EventEmitter} from '../EventEmitter.js'; import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js'; import {getPageContent, setPageContent, waitWithTimeout} from '../util.js'; @@ -44,7 +43,7 @@ export const cdpSessions = new Map(); /** * @internal */ -export class CDPSessionWrapper extends EventEmitter implements CDPSession { +export class CDPSessionWrapper extends CDPSession { #context: BrowsingContext; #sessionId = Deferred.create(); #detached = false; @@ -70,11 +69,11 @@ export class CDPSessionWrapper extends EventEmitter implements CDPSession { } } - connection(): CDPConnection | undefined { + override connection(): CDPConnection | undefined { return undefined; } - async send( + override async send( method: T, ...paramArgs: ProtocolMapping.Commands[T]['paramsType'] ): Promise { @@ -92,7 +91,7 @@ export class CDPSessionWrapper extends EventEmitter implements CDPSession { return result.result; } - async detach(): Promise { + override async detach(): Promise { cdpSessions.delete(this.id()); await this.#context.cdpSession.send('Target.detachFromTarget', { sessionId: this.id(), @@ -100,7 +99,7 @@ export class CDPSessionWrapper extends EventEmitter implements CDPSession { this.#detached = true; } - id(): string { + override id(): string { const val = this.#sessionId.value(); return val instanceof Error || val === undefined ? '' : val; } diff --git a/packages/puppeteer-core/src/common/bidi/Frame.ts b/packages/puppeteer-core/src/common/bidi/Frame.ts index 85d8b1cf10c..768d5f45f49 100644 --- a/packages/puppeteer-core/src/common/bidi/Frame.ts +++ b/packages/puppeteer-core/src/common/bidi/Frame.ts @@ -19,6 +19,7 @@ import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; import {ElementHandle} from '../../api/ElementHandle.js'; import {Frame as BaseFrame} from '../../api/Frame.js'; import {Deferred} from '../../util/Deferred.js'; +import {CDPSession} from '../Connection.js'; import {UTILITY_WORLD_NAME} from '../FrameManager.js'; import {PuppeteerLifeCycleEvent} from '../LifecycleWatcher.js'; import {TimeoutSettings} from '../TimeoutSettings.js'; @@ -74,6 +75,10 @@ export class Frame extends BaseFrame { context.setFrame(this); } + override _client(): CDPSession { + return this.context().cdpSession; + } + override mainRealm(): Sandbox { return this.sandboxes[MAIN_SANDBOX]; } diff --git a/packages/puppeteer-core/src/common/bidi/Page.ts b/packages/puppeteer-core/src/common/bidi/Page.ts index d70f5036f7e..e3ed18b1ac0 100644 --- a/packages/puppeteer-core/src/common/bidi/Page.ts +++ b/packages/puppeteer-core/src/common/bidi/Page.ts @@ -132,6 +132,10 @@ export class Page extends PageBase { #browsingContext: BrowsingContext; #browserContext: BrowserContext; + _client(): CDPSession { + return this.mainFrame().context().cdpSession; + } + constructor( browsingContext: BrowsingContext, browserContext: BrowserContext diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 7ab43d60484..5daca7ca20c 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -1193,6 +1193,18 @@ "parameters": ["webDriverBiDi"], "expectations": ["PASS"] }, + { + "testIdPattern": "[page.spec] Page Page.close should set the page close state", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] + }, + { + "testIdPattern": "[page.spec] Page Page.close should terminate network waiters", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[page.spec] Page Page.close should terminate network waiters", "platforms": ["darwin", "linux", "win32"], @@ -2177,6 +2189,12 @@ "parameters": ["chrome", "webDriverBiDi"], "expectations": ["PASS"] }, + { + "testIdPattern": "[frame.spec] Frame specs Frame.client should return the client instance", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[frame.spec] Frame specs Frame.evaluate should throw for detached frames", "platforms": ["darwin", "linux", "win32"], @@ -3203,6 +3221,12 @@ "parameters": ["cdp", "firefox"], "expectations": ["SKIP"] }, + { + "testIdPattern": "[page.spec] Page Page.client should return the client instance", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["chrome", "webDriverBiDi"], + "expectations": ["PASS"] + }, { "testIdPattern": "[page.spec] Page Page.close should not be visible in browser.pages", "platforms": ["darwin", "linux", "win32"],