Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(trace): fix the v6-v7 trace modernizer (#35998)

Pavel Feldman3c888d59dd09
diff --git a/packages/trace-viewer/src/sw/traceModernizer.ts b/packages/trace-viewer/src/sw/traceModernizer.ts
index 16bf3ad2db..0e4c3ce914 100644
--- a/packages/trace-viewer/src/sw/traceModernizer.ts
+++ b/packages/trace-viewer/src/sw/traceModernizer.ts
@@ -19,6 +19,7 @@ import type * as traceV3 from './versions/traceV3';
import type * as traceV4 from './versions/traceV4';
import type * as traceV5 from './versions/traceV5';
import type * as traceV6 from './versions/traceV6';
+import type * as traceV7 from './versions/traceV7';
import type { ActionEntry, ContextEntry, PageEntry } from '../types/entries';
import type { SnapshotStorage } from './snapshotStorage';
@@ -29,6 +30,8 @@ export class TraceVersionError extends Error {
}
}
+// 6 => 10/2023 ~1.40
+// 7 => 05/2024 ~1.45
const latestVersion: trace.VERSION = 7;
export class TraceModernizer {
@@ -187,10 +190,9 @@ export class TraceModernizer {
}
private _modernize(event: any): trace.TraceEvent[] {
- // In trace 6->7 we also need to modernize context-options event.
- let version = this._version || event.version;
- if (version === undefined)
- return [event];
+ // First record does not have this._version, but should have a version in the event entry itself.
+ // Test traces before 7 (including 6) did not have version in the first entry, run the modernizer for 6=>*.
+ let version = this._version ?? event.version ?? 6;
let events = [event];
for (; version < latestVersion; ++version)
events = (this as any)[`_modernize_${version}_to_${version + 1}`].call(this, events);
@@ -376,16 +378,16 @@ export class TraceModernizer {
return result;
}
- _modernize_6_to_7(events: traceV6.TraceEvent[]): trace.TraceEvent[] {
- const result: trace.TraceEvent[] = [];
+ _modernize_6_to_7(events: traceV6.TraceEvent[]): traceV7.TraceEvent[] {
+ const result: traceV7.TraceEvent[] = [];
if (!this._processedContextCreatedEvent() && events[0].type !== 'context-options') {
- const event: trace.ContextCreatedTraceEvent = {
+ const event: traceV7.ContextCreatedTraceEvent = {
type: 'context-options',
origin: 'testRunner',
version: 7,
browserName: '',
options: {},
- platform: process.platform,
+ platform: 'unknown',
wallTime: 0,
monotonicTime: 0,
sdkLanguage: 'javascript',
diff --git a/packages/trace-viewer/src/sw/versions/traceV7.ts b/packages/trace-viewer/src/sw/versions/traceV7.ts
new file mode 100644
index 0000000000..2388321417
--- /dev/null
+++ b/packages/trace-viewer/src/sw/versions/traceV7.ts
@@ -0,0 +1,260 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { Entry as ResourceSnapshot } from '@trace/har';
+
+type Language = 'javascript' | 'python' | 'java' | 'csharp' | 'jsonl';
+type Point = { x: number, y: number };
+export type Size = { width: number, height: number };
+
+type StackFrame = {
+ file: string,
+ line: number,
+ column: number,
+ function?: string,
+};
+
+type Binary = Buffer;
+
+type SerializedValue = {
+ n?: number,
+ b?: boolean,
+ s?: string,
+ v?: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0',
+ d?: string,
+ u?: string,
+ bi?: string,
+ ta?: {
+ b: Binary,
+ k: 'i8' | 'ui8' | 'ui8c' | 'i16' | 'ui16' | 'i32' | 'ui32' | 'f32' | 'f64' | 'bi64' | 'bui64',
+ },
+ e?: {
+ m: string,
+ n: string,
+ s: string,
+ },
+ r?: {
+ p: string,
+ f: string,
+ },
+ a?: SerializedValue[],
+ o?: {
+ k: string,
+ v: SerializedValue,
+ }[],
+ h?: number,
+ id?: number,
+ ref?: number,
+};
+
+type SerializedError = {
+ error?: {
+ message: string,
+ name: string,
+ stack?: string,
+ },
+ value?: SerializedValue,
+};
+
+// Text node.
+type TextNodeSnapshot = string;
+// Subtree reference, "x snapshots ago, node #y". Could point to a text node.
+// Only nodes that are not references are counted, starting from zero, using post-order traversal.
+type SubtreeReferenceSnapshot = [ [number, number] ];
+// Node name, and optional attributes and child nodes.
+type NodeNameAttributesChildNodesSnapshot = [ string ] | [ string, Record<string, string>, ...NodeSnapshot[] ];
+
+type NodeSnapshot =
+ TextNodeSnapshot |
+ SubtreeReferenceSnapshot |
+ NodeNameAttributesChildNodesSnapshot;
+
+type ResourceOverride = {
+ url: string,
+ sha1?: string,
+ ref?: number
+};
+
+type FrameSnapshot = {
+ snapshotName?: string,
+ callId: string,
+ pageId: string,
+ frameId: string,
+ frameUrl: string,
+ timestamp: number,
+ wallTime?: number,
+ collectionTime: number,
+ doctype?: string,
+ html: NodeSnapshot,
+ resourceOverrides: ResourceOverride[],
+ viewport: { width: number, height: number },
+ isMainFrame: boolean,
+};
+
+type BrowserContextEventOptions = {
+ baseURL?: string,
+ viewport?: Size,
+ deviceScaleFactor?: number,
+ isMobile?: boolean,
+ userAgent?: string,
+};
+
+export type ContextCreatedTraceEvent = {
+ version: number,
+ type: 'context-options',
+ origin: 'testRunner' | 'library',
+ browserName: string,
+ channel?: string,
+ platform: string,
+ wallTime: number,
+ monotonicTime: number,
+ title?: string,
+ options: BrowserContextEventOptions,
+ sdkLanguage?: Language,
+ testIdAttributeName?: string,
+ contextId?: string,
+};
+
+export type ScreencastFrameTraceEvent = {
+ type: 'screencast-frame',
+ pageId: string,
+ sha1: string,
+ width: number,
+ height: number,
+ timestamp: number,
+ frameSwapWallTime?: number,
+};
+
+export type BeforeActionTraceEvent = {
+ type: 'before',
+ callId: string;
+ startTime: number;
+ apiName: string;
+ class: string;
+ method: string;
+ params: Record<string, any>;
+ stepId?: string;
+ beforeSnapshot?: string;
+ stack?: StackFrame[];
+ pageId?: string;
+ parentId?: string;
+};
+
+export type InputActionTraceEvent = {
+ type: 'input',
+ callId: string;
+ inputSnapshot?: string;
+ point?: Point;
+};
+
+export type AfterActionTraceEventAttachment = {
+ name: string;
+ contentType: string;
+ path?: string;
+ sha1?: string;
+ base64?: string;
+};
+
+export type AfterActionTraceEventAnnotation = {
+ type: string,
+ description?: string
+};
+
+export type AfterActionTraceEvent = {
+ type: 'after',
+ callId: string;
+ endTime: number;
+ afterSnapshot?: string;
+ error?: SerializedError['error'];
+ attachments?: AfterActionTraceEventAttachment[];
+ annotations?: AfterActionTraceEventAnnotation[];
+ result?: any;
+ point?: Point;
+};
+
+export type LogTraceEvent = {
+ type: 'log',
+ callId: string;
+ time: number;
+ message: string;
+};
+
+export type EventTraceEvent = {
+ type: 'event',
+ time: number;
+ class: string;
+ method: string;
+ params: any;
+ pageId?: string;
+};
+
+export type ConsoleMessageTraceEvent = {
+ type: 'console';
+ time: number;
+ pageId?: string;
+ messageType: string,
+ text: string,
+ args?: { preview: string, value: any }[],
+ location: {
+ url: string,
+ lineNumber: number,
+ columnNumber: number,
+ },
+};
+
+export type ResourceSnapshotTraceEvent = {
+ type: 'resource-snapshot',
+ snapshot: ResourceSnapshot,
+};
+
+export type FrameSnapshotTraceEvent = {
+ type: 'frame-snapshot',
+ snapshot: FrameSnapshot,
+};
+
+export type ActionTraceEvent = {
+ type: 'action',
+} & Omit<BeforeActionTraceEvent, 'type'>
+ & Omit<AfterActionTraceEvent, 'type'>
+ & Omit<InputActionTraceEvent, 'type'>;
+
+export type StdioTraceEvent = {
+ type: 'stdout' | 'stderr';
+ timestamp: number;
+ text?: string;
+ base64?: string;
+};
+
+export type ErrorTraceEvent = {
+ type: 'error';
+ message: string;
+ stack?: StackFrame[];
+};
+
+export type TraceEvent =
+ ContextCreatedTraceEvent |
+ ScreencastFrameTraceEvent |
+ ActionTraceEvent |
+ BeforeActionTraceEvent |
+ InputActionTraceEvent |
+ AfterActionTraceEvent |
+ EventTraceEvent |
+ LogTraceEvent |
+ ConsoleMessageTraceEvent |
+ ResourceSnapshotTraceEvent |
+ FrameSnapshotTraceEvent |
+ StdioTraceEvent |
+ ErrorTraceEvent;
diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.ts
index f329fb99d1..663c000e75 100644
--- a/tests/library/trace-viewer.spec.ts
+++ b/tests/library/trace-viewer.spec.ts
@@ -1080,7 +1080,7 @@ test('should open two trace files', async ({ context, page, request, server, sho
await expect(callLine.getByText('events')).toHaveText(/events:[\d]+/);
});
-test('should open two trace files of the same test', async ({ context, page, request, server, showTraceViewer, asset }, testInfo) => {
+test('should open two trace files of the same test (v6)', async ({ showTraceViewer, asset }) => {
const traceViewer = await showTraceViewer([asset('test-trace1.zip'), asset('test-trace2.zip')]);
// Same actions from different test runs should not be merged.
await expect(traceViewer.actionTitles).toHaveText([