37 lines
2.2 KiB
TypeScript
37 lines
2.2 KiB
TypeScript
import type { Page, CDPSession as PlaywrightCDPSession } from '@xmorse/playwright-core';
|
|
import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
|
|
/**
|
|
* Type-safe CDP session interface using devtools-protocol ProtocolMapping.
|
|
* Provides autocomplete and type checking for CDP commands and events.
|
|
* Return types are inferred from the command string (e.g. 'Page.getLayoutMetrics'
|
|
* returns Protocol.Page.GetLayoutMetricsResponse).
|
|
*/
|
|
export interface ICDPSession {
|
|
send<K extends keyof ProtocolMapping.Commands>(method: K, params?: ProtocolMapping.Commands[K]['paramsType'][0], sessionId?: string | null): Promise<ProtocolMapping.Commands[K]['returnType']>;
|
|
on<K extends keyof ProtocolMapping.Events>(event: K, callback: (params: ProtocolMapping.Events[K][0]) => void): unknown;
|
|
off<K extends keyof ProtocolMapping.Events>(event: K, callback: (params: ProtocolMapping.Events[K][0]) => void): unknown;
|
|
detach(): Promise<void>;
|
|
getSessionId?(): string | null;
|
|
}
|
|
/**
|
|
* Wraps Playwright's CDPSession (from context.getExistingCDPSession) into an ICDPSession.
|
|
* This reuses Playwright's internal CDP WebSocket instead of creating a new one,
|
|
* which is important for the relay server where Target.attachToTarget is intercepted.
|
|
*/
|
|
export declare class PlaywrightCDPSessionAdapter implements ICDPSession {
|
|
private _playwrightSession;
|
|
constructor(playwrightSession: PlaywrightCDPSession);
|
|
send<K extends keyof ProtocolMapping.Commands>(method: K, params?: ProtocolMapping.Commands[K]['paramsType'][0]): Promise<ProtocolMapping.Commands[K]['returnType']>;
|
|
on<K extends keyof ProtocolMapping.Events>(event: K, callback: (params: ProtocolMapping.Events[K][0]) => void): this;
|
|
off<K extends keyof ProtocolMapping.Events>(event: K, callback: (params: ProtocolMapping.Events[K][0]) => void): this;
|
|
detach(): Promise<void>;
|
|
}
|
|
/**
|
|
* Gets a CDP session for a page by reusing Playwright's internal existing CDP session.
|
|
* This uses the same WebSocket Playwright already has, avoiding new connections.
|
|
* Works through the relay because it doesn't call Target.attachToTarget.
|
|
*/
|
|
export declare function getCDPSessionForPage({ page }: {
|
|
page: Page;
|
|
}): Promise<PlaywrightCDPSessionAdapter>;
|
|
//# sourceMappingURL=cdp-session.d.ts.map
|