Skip to content

Commit 221c73b

Browse files
committed
fix(trace-viewer): improve error dialog and command copy functionality
1 parent 2c95eb1 commit 221c73b

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

packages/trace-viewer/index.html

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,65 @@
1616
<!DOCTYPE html>
1717
<html lang="en" translate="no">
1818
<head>
19-
<meta charset="UTF-8">
20-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
21-
<link rel="icon" href="/playwright-logo.svg" type="image/svg+xml">
22-
<link rel="manifest" href="/manifest.webmanifest">
19+
<meta charset="UTF-8" />
20+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
21+
<link rel="icon" href="/playwright-logo.svg" type="image/svg+xml" />
22+
<link rel="manifest" href="/manifest.webmanifest" />
2323
<title>Playwright Trace Viewer</title>
2424
</head>
2525
<body>
2626
<div id="root"></div>
2727
<script type="module" src="/src/index.tsx"></script>
2828
<dialog id="fallback-error">
29-
<p>The Playwright Trace Viewer must be loaded over the <code>http://</code> or <code>https://</code> protocols.</p>
30-
<p>For more information, please see the <a href="https://aka.ms/playwright/trace-viewer-file-protocol">docs</a>.</p>
29+
<p>
30+
The Playwright Trace Viewer must be loaded over the
31+
<code>http://</code> or <code>https://</code> protocols.
32+
</p>
33+
<p>
34+
For more information, please see the
35+
<a href="https://aka.ms/playwright/trace-viewer-file-protocol">docs</a>.
36+
</p>
3137
</dialog>
3238
<script>
3339
if (!/^https?:/.test(window.location.protocol)) {
34-
const fallbackErrorDialog = document.getElementById('fallback-error');
35-
const isTraceViewerInsidePlaywrightReport = window.location.protocol === 'file:' && window.location.pathname.endsWith('/trace/index.html');
36-
// Best-effort to show the report path in the dialog.
40+
const fallbackErrorDialog = document.getElementById("fallback-error");
41+
42+
const isTraceViewerInsidePlaywrightReport =
43+
window.location.protocol === "file:" &&
44+
decodeURIComponent(window.location.pathname).endsWith(
45+
"/trace/index.html"
46+
);
47+
3748
if (isTraceViewerInsidePlaywrightReport) {
49+
const basePathname = decodeURIComponent(window.location.pathname);
50+
const base = basePathname.replace(/\/trace\/index\.html$/, "");
51+
3852
const reportPath = (() => {
39-
const base = window.location.pathname.replace(/\/trace\/index\.html$/, '');
40-
if (navigator.platform === 'Win32')
41-
return base.replace(/^\//, '').replace(/\//g, '\\\\');
53+
if (navigator.platform === "Win32") {
54+
return base.replace(/^\//, "").replace(/\//g, "\\");
55+
}
4256
return base;
4357
})();
44-
const reportLink = document.createElement('div');
45-
const command = `npx playwright show-report ${reportPath}`;
46-
reportLink.innerHTML = `You can open the report via <code>${command}</code> from your Playwright project. <button type="button">Copy Command</button>`;
47-
fallbackErrorDialog.insertBefore(reportLink, fallbackErrorDialog.children[1]);
48-
reportLink.querySelector('button').addEventListener('click', () => navigator.clipboard.writeText(command));
58+
59+
const command = `npx playwright show-report "${reportPath}"`;
60+
61+
const reportLink = document.createElement("div");
62+
reportLink.innerHTML =
63+
`You can open the report via <code>${command}</code> from your Playwright project. ` +
64+
`<button type="button">Copy Command</button>`;
65+
fallbackErrorDialog.insertBefore(
66+
reportLink,
67+
fallbackErrorDialog.children[1]
68+
);
69+
70+
const copyBtn = reportLink.querySelector("button");
71+
if (copyBtn) {
72+
copyBtn.addEventListener("click", () =>
73+
navigator.clipboard.writeText(command)
74+
);
75+
}
4976
}
77+
5078
fallbackErrorDialog.show();
5179
}
5280
</script>

0 commit comments

Comments
 (0)