From aa2bedae26d8711d3ffb8180666917ecca17860d Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 19 Nov 2024 17:18:00 +0100 Subject: [PATCH] use Anchor --- packages/html-reporter/src/chip.tsx | 4 +-- packages/html-reporter/src/links.tsx | 6 ++-- packages/html-reporter/src/testResultView.tsx | 28 ++++++++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/html-reporter/src/chip.tsx b/packages/html-reporter/src/chip.tsx index f94dcbc6d6..cdd07777a6 100644 --- a/packages/html-reporter/src/chip.tsx +++ b/packages/html-reporter/src/chip.tsx @@ -20,7 +20,7 @@ import './colors.css'; import './common.css'; import * as icons from './icons'; import { clsx } from '@web/uiUtils'; -import { useAnchor } from './links'; +import { type AnchorID, useAnchor } from './links'; export const Chip: React.FC<{ header: JSX.Element | string, @@ -53,7 +53,7 @@ export const AutoChip: React.FC<{ noInsets?: boolean, children?: any, dataTestId?: string, - revealOnAnchorId?: string, + revealOnAnchorId?: AnchorID, }> = ({ header, initialExpanded, noInsets, children, dataTestId, revealOnAnchorId }) => { const [expanded, setExpanded] = React.useState(initialExpanded ?? true); const onReveal = React.useCallback(() => setExpanded(true), []); diff --git a/packages/html-reporter/src/links.tsx b/packages/html-reporter/src/links.tsx index 4beb7b65e0..e72c1ef98f 100644 --- a/packages/html-reporter/src/links.tsx +++ b/packages/html-reporter/src/links.tsx @@ -114,7 +114,7 @@ export function generateTraceUrl(traces: TestAttachment[]) { const kMissingContentType = 'x-playwright/missing'; -type AnchorID = string | ((id: string | null) => boolean) | undefined; +export type AnchorID = string | ((id: string) => boolean) | undefined; export function useAnchor(id: AnchorID, onReveal: () => void) { React.useEffect(() => { @@ -123,8 +123,10 @@ export function useAnchor(id: AnchorID, onReveal: () => void) { const listener = () => { const params = new URLSearchParams(window.location.hash.slice(1)); + if (!params.has('anchor')) + return; const anchor = params.get('anchor'); - const isRevealed = typeof id === 'function' ? id(anchor) : anchor === id; + const isRevealed = typeof id === 'function' ? id(anchor!) : anchor === id; if (isRevealed) onReveal(); }; diff --git a/packages/html-reporter/src/testResultView.tsx b/packages/html-reporter/src/testResultView.tsx index 5e7f1499e7..094b47e576 100644 --- a/packages/html-reporter/src/testResultView.tsx +++ b/packages/html-reporter/src/testResultView.tsx @@ -65,7 +65,7 @@ function groupImageDiffs(screenshots: Set): ImageDiff[] { export const TestResultView: React.FC<{ test: TestCase, result: TestResult, -}> = ({ result }) => { +}> = ({ test, result }) => { const { screenshots, videos, traces, otherAttachments, diffs, errors, htmls } = React.useMemo(() => { const attachments = result?.attachments || []; const screenshots = new Set(attachments.filter(a => a.contentType.startsWith('image/'))); @@ -88,7 +88,7 @@ export const TestResultView: React.FC<{ })} } {!!result.steps.length && - {result.steps.map((step, i) => )} + {result.steps.map((step, i) => )} } {diffs.map((diff, index) => @@ -128,11 +128,17 @@ export const TestResultView: React.FC<{ )} } - {!!(otherAttachments.size + htmls.length) && + {!!(otherAttachments.size + htmls.length) && id.startsWith('attachment-')}> {[...htmls].map((a, i) => ( - ) + + + ) + )} + {[...otherAttachments].map((a, i) => + + + )} - {[...otherAttachments].map((a, i) => )} } ; }; @@ -162,21 +168,23 @@ function classifyErrors(testErrors: string[], diffs: ImageDiff[]) { } const StepTreeItem: React.FC<{ + test: TestCase; + result: TestResult; step: TestStep; depth: number, -}> = ({ step, depth }) => { +}> = ({ test, step, result, depth }) => { const attachmentName = step.title.match(/^attach "(.*)"$/)?.[1]; return {msToString(step.duration)} - {attachmentName && { evt.stopPropagation(); }}>{icons.attachment()}} + {attachmentName && { evt.stopPropagation(); }}>{icons.attachment()}} {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) => ); + const children = step.steps.map((s, i) => ); if (step.snippet) - children.unshift(); + children.unshift(); return children; - } : undefined} depth={depth}>; + } : undefined} depth={depth}/>; };