/* 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 './types'; import * as React from 'react'; import { TreeItem } from './treeItem'; import { msToString } from './utils'; import { AutoChip } from './chip'; import { traceImage } from './images'; import { AttachmentLink, generateTraceUrl } from './links'; import { statusIcon } from './statusIcon'; import type { ImageDiff } from '@web/shared/imageDiffView'; import { ImageDiffView } from '@web/shared/imageDiffView'; import { TestErrorView } from './testErrorView'; 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.actual = { attachment }; if (category === 'expected') imageDiff.expected = { attachment, title: 'Expected' }; if (category === 'previous') imageDiff.expected = { attachment, title: 'Previous' }; if (category === 'diff') imageDiff.diff = { attachment }; } for (const [name, diff] of snapshotNameToImageDiff) { if (!diff.actual || !diff.expected) { snapshotNameToImageDiff.delete(name); } else { screenshots.delete(diff.actual.attachment); screenshots.delete(diff.expected.attachment); screenshots.delete(diff.diff?.attachment!); } } return [...snapshotNameToImageDiff.values()]; } export const TestResultView: React.FC<{ test: TestCase, result: TestResult, anchor: 'video' | 'diff' | '', }> = ({ result, anchor }) => { const { screenshots, videos, traces, otherAttachments, diffs, htmls } = 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 htmls = attachments.filter(a => a.contentType.startsWith('text/html')); const otherAttachments = new Set(attachments); [...screenshots, ...videos, ...traces, ...htmls].forEach(a => otherAttachments.delete(a)); const diffs = groupImageDiffs(screenshots); return { screenshots: [...screenshots], videos, traces, otherAttachments, diffs, htmls }; }, [result]); const videoRef = React.useRef(null); const imageDiffRef = React.useRef(null); const [scrolled, setScrolled] = React.useState(false); React.useEffect(() => { if (scrolled) return; setScrolled(true); if (anchor === 'video') videoRef.current?.scrollIntoView({ block: 'start', inline: 'start' }); if (anchor === 'diff') imageDiffRef.current?.scrollIntoView({ block: 'start', inline: 'start' }); }, [scrolled, anchor, setScrolled, videoRef]); 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 && {
{traces.map((a, i) => )}
}
} {!!videos.length && {videos.map((a, i) =>
)}
} {!!(otherAttachments.size + htmls.length) && {[...htmls].map((a, i) => ( ) )} {[...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}>; };