Skip to content

Commit b9c0b92

Browse files
authored
chore: move some trace utilities to isomorphic (#38373)
1 parent fe662b3 commit b9c0b92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+137
-136
lines changed

packages/playwright-core/src/server/trace/test/inMemorySnapshotter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { SnapshotStorage } from '../../../../../trace-viewer/src/sw/snapshotStorage';
17+
import { SnapshotStorage } from '../../../utils/isomorphic/trace/snapshotStorage';
1818
import { ManualPromise } from '../../../utils';
1919
import { HarTracer } from '../../har/harTracer';
2020
import { Snapshotter } from '../recorder/snapshotter';
2121

22-
import type { SnapshotRenderer } from '../../../../../trace-viewer/src/sw/snapshotRenderer';
22+
import type { SnapshotRenderer } from '../../../utils/isomorphic/trace/snapshotRenderer';
2323
import type { BrowserContext } from '../../browserContext';
2424
import type { HarTracerDelegate } from '../../har/harTracer';
2525
import type { Page } from '../../page';
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*]
2+
./**
3+
../**

packages/trace-viewer/src/types/entries.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/entries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { Language } from '../../../playwright-core/src/utils/isomorphic/locatorGenerators';
17+
import type { Language } from '../locatorGenerators';
1818
import type { ResourceSnapshot } from '@trace/snapshot';
1919
import type * as trace from '@trace/trace';
2020

packages/trace-viewer/src/sw/snapshotRenderer.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/snapshotRenderer.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { escapeHTMLAttribute, escapeHTML } from '@isomorphic/stringUtils';
17+
import { escapeHTMLAttribute, escapeHTML } from '../stringUtils';
1818

1919
import type { FrameSnapshot, NodeNameAttributesChildNodesSnapshot, NodeSnapshot, RenderedFrameSnapshot, ResourceSnapshot, SubtreeReferenceSnapshot } from '@trace/snapshot';
20-
import type { PageEntry } from '../types/entries';
21-
import type { LRUCache } from './lruCache';
20+
import type { PageEntry } from './entries';
21+
import type { LRUCache } from '../lruCache';
2222

2323
function findClosest<T>(items: T[], metric: (v: T) => number, target: number) {
2424
return items.find((item, index) => {
@@ -254,7 +254,9 @@ declare global {
254254

255255
function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
256256
function applyPlaywrightAttributes(viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
257-
const searchParams = new URLSearchParams(location.search);
257+
// eslint-disable-next-line no-restricted-globals
258+
const win = window;
259+
const searchParams = new URLSearchParams(win.location.search);
258260
const shouldPopulateCanvasFromScreenshot = searchParams.has('shouldPopulateCanvasFromScreenshot');
259261
const isUnderTest = searchParams.has('isUnderTest');
260262

@@ -268,7 +270,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
268270
viewport,
269271
frames: new WeakMap(),
270272
};
271-
window['__playwright_frame_bounding_rects__'] = frameBoundingRectsInfo;
273+
win['__playwright_frame_bounding_rects__'] = frameBoundingRectsInfo;
272274

273275
const kPointerWarningTitle = 'Recorded click position in absolute coordinates did not' +
274276
' match the center of the clicked element. This is likely due to a difference between' +
@@ -279,7 +281,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
279281
const targetElements: Element[] = [];
280282
const canvasElements: HTMLCanvasElement[] = [];
281283

282-
let topSnapshotWindow: Window = window;
284+
let topSnapshotWindow: Window = win;
283285
while (topSnapshotWindow !== topSnapshotWindow.parent && !topSnapshotWindow.location.pathname.match(/\/page@[a-z0-9]+$/))
284286
topSnapshotWindow = topSnapshotWindow.parent;
285287

@@ -342,7 +344,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
342344
iframe.setAttribute('src', 'data:text/html,<body style="background: #ddd"></body>');
343345
} else {
344346
// Retain query parameters to inherit name=, time=, pointX=, pointY= and other values from parent.
345-
const url = new URL(window.location.href);
347+
const url = new URL(win.location.href);
346348
// We can be loading iframe from within iframe, reset base to be absolute.
347349
const index = url.pathname.lastIndexOf('/snapshot/');
348350
if (index !== -1)
@@ -354,10 +356,10 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
354356

355357
{
356358
const body = root.querySelector(`body[__playwright_custom_elements__]`);
357-
if (body && window.customElements) {
359+
if (body && win.customElements) {
358360
const customElements = (body.getAttribute('__playwright_custom_elements__') || '').split(',');
359361
for (const elementName of customElements)
360-
window.customElements.define(elementName, class extends HTMLElement {});
362+
win.customElements.define(elementName, class extends HTMLElement {});
361363
}
362364
}
363365

@@ -387,7 +389,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
387389
};
388390

389391
const onLoad = () => {
390-
window.removeEventListener('load', onLoad);
392+
win.removeEventListener('load', onLoad);
391393
for (const element of scrollTops) {
392394
element.scrollTop = +element.getAttribute('__playwright_scroll_top_')!;
393395
element.removeAttribute('__playwright_scroll_top_');
@@ -401,19 +403,19 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
401403
frameBoundingRectsInfo.frames.get(element)!.scrollLeft = element.scrollTop;
402404
}
403405

404-
document.styleSheets[0].disabled = true;
406+
win.document.styleSheets[0].disabled = true;
405407

406-
const search = new URL(window.location.href).searchParams;
407-
const isTopFrame = window === topSnapshotWindow;
408+
const search = new URL(win.location.href).searchParams;
409+
const isTopFrame = win === topSnapshotWindow;
408410

409411
if (search.get('pointX') && search.get('pointY')) {
410412
const pointX = +search.get('pointX')!;
411413
const pointY = +search.get('pointY')!;
412414
const hasInputTarget = search.has('hasInputTarget');
413415
const hasTargetElements = targetElements.length > 0;
414-
const roots = document.documentElement ? [document.documentElement] : [];
416+
const roots = win.document.documentElement ? [win.document.documentElement] : [];
415417
for (const target of (hasTargetElements ? targetElements : roots)) {
416-
const pointElement = document.createElement('x-pw-pointer');
418+
const pointElement = win.document.createElement('x-pw-pointer');
417419
pointElement.style.position = 'fixed';
418420
pointElement.style.backgroundColor = '#f44336';
419421
pointElement.style.width = '20px';
@@ -436,7 +438,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
436438
// "Warning symbol" indicates that action point is not 100% correct.
437439
// Note that action point is relative to the top frame, so we can only compare in the top frame.
438440
if (isTopFrame && (Math.abs(centerX - pointX) >= 10 || Math.abs(centerY - pointY) >= 10)) {
439-
const warningElement = document.createElement('x-pw-pointer-warning');
441+
const warningElement = win.document.createElement('x-pw-pointer-warning');
440442
warningElement.textContent = '⚠';
441443
warningElement.style.fontSize = '19px';
442444
warningElement.style.color = 'white';
@@ -445,21 +447,21 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
445447
pointElement.appendChild(warningElement);
446448
pointElement.setAttribute('title', kPointerWarningTitle);
447449
}
448-
document.documentElement.appendChild(pointElement);
450+
win.document.documentElement.appendChild(pointElement);
449451
} else if (isTopFrame && !hasInputTarget) {
450452
// For actions without a target element, e.g. page.mouse.move(),
451453
// show the point at the recorded location, which is relative to the top frame.
452454
pointElement.style.left = pointX + 'px';
453455
pointElement.style.top = pointY + 'px';
454-
document.documentElement.appendChild(pointElement);
456+
win.document.documentElement.appendChild(pointElement);
455457
}
456458
}
457459
}
458460

459461
if (canvasElements.length > 0) {
460462
function drawCheckerboard(context: CanvasRenderingContext2D, canvas: HTMLCanvasElement) {
461463
function createCheckerboardPattern() {
462-
const pattern = document.createElement('canvas');
464+
const pattern = win.document.createElement('canvas');
463465
pattern.width = pattern.width / Math.floor(pattern.width / 24);
464466
pattern.height = pattern.height / Math.floor(pattern.height / 24);
465467
const context = pattern.getContext('2d')!;
@@ -492,7 +494,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
492494
continue;
493495
}
494496

495-
let currWindow: Window = window;
497+
let currWindow: Window = win;
496498
while (currWindow !== topSnapshotWindow) {
497499
const iframe = currWindow.frameElement!;
498500
currWindow = currWindow.parent;
@@ -553,10 +555,10 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
553555
}
554556
};
555557

556-
const onDOMContentLoaded = () => visit(document);
558+
const onDOMContentLoaded = () => visit(win.document);
557559

558-
window.addEventListener('load', onLoad);
559-
window.addEventListener('DOMContentLoaded', onDOMContentLoaded);
560+
win.addEventListener('load', onLoad);
561+
win.addEventListener('DOMContentLoaded', onDOMContentLoaded);
560562
}
561563

562564
return `\n(${applyPlaywrightAttributes.toString()})(${JSON.stringify(viewport)}${targetIds.map(id => `, "${id}"`).join('')})`;

packages/trace-viewer/src/sw/snapshotServer.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/snapshotServer.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import type { SnapshotRenderer } from './snapshotRenderer';
1919
import type { SnapshotStorage } from './snapshotStorage';
2020
import type { ResourceSnapshot } from '@trace/snapshot';
2121

22-
type Point = { x: number, y: number };
23-
2422
export class SnapshotServer {
2523
private _snapshotStorage: SnapshotStorage;
2624
private _resourceLoader: (sha1: string) => Promise<Blob | undefined>;
@@ -117,12 +115,6 @@ export class SnapshotServer {
117115
}
118116
}
119117

120-
declare global {
121-
interface Window {
122-
showSnapshot: (url: string, point?: Point) => Promise<void>;
123-
}
124-
}
125-
126118
function removeHash(url: string) {
127119
try {
128120
const u = new URL(url);

packages/trace-viewer/src/sw/snapshotStorage.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/snapshotStorage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616

1717
import { rewriteURLForCustomProtocol, SnapshotRenderer } from './snapshotRenderer';
18-
import { LRUCache } from './lruCache';
18+
import { LRUCache } from '../lruCache';
1919

2020
import type { FrameSnapshot, ResourceSnapshot } from '@trace/snapshot';
21-
import type { PageEntry } from '../types/entries';
21+
import type { PageEntry } from './entries';
2222

2323

2424
export class SnapshotStorage {

packages/trace-viewer/src/sw/traceModel.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/traceLoader.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import { parseClientSideCallMetadata } from '@isomorphic/traceUtils';
1919
import { SnapshotStorage } from './snapshotStorage';
2020
import { TraceModernizer } from './traceModernizer';
2121

22-
import type { ContextEntry } from '../types/entries';
22+
import type { ContextEntry } from './entries';
2323

24-
export interface TraceModelBackend {
24+
export interface TraceLoaderBackend {
2525
entryNames(): Promise<string[]>;
2626
hasEntry(entryName: string): Promise<boolean>;
2727
readText(entryName: string): Promise<string | undefined>;
@@ -30,16 +30,16 @@ export interface TraceModelBackend {
3030
traceURL(): string;
3131
}
3232

33-
export class TraceModel {
33+
export class TraceLoader {
3434
contextEntries: ContextEntry[] = [];
3535
private _snapshotStorage: SnapshotStorage | undefined;
36-
private _backend!: TraceModelBackend;
36+
private _backend!: TraceLoaderBackend;
3737
private _resourceToContentType = new Map<string, string>();
3838

3939
constructor() {
4040
}
4141

42-
async load(backend: TraceModelBackend, unzipProgress: (done: number, total: number) => void) {
42+
async load(backend: TraceLoaderBackend, unzipProgress: (done: number, total: number) => void) {
4343
this._backend = backend;
4444

4545
const ordinals: string[] = [];

packages/trace-viewer/src/ui/modelUtil.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/traceModel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { Language } from '@isomorphic/locatorGenerators';
2020
import type { ResourceSnapshot } from '@trace/snapshot';
2121
import type * as trace from '@trace/trace';
2222
import type { ActionTraceEvent } from '@trace/trace';
23-
import type { ActionEntry, ContextEntry, PageEntry } from '../types/entries';
23+
import type { ActionEntry, ContextEntry, PageEntry } from '@isomorphic/trace/entries';
2424
import type { StackFrame } from '@protocol/channels';
2525
import type { ActionGroup } from '@isomorphic/protocolFormatter';
2626

@@ -61,7 +61,7 @@ export type ErrorDescription = {
6161

6262
export type Attachment = trace.AfterActionTraceEventAttachment & { callId: string };
6363

64-
export class MultiTraceModel {
64+
export class TraceModel {
6565
readonly startTime: number;
6666
readonly endTime: number;
6767
readonly browserName: string;

packages/trace-viewer/src/sw/traceModernizer.ts renamed to packages/playwright-core/src/utils/isomorphic/trace/traceModernizer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type * as traceV5 from './versions/traceV5';
2121
import type * as traceV6 from './versions/traceV6';
2222
import type * as traceV7 from './versions/traceV7';
2323
import type * as traceV8 from './versions/traceV8';
24-
import type { ActionEntry, ContextEntry, PageEntry } from '../types/entries';
24+
import type { ActionEntry, ContextEntry, PageEntry } from './entries';
2525
import type { SnapshotStorage } from './snapshotStorage';
2626

2727
export class TraceVersionError extends Error {
@@ -299,6 +299,7 @@ export class TraceModernizer {
299299
class: metadata.type,
300300
method: metadata.method,
301301
params: metadata.params,
302+
// eslint-disable-next-line no-restricted-globals
302303
wallTime: metadata.wallTime || Date.now(),
303304
log: metadata.log,
304305
beforeSnapshot: metadata.snapshots.find(s => s.title === 'before')?.snapshotName,

0 commit comments

Comments
 (0)