mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: split client-side instrumentation into sync and async (#31054)
This commit is contained in:
parent
61203964a8
commit
6675652269
@ -235,11 +235,11 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
|
|||||||
context.setDefaultTimeout(this._defaultContextTimeout);
|
context.setDefaultTimeout(this._defaultContextTimeout);
|
||||||
if (this._defaultContextNavigationTimeout !== undefined)
|
if (this._defaultContextNavigationTimeout !== undefined)
|
||||||
context.setDefaultNavigationTimeout(this._defaultContextNavigationTimeout);
|
context.setDefaultNavigationTimeout(this._defaultContextNavigationTimeout);
|
||||||
await this._instrumentation.onDidCreateBrowserContext(context);
|
await this._instrumentation.runAfterCreateBrowserContext(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _willCloseContext(context: BrowserContext) {
|
async _willCloseContext(context: BrowserContext) {
|
||||||
this._contexts.delete(context);
|
this._contexts.delete(context);
|
||||||
await this._instrumentation.onWillCloseBrowserContext(context);
|
await this._instrumentation.runBeforeCloseBrowserContext(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,21 +24,23 @@ export interface ClientInstrumentation {
|
|||||||
removeAllListeners(): void;
|
removeAllListeners(): void;
|
||||||
onApiCallBegin(apiCall: string, params: Record<string, any>, frames: StackFrame[], userData: any, out: { stepId?: string }): void;
|
onApiCallBegin(apiCall: string, params: Record<string, any>, frames: StackFrame[], userData: any, out: { stepId?: string }): void;
|
||||||
onApiCallEnd(userData: any, error?: Error): void;
|
onApiCallEnd(userData: any, error?: Error): void;
|
||||||
onDidCreateBrowserContext(context: BrowserContext): Promise<void>;
|
|
||||||
onDidCreateRequestContext(context: APIRequestContext): Promise<void>;
|
|
||||||
onWillPause(): void;
|
onWillPause(): void;
|
||||||
onWillCloseBrowserContext(context: BrowserContext): Promise<void>;
|
|
||||||
onWillCloseRequestContext(context: APIRequestContext): Promise<void>;
|
runAfterCreateBrowserContext(context: BrowserContext): Promise<void>;
|
||||||
|
runAfterCreateRequestContext(context: APIRequestContext): Promise<void>;
|
||||||
|
runBeforeCloseBrowserContext(context: BrowserContext): Promise<void>;
|
||||||
|
runBeforeCloseRequestContext(context: APIRequestContext): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ClientInstrumentationListener {
|
export interface ClientInstrumentationListener {
|
||||||
onApiCallBegin?(apiName: string, params: Record<string, any>, frames: StackFrame[], userData: any, out: { stepId?: string }): void;
|
onApiCallBegin?(apiName: string, params: Record<string, any>, frames: StackFrame[], userData: any, out: { stepId?: string }): void;
|
||||||
onApiCallEnd?(userData: any, error?: Error): void;
|
onApiCallEnd?(userData: any, error?: Error): void;
|
||||||
onDidCreateBrowserContext?(context: BrowserContext): Promise<void>;
|
|
||||||
onDidCreateRequestContext?(context: APIRequestContext): Promise<void>;
|
|
||||||
onWillPause?(): void;
|
onWillPause?(): void;
|
||||||
onWillCloseBrowserContext?(context: BrowserContext): Promise<void>;
|
|
||||||
onWillCloseRequestContext?(context: APIRequestContext): Promise<void>;
|
runAfterCreateBrowserContext?(context: BrowserContext): Promise<void>;
|
||||||
|
runAfterCreateRequestContext?(context: APIRequestContext): Promise<void>;
|
||||||
|
runBeforeCloseBrowserContext?(context: BrowserContext): Promise<void>;
|
||||||
|
runBeforeCloseRequestContext?(context: APIRequestContext): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createInstrumentation(): ClientInstrumentation {
|
export function createInstrumentation(): ClientInstrumentation {
|
||||||
@ -53,12 +55,19 @@ export function createInstrumentation(): ClientInstrumentation {
|
|||||||
return (listener: ClientInstrumentationListener) => listeners.splice(listeners.indexOf(listener), 1);
|
return (listener: ClientInstrumentationListener) => listeners.splice(listeners.indexOf(listener), 1);
|
||||||
if (prop === 'removeAllListeners')
|
if (prop === 'removeAllListeners')
|
||||||
return () => listeners.splice(0, listeners.length);
|
return () => listeners.splice(0, listeners.length);
|
||||||
if (!prop.startsWith('on'))
|
if (prop.startsWith('run')) {
|
||||||
return obj[prop];
|
|
||||||
return async (...params: any[]) => {
|
return async (...params: any[]) => {
|
||||||
for (const listener of listeners)
|
for (const listener of listeners)
|
||||||
await (listener as any)[prop]?.(...params);
|
await (listener as any)[prop]?.(...params);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
if (prop.startsWith('on')) {
|
||||||
|
return (...params: any[]) => {
|
||||||
|
for (const listener of listeners)
|
||||||
|
(listener as any)[prop]?.(...params);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return obj[prop];
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -77,7 +77,7 @@ export class APIRequest implements api.APIRequest {
|
|||||||
this._contexts.add(context);
|
this._contexts.add(context);
|
||||||
context._request = this;
|
context._request = this;
|
||||||
context._tracing._tracesDir = tracesDir;
|
context._tracing._tracesDir = tracesDir;
|
||||||
await context._instrumentation.onDidCreateRequestContext(context);
|
await context._instrumentation.runAfterCreateRequestContext(context);
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ export class APIRequestContext extends ChannelOwner<channels.APIRequestContextCh
|
|||||||
|
|
||||||
async dispose(options: { reason?: string } = {}): Promise<void> {
|
async dispose(options: { reason?: string } = {}): Promise<void> {
|
||||||
this._closeReason = options.reason;
|
this._closeReason = options.reason;
|
||||||
await this._instrumentation.onWillCloseRequestContext(this);
|
await this._instrumentation.runBeforeCloseRequestContext(this);
|
||||||
try {
|
try {
|
||||||
await this._channel.dispose(options);
|
await this._channel.dispose(options);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@ -268,19 +268,19 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
|
|||||||
onWillPause: () => {
|
onWillPause: () => {
|
||||||
currentTestInfo()?.setTimeout(0);
|
currentTestInfo()?.setTimeout(0);
|
||||||
},
|
},
|
||||||
onDidCreateBrowserContext: async (context: BrowserContext) => {
|
runAfterCreateBrowserContext: async (context: BrowserContext) => {
|
||||||
await artifactsRecorder?.didCreateBrowserContext(context);
|
await artifactsRecorder?.didCreateBrowserContext(context);
|
||||||
const testInfo = currentTestInfo();
|
const testInfo = currentTestInfo();
|
||||||
if (testInfo)
|
if (testInfo)
|
||||||
attachConnectedHeaderIfNeeded(testInfo, context.browser());
|
attachConnectedHeaderIfNeeded(testInfo, context.browser());
|
||||||
},
|
},
|
||||||
onDidCreateRequestContext: async (context: APIRequestContext) => {
|
runAfterCreateRequestContext: async (context: APIRequestContext) => {
|
||||||
await artifactsRecorder?.didCreateRequestContext(context);
|
await artifactsRecorder?.didCreateRequestContext(context);
|
||||||
},
|
},
|
||||||
onWillCloseBrowserContext: async (context: BrowserContext) => {
|
runBeforeCloseBrowserContext: async (context: BrowserContext) => {
|
||||||
await artifactsRecorder?.willCloseBrowserContext(context);
|
await artifactsRecorder?.willCloseBrowserContext(context);
|
||||||
},
|
},
|
||||||
onWillCloseRequestContext: async (context: APIRequestContext) => {
|
runBeforeCloseRequestContext: async (context: APIRequestContext) => {
|
||||||
await artifactsRecorder?.willCloseRequestContext(context);
|
await artifactsRecorder?.willCloseRequestContext(context);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -68,7 +68,7 @@ test.beforeAll(async function recordTrace({ browser, browserName, browserType, s
|
|||||||
|
|
||||||
// Go through instrumentation to exercise reentrant stack traces.
|
// Go through instrumentation to exercise reentrant stack traces.
|
||||||
const csi = {
|
const csi = {
|
||||||
onWillCloseBrowserContext: async () => {
|
runBeforeCloseBrowserContext: async () => {
|
||||||
await page.hover('body');
|
await page.hover('body');
|
||||||
await page.close();
|
await page.close();
|
||||||
traceFile = path.join(workerInfo.project.outputDir, String(workerInfo.workerIndex), browserName, 'trace.zip');
|
traceFile = path.join(workerInfo.project.outputDir, String(workerInfo.workerIndex), browserName, 'trace.zip');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user