Skip to content

Commit

Permalink
refactor: add parentSession() to CDPSession
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Aug 8, 2023
1 parent 2dec12f commit 2d1bc67
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
29 changes: 27 additions & 2 deletions packages/puppeteer-core/src/common/Connection.ts
Expand Up @@ -310,7 +310,8 @@ export class Connection extends EventEmitter {
const session = new CDPSessionImpl(
this,
object.params.targetInfo.type,
sessionId
sessionId,
object.sessionId
);
this.#sessions.set(sessionId, session);
this.emit('sessionattached', session);
Expand Down Expand Up @@ -470,6 +471,15 @@ export class CDPSession extends EventEmitter {
throw new Error('Not implemented');
}

/**
* Parent session in terms of CDP's auto-attach mechanism.
*
* @internal
*/
parentSession(): CDPSession | undefined {
return undefined;
}

send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
Expand Down Expand Up @@ -504,21 +514,36 @@ export class CDPSessionImpl extends CDPSession {
#targetType: string;
#callbacks = new CallbackRegistry();
#connection?: Connection;
#parentSessionId?: string;

/**
* @internal
*/
constructor(connection: Connection, targetType: string, sessionId: string) {
constructor(
connection: Connection,
targetType: string,
sessionId: string,
parentSessionId: string | undefined
) {
super();
this.#connection = connection;
this.#targetType = targetType;
this.#sessionId = sessionId;
this.#parentSessionId = parentSessionId;
}

override connection(): Connection | undefined {
return this.#connection;
}

override parentSession(): CDPSession | undefined {
if (!this.#parentSessionId) {
return;
}
const parent = this.#connection?.session(this.#parentSessionId);
return parent ?? undefined;
}

override send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
Expand Down
Expand Up @@ -36,6 +36,9 @@ class MockCDPSession extends EventEmitter {
id() {
return '1';
}
parentSession() {
return undefined;
}
}

describe('DeviceRequestPrompt', function () {
Expand Down
3 changes: 3 additions & 0 deletions packages/puppeteer-core/src/common/NetworkManager.test.ts
Expand Up @@ -37,6 +37,9 @@ class MockCDPSession extends EventEmitter {
id() {
return '1';
}
parentSession() {
return undefined;
}
}

describe('NetworkManager', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/TestExpectations.json
Expand Up @@ -2649,7 +2649,7 @@
"testIdPattern": "[mouse.spec] Mouse should resize the textarea",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["PASS", "FAIL"]
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should select the text with mouse",
Expand Down

0 comments on commit 2d1bc67

Please sign in to comment.