From 340cacf4fdfa5bb227997b2db5edb71eab6df1ab Mon Sep 17 00:00:00 2001 From: Sam Chen Date: Sat, 4 Feb 2023 02:32:23 +0800 Subject: [PATCH] feat: show diff tab by default (#19820) Close https://github.com/microsoft/playwright/issues/19803 I had to manually inject `window.playwrightReportBase64` data into `playwright/packages/html-reporter/index.html` when developing, wondering if there's any method I don't know. --- .../html-reporter/src/imageDiffView.spec.tsx | 8 ++++---- packages/html-reporter/src/imageDiffView.tsx | 18 ++++++++---------- packages/html-reporter/src/index.tsx | 2 +- tests/playwright-test/reporter-html.spec.ts | 2 ++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/html-reporter/src/imageDiffView.spec.tsx b/packages/html-reporter/src/imageDiffView.spec.tsx index 5f7cd71770..d2339919b0 100644 --- a/packages/html-reporter/src/imageDiffView.spec.tsx +++ b/packages/html-reporter/src/imageDiffView.spec.tsx @@ -38,14 +38,15 @@ const imageDiff: ImageDiff = { test('should render links', async ({ mount }) => { const component = await mount(); await expect(component.locator('a')).toHaveText([ + 'screenshot-diff.png', 'screenshot-actual.png', 'screenshot-expected.png', - 'screenshot-diff.png', ]); }); -test('should show actual by default', async ({ mount }) => { +test('should switch to actual', async ({ mount }) => { const component = await mount(); + await component.getByText('Actual', { exact: true }).click(); const sliderElement = component.locator('data-testid=test-result-image-mismatch-grip'); await expect.poll(() => sliderElement.evaluate(e => e.style.left), 'Actual slider is on the right').toBe('611px'); @@ -73,9 +74,8 @@ test('should switch to expected', async ({ mount }) => { } }); -test('should switch to diff', async ({ mount }) => { +test('should show diff by default', async ({ mount }) => { const component = await mount(); - await component.getByText('Diff', { exact: true }).click(); const image = component.locator('img'); const box = await image.boundingBox(); diff --git a/packages/html-reporter/src/imageDiffView.tsx b/packages/html-reporter/src/imageDiffView.tsx index 2c7492caec..705a827716 100644 --- a/packages/html-reporter/src/imageDiffView.tsx +++ b/packages/html-reporter/src/imageDiffView.tsx @@ -32,8 +32,8 @@ export type ImageDiff = { export const ImageDiffView: React.FunctionComponent<{ imageDiff: ImageDiff, }> = ({ imageDiff: diff }) => { - // Pre-select a tab called "actual", if any. - const [selectedTab, setSelectedTab] = React.useState('actual'); + // Pre-select a tab called "diff", if any. + const [selectedTab, setSelectedTab] = React.useState('diff'); const diffElement = React.useRef(null); const imageElement = React.useRef(null); const [sliderPosition, setSliderPosition] = React.useState(0); @@ -50,6 +50,11 @@ export const ImageDiffView: React.FunctionComponent<{ }; const tabs: TabbedPaneTab[] = []; if (diff.diff) { + tabs.push({ + id: 'diff', + title: 'Diff', + render: () => onImageLoaded()} /> + }); tabs.push({ id: 'actual', title: 'Actual', @@ -78,18 +83,11 @@ export const ImageDiffView: React.FunctionComponent<{ render: () => onImageLoaded()} /> }); } - if (diff.diff) { - tabs.push({ - id: 'diff', - title: 'Diff', - render: () => onImageLoaded()} /> - }); - } return
+ {diff.diff && } - {diff.diff && }
; }; diff --git a/packages/html-reporter/src/index.tsx b/packages/html-reporter/src/index.tsx index 33f1ca3f50..6f2d3b414f 100644 --- a/packages/html-reporter/src/index.tsx +++ b/packages/html-reporter/src/index.tsx @@ -17,7 +17,7 @@ import type { HTMLReport } from './types'; import type zip from '@zip.js/zip.js'; // @ts-ignore -import zipImport from '@zip.js/zip.js/dist/zip-no-worker-inflate.min.js'; +import * as zipImport from '@zip.js/zip.js/lib/zip-no-worker-inflate.js'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import './colors.css'; diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts index d5417333eb..129e7a377d 100644 --- a/tests/playwright-test/reporter-html.spec.ts +++ b/tests/playwright-test/reporter-html.spec.ts @@ -130,6 +130,7 @@ test('should include image diff', async ({ runInlineTest, page, showReport }) => const set = new Set(); const imageDiff = page.locator('data-testid=test-result-image-mismatch'); + await imageDiff.locator('text="Actual"').click(); const expectedImage = imageDiff.locator('img').first(); const actualImage = imageDiff.locator('img').last(); await expect(expectedImage).toHaveAttribute('src', /.*png/); @@ -255,6 +256,7 @@ test('should include image diff when screenshot failed to generate due to animat await expect(page.locator('text=Snapshot mismatch')).toHaveCount(0); await expect(page.locator('.chip-header', { hasText: 'Screenshots' })).toHaveCount(0); const imageDiff = page.locator('data-testid=test-result-image-mismatch'); + await imageDiff.locator('text="Actual"').click(); const image = imageDiff.locator('img'); await expect(image.first()).toHaveAttribute('src', /.*png/); await expect(image.last()).toHaveAttribute('src', /.*png/);