Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(html): preserve query params in relative links (#38042)

Dmitry Gozmanc3aa5f81cfe7
diff --git a/packages/html-reporter/src/labels.tsx b/packages/html-reporter/src/labels.tsx
index bbac8d7cb0..eb8d593ff9 100644
--- a/packages/html-reporter/src/labels.tsx
+++ b/packages/html-reporter/src/labels.tsx
@@ -16,7 +16,7 @@
import * as React from 'react';
import { clsx } from '@web/uiUtils';
-import { hashStringToInt } from './utils';
+import { formatUrl, hashStringToInt } from './utils';
import { navigate, ProjectLink, SearchParamsContext } from './links';
import { filterWithQuery } from './filter';
import './labels.css';
@@ -33,7 +33,7 @@ export const Label: React.FC<{
</span>;
return href
- ? <a className='label-anchor' href={href}>{baseLabel}</a>
+ ? <a className='label-anchor' href={formatUrl(href)}>{baseLabel}</a>
: baseLabel;
};
diff --git a/packages/html-reporter/src/links.tsx b/packages/html-reporter/src/links.tsx
index e9cbaa1b75..26f895ba0c 100644
--- a/packages/html-reporter/src/links.tsx
+++ b/packages/html-reporter/src/links.tsx
@@ -25,6 +25,7 @@ import { trace } from './icons';
import { Expandable } from './expandable';
import { Label } from './labels';
import { filterWithQuery } from './filter';
+import { formatUrl } from './utils';
export function navigate(href: string | URL) {
window.history.pushState({}, '', href);
@@ -52,7 +53,7 @@ export const Link: React.FunctionComponent<LinkProps> = ({ click, ctrlClick, chi
return <a {...rest} style={{ textDecoration: 'none', color: 'var(--color-fg-default)', cursor: 'pointer' }} onClick={e => {
if (click) {
e.preventDefault();
- navigate(e.metaKey || e.ctrlKey ? ctrlClick || click : click);
+ navigate(formatUrl(e.metaKey || e.ctrlKey ? ctrlClick || click : click));
}
}}>{children}</a>;
};
@@ -84,8 +85,8 @@ export const AttachmentLink: React.FunctionComponent<{
{attachment.contentType === kMissingContentType ? icons.warning() : icons.attachment()}
{attachment.path && (
openInNewTab
- ? <a href={href || attachment.path} target='_blank' rel='noreferrer'>{linkName || attachment.name}</a>
- : <a href={href || attachment.path} download={downloadFileNameForAttachment(attachment)}>{linkName || attachment.name}</a>
+ ? <a href={formatUrl(href || attachment.path)} target='_blank' rel='noreferrer'>{linkName || attachment.name}</a>
+ : <a href={formatUrl(href || attachment.path)} download={downloadFileNameForAttachment(attachment)}>{linkName || attachment.name}</a>
)}
{!attachment.path && (
openInNewTab
@@ -137,7 +138,7 @@ export const TraceLink: React.FC<{ test: TestCaseSummary, trailingSeparator?: bo
return (
<>
<LinkBadge
- href={generateTraceUrl(firstTraces)}
+ href={formatUrl(generateTraceUrl(firstTraces))}
title='View Trace'
className='button trace-link'
dim={dim}>
diff --git a/packages/html-reporter/src/metadataView.tsx b/packages/html-reporter/src/metadataView.tsx
index 11077b9a04..2552aea0b5 100644
--- a/packages/html-reporter/src/metadataView.tsx
+++ b/packages/html-reporter/src/metadataView.tsx
@@ -24,6 +24,7 @@ import type { CIInfo, GitCommitInfo, MetadataWithCommitInfo } from '@testIsomorp
import { CopyToClipboardContainer } from './copyToClipboard';
import { linkifyText } from '@web/renderUtils';
import { SearchParamsContext } from './links';
+import { formatUrl } from './utils';
class ErrorBoundary extends React.Component<React.PropsWithChildren<{}>, { error: Error | null, errorInfo: React.ErrorInfo | null }> {
override state: { error: Error | null, errorInfo: React.ErrorInfo | null } = {
@@ -90,7 +91,7 @@ const CiInfoView: React.FC<{ info: CIInfo }> = ({ info }) => {
const link = info.prHref || info.commitHref;
return <div className='metadata-section' role='list'>
<div role='listitem'>
- <a href={link} target='_blank' rel='noopener noreferrer' title={title}>{title}</a>
+ <a href={formatUrl(link)} target='_blank' rel='noopener noreferrer' title={title}>{title}</a>
</div>
</div>;
};
@@ -105,7 +106,7 @@ const GitCommitInfoView: React.FC<{ ci?: CIInfo, commit: GitCommitInfo }> = ({ c
return <div className='metadata-section' role='list'>
<div role='listitem'>
- {link && <a href={link} target='_blank' rel='noopener noreferrer' title={title}>{title}</a>}
+ {link && <a href={formatUrl(link)} target='_blank' rel='noopener noreferrer' title={title}>{title}</a>}
{!link && <span title={title}>{title}</span>}
</div>
<div role='listitem' className='hbox'>
diff --git a/packages/html-reporter/src/testResultView.tsx b/packages/html-reporter/src/testResultView.tsx
index 1b5d7c6c87..005b7d8f7c 100644
--- a/packages/html-reporter/src/testResultView.tsx
+++ b/packages/html-reporter/src/testResultView.tsx
@@ -17,7 +17,7 @@
import type { HTMLReportOptions, TestAttachment, TestCase, TestResult, TestStep } from './types';
import * as React from 'react';
import { TreeItem } from './treeItem';
-import { msToString } from './utils';
+import { formatUrl, msToString } from './utils';
import { AutoChip } from './chip';
import { traceImage } from './images';
import { Anchor, AttachmentLink, generateTraceUrl, testResultHref } from './links';
@@ -144,8 +144,8 @@ export const TestResultView: React.FC<{
{!!screenshots.length && <AutoChip header='Screenshots' revealOnAnchorId={screenshotAnchors}>
{screenshots.map((a, i) => {
return <Anchor key={`screenshot-${i}`} id={`attachment-${result.attachments.indexOf(a)}`}>
- <a href={a.path}>
- <img className='screenshot' src={a.path} />
+ <a href={formatUrl(a.path)}>
+ <img className='screenshot' src={formatUrl(a.path)} />
</a>
<AttachmentLink attachment={a} result={result}></AttachmentLink>
</Anchor>;
@@ -154,7 +154,7 @@ export const TestResultView: React.FC<{
{!!traces.length && <Anchor id='attachment-trace'><AutoChip header='Traces' revealOnAnchorId='attachment-trace'>
{<div>
- <a href={generateTraceUrl(traces)}>
+ <a href={formatUrl(generateTraceUrl(traces))}>
<img className='screenshot' src={traceImage} style={{ width: 192, height: 117, marginLeft: 20 }} />
</a>
{traces.map((a, i) => <AttachmentLink key={`trace-${i}`} attachment={a} result={result} linkName={traces.length === 1 ? 'trace' : `trace-${i + 1}`}></AttachmentLink>)}
@@ -164,7 +164,7 @@ export const TestResultView: React.FC<{
{!!videos.length && <Anchor id='attachment-video'><AutoChip header='Videos' revealOnAnchorId='attachment-video'>
{videos.map(a => <div key={a.path}>
<video controls>
- <source src={a.path} type={a.contentType}/>
+ <source src={formatUrl(a.path)} type={a.contentType}/>
</video>
<AttachmentLink attachment={a} result={result}></AttachmentLink>
</div>)}
@@ -195,7 +195,13 @@ const StepTreeItem: React.FC<{
}> = ({ test, step, result, depth }) => {
return <TreeItem title={<span aria-label={step.title}>
<span style={{ float: 'right' }}>{msToString(step.duration)}</span>
- {step.attachments.length > 0 && <a style={{ float: 'right' }} title={`reveal attachment`} href={testResultHref({ test, result, anchor: `attachment-${step.attachments[0]}` })} onClick={evt => { evt.stopPropagation(); }}>{icons.attachment()}</a>}
+ {step.attachments.length > 0 && <a
+ style={{ float: 'right' }}
+ title={`reveal attachment`}
+ href={formatUrl(testResultHref({ test, result, anchor: `attachment-${step.attachments[0]}` }))}
+ onClick={evt => { evt.stopPropagation(); }}>
+ {icons.attachment()}
+ </a>}
{statusIcon(step.error || step.duration === -1 ? 'failed' : (step.skipped ? 'skipped' : 'passed'))}
<span>{step.title}</span>
{step.count > 1 && <> ✕ <span className='test-result-counter'>{step.count}</span></>}
diff --git a/packages/html-reporter/src/utils.ts b/packages/html-reporter/src/utils.ts
index eec765969b..ebeb93d285 100644
--- a/packages/html-reporter/src/utils.ts
+++ b/packages/html-reporter/src/utils.ts
@@ -47,3 +47,23 @@ export function hashStringToInt(str: string) {
hash = str.charCodeAt(i) + ((hash << 8) - hash);
return Math.abs(hash % 6);
}
+
+// Preserves any query parameters that could be used for serving the report from storage,
+// or for anything else. We don't use any query params in the report anyway.
+export function formatUrl(url: string): string;
+export function formatUrl(url: string | undefined): string | undefined;
+export function formatUrl(url: string | undefined): string | undefined {
+ if (!url)
+ return url;
+ try {
+ const parsed = new URL(url, window.location.href);
+ if (parsed.origin === window.location.origin) {
+ for (const [key, value] of new URLSearchParams(window.location.search))
+ parsed.searchParams.append(key, value);
+ return parsed.toString();
+ }
+ return url;
+ } catch {
+ return url;
+ }
+}