Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
test: migrate snapshotter tests to trace-viewer.spec (#38410)
Dmitry Gozman3a8a4a53c938
diff --git a/packages/playwright-core/src/server/trace/test/inMemorySnapshotter.ts b/packages/playwright-core/src/server/trace/test/inMemorySnapshotter.tsdeleted file mode 100644index 7da822af1d..0000000000--- a/packages/playwright-core/src/server/trace/test/inMemorySnapshotter.ts+++ /dev/null@@ -1,101 +0,0 @@-/**- * 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 { SnapshotStorage } from '../../../utils/isomorphic/trace/snapshotStorage';-import { ManualPromise } from '../../../utils';-import { HarTracer } from '../../har/harTracer';-import { Snapshotter } from '../recorder/snapshotter';--import type { SnapshotRenderer } from '../../../utils/isomorphic/trace/snapshotRenderer';-import type { BrowserContext } from '../../browserContext';-import type { HarTracerDelegate } from '../../har/harTracer';-import type { Page } from '../../page';-import type { SnapshotterBlob, SnapshotterDelegate } from '../recorder/snapshotter';-import type * as har from '@trace/har';-import type { FrameSnapshot } from '@trace/snapshot';---export class InMemorySnapshotter implements SnapshotterDelegate, HarTracerDelegate {- private _blobs = new Map<string, Buffer>();- private _snapshotter: Snapshotter;- private _harTracer: HarTracer;- private _snapshotReadyPromises = new Map<string, ManualPromise<SnapshotRenderer>>();- private _storage: SnapshotStorage;- private _snapshotCount = 0;-- constructor(context: BrowserContext) {- this._snapshotter = new Snapshotter(context, this);- this._harTracer = new HarTracer(context, null, this, { content: 'attach', includeTraceInfo: true, recordRequestOverrides: false, waitForContentOnStop: false });- this._storage = new SnapshotStorage();- }-- async initialize(): Promise<void> {- await this._snapshotter.start();- this._harTracer.start({ omitScripts: true });- }-- async reset() {- await this._snapshotter.reset();- await this._harTracer.flush();- this._harTracer.stop();- this._harTracer.start({ omitScripts: true });- }-- async dispose() {- this._snapshotter.dispose();- await this._harTracer.flush();- this._harTracer.stop();- }-- async captureSnapshot(page: Page, callId: string, snapshotName: string): Promise<SnapshotRenderer> {- if (this._snapshotReadyPromises.has(snapshotName))- throw new Error('Duplicate snapshot name: ' + snapshotName);-- this._snapshotter.captureSnapshot(page, callId, snapshotName).catch(() => {});- const promise = new ManualPromise<SnapshotRenderer>();- this._snapshotReadyPromises.set(snapshotName, promise);- return promise;- }-- onEntryStarted(entry: har.Entry) {- }-- onEntryFinished(entry: har.Entry) {- this._storage.addResource('', entry);- }-- onContentBlob(sha1: string, buffer: Buffer) {- this._blobs.set(sha1, buffer);- }-- onSnapshotterBlob(blob: SnapshotterBlob): void {- this._blobs.set(blob.sha1, blob.buffer);- }-- onFrameSnapshot(snapshot: FrameSnapshot): void {- ++this._snapshotCount;- const renderer = this._storage.addFrameSnapshot('', snapshot, []);- this._snapshotReadyPromises.get(snapshot.snapshotName || '')?.resolve(renderer);- }-- async resourceContentForTest(sha1: string): Promise<Buffer | undefined> {- return this._blobs.get(sha1);- }-- snapshotCount() {- return this._snapshotCount;- }-}diff --git a/tests/library/snapshotter.spec.ts b/tests/library/snapshotter.spec.tsdeleted file mode 100644index 12f8681f89..0000000000--- a/tests/library/snapshotter.spec.ts+++ /dev/null@@ -1,283 +0,0 @@-/**- * 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 { contextTest, expect } from '../config/browserTest';-import { InMemorySnapshotter } from '../../packages/playwright-core/lib/server/trace/test/inMemorySnapshotter';--const it = contextTest.extend<{ snapshotter: InMemorySnapshotter }>({- snapshotter: async ({ toImpl, context }, run) => {- const snapshotter = new InMemorySnapshotter(toImpl(context));- await snapshotter.initialize();- await run(snapshotter);- await snapshotter.dispose();- },-});--it.describe('snapshots', () => {- it('should collect snapshot', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<button>Hello</button>');- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot, page.url())).toBe('<BUTTON>Hello</BUTTON>');- });-- it('should preserve BASE and other content on reset', async ({ page, toImpl, snapshotter, server }) => {- await page.goto(server.EMPTY_PAGE);- const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- const html1 = snapshot1.render().html;- expect(html1).toContain(`<BASE href="${server.EMPTY_PAGE}"`);- await snapshotter.reset();- const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- const html2 = snapshot2.render().html;- expect(html2.replace(/call@2/g, `call@1`)).toEqual(html1);- });-- it('should capture resources', async ({ page, toImpl, server, snapshotter }) => {- await page.goto(server.EMPTY_PAGE);- await page.route('**/style.css', route => {- route.fulfill({ body: 'button { color: red; }', }).catch(() => {});- });- await page.setContent('<link rel="stylesheet" href="style.css"><button>Hello</button>');- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- const resource = snapshot.resourceByUrl(`${server.PREFIX}/style.css`, 'GET');- expect(resource).toBeTruthy();- });-- it('should collect multiple', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<button>Hello</button>');- await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- expect(snapshotter.snapshotCount()).toBe(2);- });-- it('should respect inline CSSOM change', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<style>button { color: red; }</style><button>Hello</button>');- const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot1, page.url())).toBe('<STYLE>button { color: red; }</STYLE><BUTTON>Hello</BUTTON>');-- await page.evaluate(() => { (document.styleSheets[0].cssRules[0] as any).style.color = 'blue'; });- const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- expect(distillSnapshot(snapshot2, page.url())).toBe('<STYLE>button { color: blue; }</STYLE><BUTTON>Hello</BUTTON>');- });-- it('should respect CSSOM change through CSSGroupingRule', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<style>@media { button { color: red; } }</style><button>Hello</button>');- await page.evaluate(() => {- window['rule'] = document.styleSheets[0].cssRules[0];- void 0;- });- const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot1, page.url())).toBe('<STYLE>@media {\n button { color: red; }\n}</STYLE><BUTTON>Hello</BUTTON>');-- await page.evaluate(() => { window['rule'].cssRules[0].style.color = 'blue'; });- const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- expect(distillSnapshot(snapshot2, page.url())).toBe('<STYLE>@media {\n button { color: blue; }\n}</STYLE><BUTTON>Hello</BUTTON>');-- await page.evaluate(() => { window['rule'].insertRule('button { color: green; }', 1); });- const snapshot3 = await snapshotter.captureSnapshot(toImpl(page), 'call@3', 'snapshot@call@3');- expect(distillSnapshot(snapshot3, page.url())).toBe('<STYLE>@media {\n button { color: blue; }\n button { color: green; }\n}</STYLE><BUTTON>Hello</BUTTON>');- });-- it('should respect node removal', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<div><button id="button1"></button><button id="button2"></button></div>');- const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot1, page.url())).toBe('<DIV><BUTTON id=\"button1\"></BUTTON><BUTTON id=\"button2\"></BUTTON></DIV>');- await page.evaluate(() => document.getElementById('button2').remove());- const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- expect(distillSnapshot(snapshot2, page.url())).toBe('<DIV><BUTTON id=\"button1\"></BUTTON></DIV>');- });-- it('should respect attr removal', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<div id="div" attr1="1" attr2="2"></div>');- const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot1, page.url())).toBe('<DIV id=\"div\" attr1=\"1\" attr2=\"2\"></DIV>');- await page.evaluate(() => document.getElementById('div').removeAttribute('attr2'));- const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- expect(distillSnapshot(snapshot2, page.url())).toBe('<DIV id=\"div\" attr1=\"1\"></DIV>');- });-- it('should have a custom doctype', async ({ page, server, toImpl, snapshotter }) => {- await page.goto(server.EMPTY_PAGE);- await page.setContent('<!DOCTYPE foo><body>hi</body>');-- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot, page.url())).toBe('<!DOCTYPE foo>hi');- });-- it('should replace meta charset attr that specifies charset', async ({ page, server, toImpl, snapshotter }) => {- await page.goto(server.EMPTY_PAGE);- await page.setContent('<meta charset="shift-jis" />');- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot, page.url())).toBe('<META charset="utf-8">');- });-- it('should replace meta content attr that specifies charset', async ({ page, server, toImpl, snapshotter }) => {- await page.goto(server.EMPTY_PAGE);- await page.setContent('<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">');- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot, page.url())).toBe('<META http-equiv="Content-Type" content="text/html; charset=utf-8">');- });-- it('should respect subresource CSSOM change', async ({ page, server, toImpl, snapshotter }) => {- await page.goto(server.EMPTY_PAGE);- await page.route('**/style.css', route => {- route.fulfill({ body: 'button { color: red; }', }).catch(() => {});- });- await page.setContent('<link rel="stylesheet" href="style.css"><button>Hello</button>');-- const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot1, page.url())).toBe('<LINK rel=\"stylesheet\" href=\"style.css\"><BUTTON>Hello</BUTTON>');-- await page.evaluate(() => { (document.styleSheets[0].cssRules[0] as any).style.color = 'blue'; });- const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- const resource = snapshot2.resourceByUrl(`${server.PREFIX}/style.css`, 'GET');- expect((await snapshotter.resourceContentForTest(resource.response.content._sha1)).toString()).toBe('button { color: blue; }');- });-- it('should capture frame', async ({ page, server, toImpl, snapshotter }) => {- await page.route('**/empty.html', route => {- route.fulfill({- body: '<frameset><frame src="frame.html"></frameset>',- contentType: 'text/html'- }).catch(() => {});- });- await page.route('**/frame.html', route => {- route.fulfill({- body: '<html><button>Hello iframe</button></html>',- contentType: 'text/html'- }).catch(() => {});- });- await page.goto(server.EMPTY_PAGE);-- for (let counter = 0; ; ++counter) {- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@' + counter, 'snapshot@call@' + counter);- const text = distillSnapshot(snapshot, page.url()).replace(/frame@[^"]+["]/, '<id>"');- if (text === '<FRAMESET><FRAME __playwright_src__=\"/snapshot/<id>\"></FRAME></FRAMESET>')- break;- await page.waitForTimeout(250);- }- });-- it('should capture iframe', async ({ page, server, toImpl, snapshotter }) => {- await page.route('**/empty.html', route => {- route.fulfill({- body: '<iframe src="iframe.html"></iframe>',- contentType: 'text/html'- }).catch(() => {});- });- await page.route('**/iframe.html', route => {- route.fulfill({- body: '<html><button>Hello iframe</button></html>',- contentType: 'text/html'- }).catch(() => {});- });- await page.goto(server.EMPTY_PAGE);-- // Marking iframe hierarchy is racy, do not expect snapshot, wait for it.- for (let counter = 0; ; ++counter) {- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@' + counter, 'snapshot@call@' + counter);- const text = distillSnapshot(snapshot, page.url()).replace(/frame@[^"]+["]/, '<id>"');- if (text === '<IFRAME __playwright_src__=\"/snapshot/<id>\"></IFRAME>')- break;- await page.waitForTimeout(250);- }- });--- it('should capture iframe with srcdoc', async ({ page, server, toImpl, snapshotter }) => {- await page.route('**/empty.html', route => {- route.fulfill({- body: '<iframe srcdoc="<html><button>Hello iframe</button></html>"></iframe>',- contentType: 'text/html'- }).catch(() => {});- });- await page.goto(server.EMPTY_PAGE);-- // Marking iframe hierarchy is racy, do not expect snapshot, wait for it.- for (let counter = 0; ; ++counter) {- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@' + counter, 'snapshot@call@' + counter);- const text = distillSnapshot(snapshot, page.url()).replace(/frame@[^"]+["]/, '<id>"');- if (text === '<IFRAME __playwright_src__=\"/snapshot/<id>\"></IFRAME>')- break;- await page.waitForTimeout(250);- }- });-- it('should collect on attribute change', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<button>Hello</button>');- {- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- expect(distillSnapshot(snapshot, page.url())).toBe('<BUTTON>Hello</BUTTON>');- }- const handle = await page.$('text=Hello')!;- await handle.evaluate(element => element.setAttribute('data', 'one'));- {- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- expect(distillSnapshot(snapshot, page.url())).toBe('<BUTTON data="one">Hello</BUTTON>');- }- await handle.evaluate(element => element.setAttribute('data', 'two'));- {- const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@3', 'snapshot@call@3');- expect(distillSnapshot(snapshot, page.url())).toBe('<BUTTON data="two">Hello</BUTTON>');- }- });-- it('empty adopted style sheets should not prevent node refs', async ({ page, toImpl, snapshotter }) => {- await page.setContent('<button>Hello</button>');- await page.evaluate(() => {- const sheet = new CSSStyleSheet();- document.adoptedStyleSheets = [sheet];-- const sheet2 = new CSSStyleSheet();- for (const element of [document.createElement('div'), document.createElement('span')]) {- const root = element.attachShadow({- mode: 'open'- });- root.append('foo');- root.adoptedStyleSheets = [sheet2];- document.body.appendChild(element);- }- });-- const renderer1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1');- // Expect some adopted style sheets.- expect(distillSnapshot(renderer1, page.url())).toContain('__playwright_style_sheet_');-- const renderer2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2');- const snapshot2 = renderer2.snapshot();- // Second snapshot should be just a copy of the first one.- expect(snapshot2.html).toEqual([[1, 13]]);- });-});--function distillSnapshot(snapshot, url: string, options: { distillTarget: boolean, distillBoundingRect: boolean } = { distillTarget: true, distillBoundingRect: true }) {- let { html } = snapshot.render();- if (options.distillTarget)- html = html.replace(/\s__playwright_target__="[^"]+"/g, '');- if (options.distillBoundingRect)- html = html.replace(/\s__playwright_bounding_rect__="[^"]+"/g, '');- return html- .replace(/<style>\*,\*::before,\*::after { visibility: hidden }<\/style>/, '')- .replace(/<script>[.\s\S]+<\/script>/, '')- .replace(/<style>.*__playwright_target__.*?<\/style>/, '')- .replace(/<BASE href="about:blank">/, '')- .replace(new RegExp(`<BASE href="${url}">`), '')- .replace(/<HTML>/, '')- .replace(/<\/HTML>/, '')- .replace(/<HEAD>/, '')- .replace(/<\/HEAD>/, '')- .replace(/<BODY>/, '')- .replace(/<\/BODY>/, '').trim();-}diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.tsindex d66c04f44e..f52ea66d30 100644--- a/tests/library/trace-viewer.spec.ts+++ b/tests/library/trace-viewer.spec.ts@@ -26,7 +26,8 @@ import type http from 'http';import { pathToFileURL } from 'url';import { expect, playwrightTest } from '../config/browserTest';import type { FrameLocator } from '@playwright/test';-import { rafraf, roundBox } from 'tests/page/pageTest';+import { rafraf, roundBox } from '../page/pageTest';+import { parseTrace } from '../config/utils';const test = playwrightTest.extend<TraceViewerFixtures>(traceViewerFixtures);@@ -711,6 +712,36 @@ test('should work with adopted style sheets and all: unset', async ({ page, runA}});+test('empty adopted style sheets should not prevent node refs', async ({ page }) => {+ const traceFile = test.info().outputPath('trace.zip');+ await page.context().tracing.start({ snapshots: true });+ await page.setContent('<button>Hello</button>');+ await page.evaluate(() => {+ const sheet = new CSSStyleSheet();+ document.adoptedStyleSheets = [sheet];++ const sheet2 = new CSSStyleSheet();+ for (const element of [document.createElement('div'), document.createElement('span')]) {+ const root = element.attachShadow({+ mode: 'open'+ });+ root.append('foo');+ root.adoptedStyleSheets = [sheet2];+ document.body.appendChild(element);+ }+ });+ await page.evaluate('2 + 2');+ await page.context().tracing.stop({ path: traceFile });++ const trace = await parseTrace(traceFile);+ const snapshots = trace.loader.storage();+ const secondEvaluate = trace.actions.findLast(a => a.method === 'evaluateExpression');+ expect(secondEvaluate.beforeSnapshot).toBeTruthy();+ const snapshot = snapshots.snapshotByName(snapshots.snapshotsForTest()[0], secondEvaluate.beforeSnapshot);+ // Second snapshot should be just a copy of the first one.+ expect(snapshot.snapshot().html).toEqual([[1, 9]]);+});+test('should work with nesting CSS selectors', async ({ page, runAndTrace }) => {test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/31607' });@@ -2168,3 +2199,47 @@ test('should respect CSSOM changes', async ({ runAndTrace, page, server }) => {const frame7 = await traceViewer.snapshotFrame('Evaluate', 4);await expect(frame7.locator('button')).toHaveCSS('color', 'rgb(0, 0, 255)');});++test('should preserve custom doctype', async ({ runAndTrace, page }) => {+ const traceViewer = await runAndTrace(async () => {+ await page.setContent('<!DOCTYPE foo><body>hi</body>');+ });+ const frame = await traceViewer.snapshotFrame('Set content');+ await expect.poll(() => frame.locator('body').evaluate(() => document.doctype.name)).toBe('foo');+});++test('should replace meta charset attr that specifies charset', async ({ runAndTrace, page, server }) => {+ const traceViewer = await runAndTrace(async () => {+ await page.goto(server.EMPTY_PAGE);+ await page.setContent('<meta charset="shift-jis" />');+ });+ const frame = await traceViewer.snapshotFrame('Set content');+ await expect.poll(() => frame.locator('body').evaluate(() => document.querySelector('meta')?.outerHTML.toLowerCase())).toBe('<meta charset="utf-8">');+});++test('should replace meta content attr that specifies charset', async ({ runAndTrace, page, server }) => {+ const traceViewer = await runAndTrace(async () => {+ await page.goto(server.EMPTY_PAGE);+ await page.setContent('<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">');+ });+ const frame = await traceViewer.snapshotFrame('Set content');+ await expect.poll(() => frame.locator('body').evaluate(() => document.querySelector('meta')?.outerHTML.toLowerCase())).toBe('<meta http-equiv="content-type" content="text/html; charset=utf-8">');+});++test('should capture iframe with srcdoc', async ({ page, server, runAndTrace }) => {+ await page.route('**/empty.html', route => {+ void route.fulfill({+ body: `<iframe srcdoc="<button>Hello iframe</button>"></iframe>`,+ contentType: 'text/html'+ }).catch(() => {});+ });++ const traceViewer = await runAndTrace(async () => {+ await page.goto(server.EMPTY_PAGE);+ await expect(page.frameLocator('iframe').locator('button')).toHaveText('Hello iframe');+ await page.evaluate('2+2');+ });++ const frame = await traceViewer.snapshotFrame('Evaluate');+ await expect(frame.frameLocator('iframe').getByRole('button')).toHaveText('Hello iframe');+});