/* 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 { TestAttachment, TestCase, TestResult, TestStep } from '@playwright-test/reporters/html'; import ansi2html from 'ansi-to-html'; import * as React from 'react'; import { TreeItem } from './treeItem'; import { msToString } from './uiUtils'; import { AutoChip } from './chip'; import { traceImage } from './images'; import { AttachmentLink } from './links'; import { statusIcon } from './statusIcon'; import { ImageDiff, ImageDiffView } from './imageDiffView'; import './testResultView.css'; function groupImageDiffs(screenshots: Set): ImageDiff[] { const snapshotNameToImageDiff = new Map(); for (const attachment of screenshots) { const match = attachment.name.match(/^(.*)-(expected|actual|diff|previous)(\.[^.]+)?$/); if (!match) continue; const [, name, category, extension = ''] = match; const snapshotName = name + extension; let imageDiff = snapshotNameToImageDiff.get(snapshotName); if (!imageDiff) { imageDiff = { name: snapshotName }; snapshotNameToImageDiff.set(snapshotName, imageDiff); } if (category === 'actual') imageDiff.left = { attachment, title: 'Actual' }; if (category === 'expected') imageDiff.right = { attachment, title: 'Expected' }; if (category === 'previous') imageDiff.right = { attachment, title: 'Previous' }; if (category === 'diff') imageDiff.diff = { attachment, title: 'Diff' }; } for (const [name, diff] of snapshotNameToImageDiff) { if (!diff.left || !diff.right) { snapshotNameToImageDiff.delete(name); } else { screenshots.delete(diff.left.attachment); screenshots.delete(diff.right.attachment); screenshots.delete(diff.diff?.attachment!); } } return [...snapshotNameToImageDiff.values()]; } export const TestResultView: React.FC<{ test: TestCase, result: TestResult, }> = ({ result }) => { const { screenshots, videos, traces, otherAttachments, diffs } = React.useMemo(() => { const attachments = result?.attachments || []; const screenshots = new Set(attachments.filter(a => a.contentType.startsWith('image/'))); const videos = attachments.filter(a => a.name === 'video'); const traces = attachments.filter(a => a.name === 'trace'); const otherAttachments = new Set(attachments); [...screenshots, ...videos, ...traces].forEach(a => otherAttachments.delete(a)); const diffs = groupImageDiffs(screenshots); return { screenshots: [...screenshots], videos, traces, otherAttachments, diffs }; }, [ result ]); return
{!!result.errors.length && {result.errors.map((error, index) => )} } {!!result.steps.length && {result.steps.map((step, i) => )} } {diffs.map((diff, index) => )} {!!screenshots.length && {screenshots.map((a, i) => { return
; })}
} {!!traces.length && {} } {!!videos.length && {videos.map((a, i) =>
)}
} {!!otherAttachments.size && {[...otherAttachments].map((a, i) => )} }
; }; const StepTreeItem: React.FC<{ step: TestStep; depth: number, }> = ({ step, depth }) => { return {msToString(step.duration)} {statusIcon(step.error || step.duration === -1 ? 'failed' : 'passed')} {step.title} {step.count > 1 && <> ✕ {step.count}} {step.location && — {step.location.file}:{step.location.line}} } loadChildren={step.steps.length + (step.snippet ? 1 : 0) ? () => { const children = step.steps.map((s, i) => ); if (step.snippet) children.unshift(); return children; } : undefined} depth={depth}>; }; const ErrorMessage: React.FC<{ error: string; }> = ({ error }) => { const html = React.useMemo(() => { const config: any = { bg: 'var(--color-canvas-subtle)', fg: 'var(--color-fg-default)', }; config.colors = ansiColors; return new ansi2html(config).toHtml(escapeHTML(error)); }, [error]); return
; }; const ansiColors = { 0: '#000', 1: '#C00', 2: '#0C0', 3: '#C50', 4: '#00C', 5: '#C0C', 6: '#0CC', 7: '#CCC', 8: '#555', 9: '#F55', 10: '#5F5', 11: '#FF5', 12: '#55F', 13: '#F5F', 14: '#5FF', 15: '#FFF' }; function escapeHTML(text: string): string { return text.replace(/[&"<>]/g, c => ({ '&': '&', '"': '"', '<': '<', '>': '>' }[c]!)); }