1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233/*
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, TestCaseSummary, TestResult, TestResultSummary } from './types';
import * as React from 'react';
import * as icons from './icons';
import { CopyToClipboard } from './copyToClipboard';
import './links.css';
import { linkifyText } from '@web/renderUtils';
import { clsx, useFlash } from '@web/uiUtils';
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);
const navEvent = new PopStateEvent('popstate');
window.dispatchEvent(navEvent);
}
export const Route: React.FunctionComponent<{
predicate: (params: URLSearchParams) => boolean,
children: any
}> = ({ predicate, children }) => {
return predicate(useSearchParams()) ? children : null;
};
type LinkProps = React.PropsWithChildren<{
href?: string,
click?: string,
ctrlClick?: string,
className?: string,
title?: string,
}>;
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();
navigate(formatUrl(e.metaKey || e.ctrlKey ? ctrlClick || click : click));
}
}}>{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,
}> = ({ projectNames, projectName }) => {
const searchParams = new URLSearchParams(useSearchParams());
if (searchParams.has('testId'))
searchParams.delete('speedboard');
searchParams.delete('testId');
return <Link click={filterWithQuery(searchParams, `p:${projectName}`, false)} ctrlClick={filterWithQuery(searchParams, `p:${projectName}`, true)}>
<Label label={projectName} colorIndex={projectNames.indexOf(projectName) % 6} />
</Link>;
};
export const AttachmentLink: React.FunctionComponent<{
attachment: TestAttachment,
result: TestResult,
href?: string,
linkName?: string,
openInNewTab?: boolean,
}> = ({ attachment, result, href, linkName, openInNewTab }) => {
const [flash, triggerFlash] = useFlash();
useAnchor('attachment-' + result.attachments.indexOf(attachment), triggerFlash);
const summaryContent = (
<span>
{attachment.contentType === kMissingContentType ? icons.warning() : icons.attachment()}
{attachment.path && (
openInNewTab
? <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
? (
<a
href={URL.createObjectURL(new Blob([attachment.body!], { type: attachment.contentType }))}
target='_blank' rel='noreferrer'
onClick={e => e.stopPropagation() /* dont expand the details */}
>
{attachment.name}
</a>
)
: <span>{linkifyText(attachment.name)}</span>
)}
</span>
);
if (!attachment.body) {
return (
<div
style={{ lineHeight: '32px', whiteSpace: 'nowrap', paddingLeft: 4 }}
className={clsx(flash && 'attachment-flash')}
>
<span style={{ visibility: 'hidden' }}>{icons.rightArrow()}</span>
{summaryContent}
</div>
);
}
return (
<Expandable
style={{ lineHeight: '32px' }}
className={clsx(flash && 'attachment-flash')}
summary={summaryContent}
>
<div className='attachment-body'>
<CopyToClipboard value={attachment.body!}/>
{linkifyText(attachment.body!)}
</div>
</Expandable>
);
};
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={formatUrl(generateTraceUrl(firstTraces))}
title='View Trace'
className='button trace-link'
dim={dim}>
{trace()}
<span>View Trace</span>
</LinkBadge>
{trailingSeparator && <div className='trace-link-separator'>|</div>}
</>
);
};
const SearchParamsContext = React.createContext<URLSearchParams>(new URLSearchParams(window.location.hash.slice(1)));
// Note: make sure you are not mutating the returned URLSearchParams object.
export function useSearchParams(): URLSearchParams {
return React.useContext(SearchParamsContext);
}
export const SearchParamsProvider: React.FunctionComponent<React.PropsWithChildren> = ({ children }) => {
const [searchParams, setSearchParams] = React.useState<URLSearchParams>(new URLSearchParams(window.location.hash.slice(1)));
React.useEffect(() => {
const listener = () => setSearchParams(new URLSearchParams(window.location.hash.slice(1)));
window.addEventListener('popstate', listener);
return () => window.removeEventListener('popstate', listener);
}, []);
return <SearchParamsContext.Provider value={searchParams}>{children}</SearchParamsContext.Provider>;
};
function downloadFileNameForAttachment(attachment: TestAttachment): string {
if (attachment.name.includes('.') || !attachment.path)
return attachment.name;
const firstDotIndex = attachment.path.indexOf('.');
if (firstDotIndex === -1)
return attachment.name;
return attachment.name + attachment.path.slice(firstDotIndex, attachment.path.length);
}
export function generateTraceUrl(traces: TestAttachment[]) {
return `trace/index.html?${traces.map((a, i) => `trace=${new URL(a.path!, window.location.href)}`).join('&')}`;
}
const kMissingContentType = 'x-playwright/missing';
export type AnchorID = string | string[] | ((id: string) => boolean) | undefined;
export function useAnchor(id: AnchorID, onReveal: React.EffectCallback) {
const searchParams = useSearchParams();
const isAnchored = useIsAnchored(id);
React.useEffect(() => {
if (isAnchored)
return onReveal();
}, [isAnchored, onReveal, searchParams]);
}
export function useIsAnchored(id: AnchorID) {
const anchor = useSearchParams().get('anchor');
if (anchor === null)
return false;
if (typeof id === 'undefined')
return false;
if (typeof id === 'string')
return id === anchor;
if (Array.isArray(id))
return id.includes(anchor);
return id(anchor);
}
export function Anchor({ id, children }: React.PropsWithChildren<{ id: AnchorID }>) {
const ref = React.useRef<HTMLDivElement>(null);
const onAnchorReveal = React.useCallback(() => {
ref.current?.scrollIntoView({ block: 'start', inline: 'start' });
}, []);
useAnchor(id, onAnchorReveal);
return <div ref={ref}>{children}</div>;
}
export function testResultHref({ test, result, anchor }: { test?: TestCase | TestCaseSummary, result?: TestResult | TestResultSummary, anchor?: string }, searchParams: URLSearchParams) {
const params = new URLSearchParams(searchParams);
if (test)
params.set('testId', test.testId);
if (test && result)
params.set('run', '' + test.results.indexOf(result as any));
if (anchor)
params.set('anchor', anchor);
return `#?` + params;
}