Skip to content

Commit

Permalink
cherry-pick(release-1.12): show interrupted actions in trace (#7333)
Browse files Browse the repository at this point in the history
PR #7329 SHA ec47b03

Co-authored-by: Pavel Feldman <pavel.feldman@gmail.com>
  • Loading branch information
aslushnikov and pavelfeldman authored Jun 26, 2021
1 parent f52332a commit 6a2175e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/server/trace/recorder/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export class Tracing implements InstrumentationListener {
private _appendEventChain = Promise.resolve();
private _snapshotter: TraceSnapshotter;
private _eventListeners: RegisteredListener[] = [];
private _pendingCalls = new Map<string, { sdkObject: SdkObject, metadata: CallMetadata }>();
private _pendingCalls = new Map<string, { sdkObject: SdkObject, metadata: CallMetadata, beforeSnapshot: Promise<void>, actionSnapshot?: Promise<void>, afterSnapshot?: Promise<void> }>();
private _context: BrowserContext;
private _traceFile: string | undefined;
private _resourcesDir: string;
private _sha1s: string[] = [];
private _started = false;
private _recordingTraceEvents = false;
private _tracesDir: string;

constructor(context: BrowserContext) {
Expand All @@ -56,9 +56,9 @@ export class Tracing implements InstrumentationListener {

async start(options: TracerOptions): Promise<void> {
// context + page must be the first events added, this method can't have awaits before them.
if (this._started)
if (this._recordingTraceEvents)
throw new Error('Tracing has already been started');
this._started = true;
this._recordingTraceEvents = true;
this._traceFile = path.join(this._tracesDir, (options.name || createGuid()) + '.trace');

this._appendEventChain = mkdirIfNeeded(this._traceFile);
Expand All @@ -83,18 +83,22 @@ export class Tracing implements InstrumentationListener {
}

async stop(): Promise<void> {
if (!this._started)
if (!this._eventListeners.length)
return;
this._started = false;
this._context.instrumentation.removeListener(this);
helper.removeEventListeners(this._eventListeners);
for (const { sdkObject, metadata } of this._pendingCalls.values())
for (const { sdkObject, metadata, beforeSnapshot, actionSnapshot, afterSnapshot } of this._pendingCalls.values()) {
await Promise.all([beforeSnapshot, actionSnapshot, afterSnapshot]);
if (!afterSnapshot)
metadata.error = 'Action was interrupted';
await this.onAfterCall(sdkObject, metadata);
}
for (const page of this._context.pages())
page.setScreencastOptions(null);
await this._snapshotter.stop();

// Ensure all writes are finished.
this._recordingTraceEvents = false;
await this._appendEventChain;
}

Expand All @@ -103,7 +107,7 @@ export class Tracing implements InstrumentationListener {
}

async export(): Promise<Artifact> {
if (!this._traceFile || this._started)
if (!this._traceFile || this._recordingTraceEvents)
throw new Error('Must start and stop tracing before exporting');
const zipFile = new yazl.ZipFile();
const failedPromise = new Promise<Artifact>((_, reject) => (zipFile as any as EventEmitter).on('error', reject));
Expand Down Expand Up @@ -142,23 +146,30 @@ export class Tracing implements InstrumentationListener {
}

async onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata) {
await this._captureSnapshot('before', sdkObject, metadata);
this._pendingCalls.set(metadata.id, { sdkObject, metadata });
const beforeSnapshot = this._captureSnapshot('before', sdkObject, metadata);
this._pendingCalls.set(metadata.id, { sdkObject, metadata, beforeSnapshot });
await beforeSnapshot;
}

async onBeforeInputAction(sdkObject: SdkObject, metadata: CallMetadata, element: ElementHandle) {
await this._captureSnapshot('action', sdkObject, metadata, element);
const actionSnapshot = this._captureSnapshot('action', sdkObject, metadata, element);
this._pendingCalls.get(metadata.id)!.actionSnapshot = actionSnapshot;
await actionSnapshot;
}

async onAfterCall(sdkObject: SdkObject, metadata: CallMetadata) {
if (!this._pendingCalls.has(metadata.id))
const pendingCall = this._pendingCalls.get(metadata.id);
if (!pendingCall || pendingCall.afterSnapshot)
return;
this._pendingCalls.delete(metadata.id);
if (!sdkObject.attribution.page)
if (!sdkObject.attribution.page) {
this._pendingCalls.delete(metadata.id);
return;
await this._captureSnapshot('after', sdkObject, metadata);
}
pendingCall.afterSnapshot = this._captureSnapshot('after', sdkObject, metadata);
await pendingCall.afterSnapshot;
const event: trace.ActionTraceEvent = { type: 'action', metadata };
this._appendTraceEvent(event);
this._pendingCalls.delete(metadata.id);
}

onEvent(sdkObject: SdkObject, metadata: CallMetadata) {
Expand Down Expand Up @@ -192,7 +203,7 @@ export class Tracing implements InstrumentationListener {
}

private _appendTraceEvent(event: any) {
if (!this._started)
if (!this._recordingTraceEvents)
return;

const visit = (object: any) => {
Expand Down
15 changes: 15 additions & 0 deletions tests/tracing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ for (const params of [
});
}

test('should include interrupted actions', async ({ context, page, server }, testInfo) => {
await context.tracing.start({ name: 'test', screenshots: true, snapshots: true });
await page.goto(server.EMPTY_PAGE);
await page.setContent('<button>Click</button>');
page.click('"ClickNoButton"').catch(() => {});
await context.tracing.stop({ path: testInfo.outputPath('trace.zip') });
await context.close();

const { events } = await parseTrace(testInfo.outputPath('trace.zip'));
const clickEvent = events.find(e => e.metadata?.apiName === 'page.click');
expect(clickEvent).toBeTruthy();
expect(clickEvent.metadata.error).toBe('Action was interrupted');
});


async function parseTrace(file: string): Promise<{ events: any[], resources: Map<string, Buffer> }> {
const entries = await new Promise<any[]>(f => {
const entries: Promise<any>[] = [];
Expand Down

0 comments on commit 6a2175e

Please sign in to comment.