Skip to content

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

feat(html): add view trace button to top of test view (#36520)

Adam Gastineau06c359c85f51
diff --git a/packages/html-reporter/src/links.css b/packages/html-reporter/src/links.css
index 4abe8a6caa..29dc2fc0c6 100644
--- a/packages/html-reporter/src/links.css
+++ b/packages/html-reporter/src/links.css
@@ -110,3 +110,39 @@
right: 5px;
top: 5px;
}
+
+.link-badge {
+ flex: none;
+ background-color: transparent;
+ border-color: transparent;
+}
+
+.link-badge-dim span {
+ color: var(--color-fg-muted);
+}
+
+.link-badge:hover {
+ cursor: pointer;
+}
+
+.link-badge svg {
+ fill: var(--color-fg-default);
+}
+
+.link-badge-dim svg {
+ fill: var(--color-fg-muted);
+}
+
+.link-badge-dim:hover svg {
+ fill: var(--color-fg-muted);
+}
+
+.trace-link {
+ /* Trace link button has 1px border and 4px padding, so we need 3px right margin to match content on the other side of divider */
+ margin-right: 3px;
+}
+
+.trace-link-separator {
+ color: var(--color-fg-muted);
+ user-select: none;
+}
diff --git a/packages/html-reporter/src/links.tsx b/packages/html-reporter/src/links.tsx
index f0054fb501..4614a22da8 100644
--- a/packages/html-reporter/src/links.tsx
+++ b/packages/html-reporter/src/links.tsx
@@ -22,6 +22,7 @@ import { CopyToClipboard } from './copyToClipboard';
import './links.css';
import { linkifyText } from '@web/renderUtils';
import { clsx, useFlash } from '@web/uiUtils';
+import { trace } from './icons';
export function navigate(href: string | URL) {
window.history.pushState({}, '', href);
@@ -37,14 +38,15 @@ export const Route: React.FunctionComponent<{
return predicate(searchParams) ? children : null;
};
-export const Link: React.FunctionComponent<{
+type LinkProps = React.PropsWithChildren<{
href?: string,
click?: string,
ctrlClick?: string,
className?: string,
title?: string,
- children: any,
-}> = ({ click, ctrlClick, children, ...rest }) => {
+}>;
+
+export const Link: React.FunctionComponent<LinkProps> = ({ click, ctrlClick, children, ...rest }) => {
return <a {...rest} style={{ textDecoration: 'none', color: 'var(--color-fg-default)', cursor: 'pointer' }} onClick={e => {
if (click) {
e.preventDefault();
@@ -53,6 +55,8 @@ export const Link: React.FunctionComponent<{
}}>{children}</a>;
};
+export const LinkBadge: React.FunctionComponent<LinkProps & { dim?: boolean }> = ({ className, ...props }) => <Link {...props} className={clsx('link-badge', props.dim && 'link-badge-dim', className)} />;
+
export const ProjectLink: React.FunctionComponent<{
projectNames: string[],
projectName: string,
@@ -100,6 +104,26 @@ export const AttachmentLink: React.FunctionComponent<{
} : undefined} depth={0} style={{ lineHeight: '32px' }} flash={flash}></TreeItem>;
};
+export const TraceLink: React.FC<{ test: TestCaseSummary, trailingSeparator?: boolean, dim?: boolean }> = ({ test, trailingSeparator, dim }) => {
+ const firstTraces = test.results.map(result => result.attachments.filter(attachment => attachment.name === 'trace')).filter(traces => traces.length > 0)[0];
+ if (!firstTraces)
+ return undefined;
+
+ return (
+ <>
+ <LinkBadge
+ href={generateTraceUrl(firstTraces)}
+ title='View Trace'
+ className='button trace-link'
+ dim={dim}>
+ {trace()}
+ <span>View Trace</span>
+ </LinkBadge>
+ {trailingSeparator && <div className='trace-link-separator'>|</div>}
+ </>
+ );
+};
+
export const SearchParamsContext = React.createContext<URLSearchParams>(new URLSearchParams(window.location.hash.slice(1)));
export const SearchParamsProvider: React.FunctionComponent<React.PropsWithChildren> = ({ children }) => {
diff --git a/packages/html-reporter/src/testCaseView.css b/packages/html-reporter/src/testCaseView.css
index b070503668..6ee692d6fd 100644
--- a/packages/html-reporter/src/testCaseView.css
+++ b/packages/html-reporter/src/testCaseView.css
@@ -39,7 +39,6 @@
flex: none;
align-items: center;
padding: 0 8px 8px;
- line-height: 24px;
}
.test-case-run-duration {
diff --git a/packages/html-reporter/src/testCaseView.tsx b/packages/html-reporter/src/testCaseView.tsx
index d80049ca33..68125e19f9 100644
--- a/packages/html-reporter/src/testCaseView.tsx
+++ b/packages/html-reporter/src/testCaseView.tsx
@@ -20,7 +20,7 @@ import * as React from 'react';
import { TabbedPane } from './tabbedPane';
import { AutoChip } from './chip';
import './common.css';
-import { Link, ProjectLink, SearchParamsContext, testResultHref } from './links';
+import { Link, ProjectLink, SearchParamsContext, testResultHref, TraceLink } from './links';
import { statusIcon } from './statusIcon';
import './testCaseView.css';
import { TestResultView } from './testResultView';
@@ -56,13 +56,14 @@ export const TestCaseView: React.FC<{
<div className={clsx(!next && 'hidden')}><Link href={testResultHref({ test: next }) + filterParam}>next »</Link></div>
</>}
/>
- <div className='hbox'>
+ <div className='hbox' style={{ lineHeight: '24px' }}>
<div className='test-case-location'>
<CopyToClipboardContainer value={`${test.location.file}:${test.location.line}`}>
{test.location.file}:{test.location.line}
</CopyToClipboardContainer>
</div>
<div style={{ flex: 'auto' }}></div>
+ <TraceLink test={test} trailingSeparator={true} />
<div className='test-case-duration'>{msToString(test.duration)}</div>
</div>
{(!!test.projectName || labels) && <div className='test-case-project-labels-row'>
diff --git a/packages/html-reporter/src/testFileView.css b/packages/html-reporter/src/testFileView.css
index 489cce61ba..c071781ce1 100644
--- a/packages/html-reporter/src/testFileView.css
+++ b/packages/html-reporter/src/testFileView.css
@@ -51,28 +51,6 @@
margin-right: 10px;
}
-.test-file-badge {
- flex: none;
- background-color: transparent;
- border-color: transparent;
-}
-
-.test-file-badge span {
- color: var(--color-fg-muted);
-}
-
-.test-file-badge:hover {
- cursor: pointer;
-}
-
-.test-file-badge svg {
- fill: var(--color-fg-muted);
-}
-
-.test-file-badge:hover svg {
- fill: var(--color-fg-muted);
-}
-
.test-file-test-outcome-skipped {
color: var(--color-fg-muted);
}
diff --git a/packages/html-reporter/src/testFileView.tsx b/packages/html-reporter/src/testFileView.tsx
index 24e9011219..2086272bb8 100644
--- a/packages/html-reporter/src/testFileView.tsx
+++ b/packages/html-reporter/src/testFileView.tsx
@@ -19,10 +19,10 @@ import * as React from 'react';
import { hashStringToInt, msToString } from './utils';
import { Chip } from './chip';
import { filterWithToken } from './filter';
-import { generateTraceUrl, Link, navigate, ProjectLink, SearchParamsContext, testResultHref } from './links';
+import { Link, LinkBadge, navigate, ProjectLink, SearchParamsContext, testResultHref, TraceLink } from './links';
import { statusIcon } from './statusIcon';
import './testFileView.css';
-import { video, image, trace } from './icons';
+import { video, image } from './icons';
import { clsx } from '@web/uiUtils';
export const TestFileView: React.FC<React.PropsWithChildren<{
@@ -64,7 +64,7 @@ export const TestFileView: React.FC<React.PropsWithChildren<{
</Link>
{imageDiffBadge(test)}
{videoBadge(test)}
- {traceBadge(test)}
+ <TraceLink test={test} dim={true} />
</div>
</div>
)}
@@ -75,28 +75,14 @@ function imageDiffBadge(test: TestCaseSummary): JSX.Element | undefined {
for (const result of test.results) {
for (const attachment of result.attachments) {
if (attachment.contentType.startsWith('image/') && !!attachment.name.match(/-(expected|actual|diff)/))
- return <Link href={testResultHref({ test, result, anchor: `attachment-${result.attachments.indexOf(attachment)}` })} title='View images' className='test-file-badge'>{image()}</Link>;
+ return <LinkBadge href={testResultHref({ test, result, anchor: `attachment-${result.attachments.indexOf(attachment)}` })} title='View images' dim={true}>{image()}</LinkBadge>;
}
}
}
function videoBadge(test: TestCaseSummary): JSX.Element | undefined {
const resultWithVideo = test.results.find(result => result.attachments.some(attachment => attachment.name === 'video'));
- return resultWithVideo ? <Link href={testResultHref({ test, result: resultWithVideo, anchor: 'attachment-video' })} title='View video' className='test-file-badge'>{video()}</Link> : undefined;
-}
-
-function traceBadge(test: TestCaseSummary): JSX.Element | undefined {
- const firstTraces = test.results.map(result => result.attachments.filter(attachment => attachment.name === 'trace')).filter(traces => traces.length > 0)[0];
- if (!firstTraces)
- return undefined;
-
- return <Link
- href={generateTraceUrl(firstTraces)}
- title='View Trace'
- className='button test-file-badge'>
- {trace()}
- <span>View Trace</span>
- </Link>;
+ return resultWithVideo ? <LinkBadge href={testResultHref({ test, result: resultWithVideo, anchor: 'attachment-video' })} title='View video' dim={true}>{video()}</LinkBadge> : undefined;
}
const LabelsClickView: React.FC<React.PropsWithChildren<{
diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts
index 6b525885bc..aff477ed2e 100644
--- a/tests/playwright-test/reporter-html.spec.ts
+++ b/tests/playwright-test/reporter-html.spec.ts
@@ -431,7 +431,7 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await expect(page.locator('video').locator('source')).toHaveAttribute('src', /(https:\/\/some-url\.com\/)[^/\s]+?\.[^/\s]+/);
await expect(page.getByRole('link', { name: 'video' })).toHaveAttribute('href', /(https:\/\/some-url\.com\/)[^/\s]+?\.[^/\s]+/);
- await expect(page.getByRole('link', { name: 'trace' })).toHaveAttribute('href', /(https:\/\/some-url\.com\/)[^/\s]+?\.[^/\s]+/);
+ await expect(page.getByRole('link', { name: 'trace', exact: true })).toHaveAttribute('href', /(https:\/\/some-url\.com\/)[^/\s]+?\.[^/\s]+/);
await expect(page.locator('div').filter({ hasText: /^Tracestrace$/ }).getByRole('link').first()).toHaveAttribute('href', /trace=(https:\/\/some-url\.com\/)[^/\s]+?\.[^/\s]+/);
});
@@ -515,6 +515,27 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await expect(page.locator('.header-title')).toHaveText('Omega Star Test Suite (Version: abcde)');
});
+ test('should include view trace button in header', async ({ runInlineTest, server, page, showReport }) => {
+ const result = await runInlineTest({
+ 'playwright.config.js': `
+ module.exports = { use: { trace: 'on' } };
+ `,
+ 'a.test.js': `
+ import { test, expect } from '@playwright/test';
+ test('passes', async ({ playwright, page }) => {
+ await page.evaluate('2 + 2');
+ const request = await playwright.request.newContext();
+ await request.get('${server.EMPTY_PAGE}');
+ await request.dispose();
+ });
+ `,
+ }, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
+ await showReport();
+ await expect(page.getByRole('link', { name: 'View Trace' })).toBeVisible();
+ });
+
test('should include stdio', async ({ runInlineTest, page, showReport }) => {
const result = await runInlineTest({
'a.test.js': `
@@ -664,7 +685,7 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await page.getByRole('link', { name: 'passes' }).click();
// Expect one image-link to trace viewer and 2 separate download links
await expect(page.locator('img')).toHaveCount(1);
- await expect(page.locator('a', { hasText: 'trace' })).toHaveText(['trace']);
+ await expect(page.getByRole('link', { name: 'trace', exact: true })).toBeVisible();
await page.click('img');
await page.click('.action-title >> text=EVALUATE');