Skip to content

Commit 4fa8508

Browse files
committed
chore: move some trace utilities from sw to isomorphic
1 parent 05fa035 commit 4fa8508

File tree

24 files changed

+49
-42
lines changed

24 files changed

+49
-42
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';
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) => {
@@ -256,7 +256,9 @@ declare global {
256256

257257
function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
258258
function applyPlaywrightAttributes(viewport: ViewportSize, ...targetIds: (string | undefined)[]) {
259-
const searchParams = new URLSearchParams(location.search);
259+
// eslint-disable-next-line no-restricted-globals
260+
const win = window;
261+
const searchParams = new URLSearchParams(win.location.search);
260262
const shouldPopulateCanvasFromScreenshot = searchParams.has('shouldPopulateCanvasFromScreenshot');
261263
const isUnderTest = searchParams.has('isUnderTest');
262264

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

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

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

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

357359
{
358360
const body = root.querySelector(`body[__playwright_custom_elements__]`);
359-
if (body && window.customElements) {
361+
if (body && win.customElements) {
360362
const customElements = (body.getAttribute('__playwright_custom_elements__') || '').split(',');
361363
for (const elementName of customElements)
362-
window.customElements.define(elementName, class extends HTMLElement {});
364+
win.customElements.define(elementName, class extends HTMLElement {});
363365
}
364366
}
365367

@@ -391,7 +393,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
391393
};
392394

393395
const onLoad = () => {
394-
window.removeEventListener('load', onLoad);
396+
win.removeEventListener('load', onLoad);
395397
for (const element of scrollTops) {
396398
element.scrollTop = +element.getAttribute('__playwright_scroll_top_')!;
397399
element.removeAttribute('__playwright_scroll_top_');
@@ -405,19 +407,19 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
405407
frameBoundingRectsInfo.frames.get(element)!.scrollLeft = element.scrollTop;
406408
}
407409

408-
document.styleSheets[0].disabled = true;
410+
win.document.styleSheets[0].disabled = true;
409411

410-
const search = new URL(window.location.href).searchParams;
411-
const isTopFrame = window === topSnapshotWindow;
412+
const search = new URL(win.location.href).searchParams;
413+
const isTopFrame = win === topSnapshotWindow;
412414

413415
if (search.get('pointX') && search.get('pointY')) {
414416
const pointX = +search.get('pointX')!;
415417
const pointY = +search.get('pointY')!;
416418
const hasInputTarget = search.has('hasInputTarget');
417419
const hasTargetElements = targetElements.length > 0;
418-
const roots = document.documentElement ? [document.documentElement] : [];
420+
const roots = win.document.documentElement ? [win.document.documentElement] : [];
419421
for (const target of (hasTargetElements ? targetElements : roots)) {
420-
const pointElement = document.createElement('x-pw-pointer');
422+
const pointElement = win.document.createElement('x-pw-pointer');
421423
pointElement.style.position = 'fixed';
422424
pointElement.style.backgroundColor = '#f44336';
423425
pointElement.style.width = '20px';
@@ -440,7 +442,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
440442
// "Warning symbol" indicates that action point is not 100% correct.
441443
// Note that action point is relative to the top frame, so we can only compare in the top frame.
442444
if (isTopFrame && (Math.abs(centerX - pointX) >= 10 || Math.abs(centerY - pointY) >= 10)) {
443-
const warningElement = document.createElement('x-pw-pointer-warning');
445+
const warningElement = win.document.createElement('x-pw-pointer-warning');
444446
warningElement.textContent = '⚠';
445447
warningElement.style.fontSize = '19px';
446448
warningElement.style.color = 'white';
@@ -449,21 +451,21 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
449451
pointElement.appendChild(warningElement);
450452
pointElement.setAttribute('title', kPointerWarningTitle);
451453
}
452-
document.documentElement.appendChild(pointElement);
454+
win.document.documentElement.appendChild(pointElement);
453455
} else if (isTopFrame && !hasInputTarget) {
454456
// For actions without a target element, e.g. page.mouse.move(),
455457
// show the point at the recorded location, which is relative to the top frame.
456458
pointElement.style.left = pointX + 'px';
457459
pointElement.style.top = pointY + 'px';
458-
document.documentElement.appendChild(pointElement);
460+
win.document.documentElement.appendChild(pointElement);
459461
}
460462
}
461463
}
462464

463465
if (canvasElements.length > 0) {
464466
function drawCheckerboard(context: CanvasRenderingContext2D, canvas: HTMLCanvasElement) {
465467
function createCheckerboardPattern() {
466-
const pattern = document.createElement('canvas');
468+
const pattern = win.document.createElement('canvas');
467469
pattern.width = pattern.width / Math.floor(pattern.width / 24);
468470
pattern.height = pattern.height / Math.floor(pattern.height / 24);
469471
const context = pattern.getContext('2d')!;
@@ -496,7 +498,7 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
496498
continue;
497499
}
498500

499-
let currWindow: Window = window;
501+
let currWindow: Window = win;
500502
while (currWindow !== topSnapshotWindow) {
501503
const iframe = currWindow.frameElement!;
502504
currWindow = currWindow.parent;
@@ -557,10 +559,10 @@ function snapshotScript(viewport: ViewportSize, ...targetIds: (string | undefine
557559
}
558560
};
559561

560-
const onDOMContentLoaded = () => visit(document);
562+
const onDOMContentLoaded = () => visit(win.document);
561563

562-
window.addEventListener('load', onLoad);
563-
window.addEventListener('DOMContentLoaded', onDOMContentLoaded);
564+
win.addEventListener('load', onLoad);
565+
win.addEventListener('DOMContentLoaded', onDOMContentLoaded);
564566
}
565567

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

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/traceModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ 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

2424
export interface TraceModelBackend {
2525
entryNames(): Promise<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)