Skip to content

Commit

Permalink
feat(html): link from attachment step to attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Oct 24, 2024
1 parent 3641e59 commit c060923
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/html-reporter/src/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CopyToClipboard } from './copyToClipboard';
import './links.css';
import { linkifyText } from '@web/renderUtils';
import { clsx } from '@web/uiUtils';
import { componentID } from './testResultView';

export function navigate(href: string) {
window.history.pushState({}, '', href);
Expand Down Expand Up @@ -77,7 +78,7 @@ export const AttachmentLink: React.FunctionComponent<{
linkName?: string,
openInNewTab?: boolean,
}> = ({ attachment, href, linkName, openInNewTab }) => {
return <TreeItem title={<span>
return <TreeItem id={componentID(params => params.set('attachment', attachment.name))} title={<span>
{attachment.contentType === kMissingContentType ? icons.warning() : icons.attachment()}
{attachment.path && <a href={href || attachment.path} download={downloadFileNameForAttachment(attachment)}>{linkName || attachment.name}</a>}
{!attachment.path && (
Expand Down
3 changes: 3 additions & 0 deletions packages/html-reporter/src/testCaseView.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ const result: TestResult = {
errors: [],
steps: [{
title: 'Outer step',
category: 'test.step',
startTime: new Date(100).toUTCString(),
duration: 10,
location: { file: 'test.spec.ts', line: 62, column: 0 },
count: 1,
steps: [{
title: 'Inner step',
category: 'test.step',
startTime: new Date(200).toUTCString(),
duration: 10,
location: { file: 'test.spec.ts', line: 82, column: 0 },
Expand Down Expand Up @@ -134,6 +136,7 @@ const resultWithAttachment: TestResult = {
errors: [],
steps: [{
title: 'Outer step',
category: 'test.step',
startTime: new Date(100).toUTCString(),
duration: 10,
location: { file: 'test.spec.ts', line: 62, column: 0 },
Expand Down
11 changes: 10 additions & 1 deletion packages/html-reporter/src/testResultView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { statusIcon } from './statusIcon';
import type { ImageDiff } from '@web/shared/imageDiffView';
import { ImageDiffView } from '@web/shared/imageDiffView';
import { TestErrorView, TestScreenshotErrorView } from './testErrorView';
import * as icons from './icons';
import './testResultView.css';

function groupImageDiffs(screenshots: Set<TestAttachment>): ImageDiff[] {
Expand Down Expand Up @@ -173,12 +174,20 @@ function classifyErrors(testErrors: string[], diffs: ImageDiff[]) {
});
}

export function componentID(cb: (params: URLSearchParams) => void) {
const searchParams = new URLSearchParams(window.location.hash.slice(1));
cb(searchParams);
return '?' + searchParams;
}

const StepTreeItem: React.FC<{
step: TestStep;
depth: number,
}> = ({ step, depth }) => {
return <TreeItem title={<span>
const attachmentName = step.category === 'attach' ? step.title.match(/^attach "(.*)"$/)?.[1] : undefined;
return <TreeItem title={<span aria-label={step.title}>
<span style={{ float: 'right' }}>{msToString(step.duration)}</span>
{attachmentName && <a style={{ float: 'right' }} title='link to attachment' href={'#' + componentID(params => params.set('attachment', attachmentName))} onClick={(evt) => { evt.stopPropagation(); }}>{icons.attachment()}</a>}

Check failure on line 190 in packages/html-reporter/src/testResultView.tsx

View workflow job for this annotation

GitHub Actions / docs & lint

Unexpected parentheses around single function argument
{statusIcon(step.error || step.duration === -1 ? 'failed' : 'passed')}
<span>{step.title}</span>
{step.count > 1 && <><span className='test-result-counter'>{step.count}</span></>}
Expand Down
5 changes: 5 additions & 0 deletions packages/html-reporter/src/treeItem.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
cursor: pointer;
}

.tree-item:target > .tree-item-title {
text-decoration: underline var(--color-underlinenav-icon);
text-decoration-thickness: 1.5px;
}

.tree-item-body {
min-height: 18px;
}
5 changes: 3 additions & 2 deletions packages/html-reporter/src/treeItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ export const TreeItem: React.FunctionComponent<{
depth: number,
selected?: boolean,
style?: React.CSSProperties,
}> = ({ title, loadChildren, onClick, expandByDefault, depth, selected, style }) => {
id?: string,
}> = ({ title, loadChildren, onClick, expandByDefault, depth, selected, style, id }) => {
const [expanded, setExpanded] = React.useState(expandByDefault || false);
const className = selected ? 'tree-item-title selected' : 'tree-item-title';
return <div className={'tree-item'} style={style}>
return <div className={'tree-item'} id={id} style={style}>
<span className={className} style={{ whiteSpace: 'nowrap', paddingLeft: depth * 22 + 4 }} onClick={() => { onClick?.(); setExpanded(!expanded); }} >
{loadChildren && !!expanded && icons.downArrow()}
{loadChildren && !expanded && icons.rightArrow()}
Expand Down
1 change: 1 addition & 0 deletions packages/html-reporter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type TestResult = {

export type TestStep = {
title: string;
category: 'hook' | 'fixture' | 'test.step' | 'expect' | 'attach' | string;
startTime: string;
duration: number;
location?: Location;
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/src/reporters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ class HtmlBuilder {
const { step, duration, count } = dedupedStep;
const result: TestStep = {
title: step.title,
category: step.category,
startTime: step.startTime.toISOString(),
duration,
steps: dedupeSteps(step.steps).map(s => this._createTestStep(s)),
Expand Down
23 changes: 22 additions & 1 deletion tests/playwright-test/reporter-html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ for (const useIntermediateMergeReport of [true, false] as const) {
'a.test.js': `
import { test, expect } from '@playwright/test';
test('passing', async ({ page }, testInfo) => {
testInfo.attach('axe-report.html', {
await testInfo.attach('axe-report.html', {
contentType: 'text/html',
body: '<h1>Axe Report</h1>',
});
Expand Down Expand Up @@ -914,6 +914,27 @@ for (const useIntermediateMergeReport of [true, false] as const) {
]));
});

test('should link from attach step to attachment view', async ({ runInlineTest, page, showReport }) => {
const result = await runInlineTest({
'a.test.js': `
import { test, expect } from '@playwright/test';
test('passing', async ({ page }, testInfo) => {
await testInfo.attach('foo', { body: 'bar' });
});
`,
}, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' });
expect(result.exitCode).toBe(0);

await showReport();
await page.getByRole('link', { name: 'passing' }).click();
await page.getByLabel('attach "foo"').getByTitle('link to attachment').click();

await page.waitForURL(url => {
const navState = new URLSearchParams(url.hash.slice(1));
return navState.get('attachment') === 'foo';
});
});

test('should strikethrough textual diff', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({
'helper.ts': `
Expand Down

0 comments on commit c060923

Please sign in to comment.