Skip to content

Commit

Permalink
feature(trace-viewer): embedded mode support PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed May 22, 2024
1 parent cfc9623 commit 216aedb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
13 changes: 10 additions & 3 deletions packages/playwright-core/src/cli/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { Command } from '../utilsBundle';
import { program } from '../utilsBundle';
export { program } from '../utilsBundle';
import { runDriver, runServer, printApiJson, launchBrowserServer } from './driver';
import { runTraceInBrowser, runTraceViewerApp } from '../server/trace/viewer/traceViewer';
import { runTraceInBrowser, runTraceViewerApp, runTraceViewerServer } from '../server/trace/viewer/traceViewer';
import type { TraceViewerServerOptions } from '../server/trace/viewer/traceViewer';
import * as playwright from '../..';
import type { BrowserContext } from '../client/browserContext';
Expand Down Expand Up @@ -296,6 +296,7 @@ program
.option('-h, --host <host>', 'Host to serve trace on; specifying this option opens trace in a browser tab')
.option('-p, --port <port>', 'Port to serve trace on, 0 for any free port; specifying this option opens trace in a browser tab')
.option('--stdin', 'Accept trace URLs over stdin to update the viewer')
.option('--server-only', 'Run server only')
.description('show trace viewer')
.action(function(traces, options) {
if (options.browser === 'cr')
Expand All @@ -309,12 +310,18 @@ program
host: options.host,
port: +options.port,
isServer: !!options.stdin,
embedded: !!options.stdin && options.serverOnly,
};

if (options.port !== undefined || options.host !== undefined)
if (openOptions.embedded) {
runTraceViewerServer(traces, openOptions).then(server => {
process.stdout.write(server.urlPrefix('precise') + '\n');
}).catch(logErrorAndExit);
} else if (options.port !== undefined || options.host !== undefined) {
runTraceInBrowser(traces, openOptions).catch(logErrorAndExit);
else
} else {
runTraceViewerApp(traces, options.browser, openOptions, true).catch(logErrorAndExit);
}
}).addHelpText('afterAll', `
Examples:
Expand Down
15 changes: 11 additions & 4 deletions packages/playwright-core/src/server/trace/viewer/traceViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type TraceViewerServerOptions = {
port?: number;
isServer?: boolean;
transport?: Transport;
embedded?: boolean;
};

export type TraceViewerRedirectOptions = {
Expand All @@ -47,6 +48,7 @@ export type TraceViewerRedirectOptions = {
webApp?: string;
isServer?: boolean;
outputDir?: string;
embedded?: boolean;
};

export type TraceViewerAppOptions = {
Expand Down Expand Up @@ -134,6 +136,8 @@ export async function installRootRedirect(server: HttpServer, traceUrls: string[
params.append('outputDir', options.outputDir);
for (const reporter of options.reporter || [])
params.append('reporter', reporter);
if (options.embedded)
params.append('embedded', '');

const urlPath = `./trace/${options.webApp || 'index.html'}?${params.toString()}`;
server.routePath('/', (_, response) => {
Expand All @@ -144,20 +148,23 @@ export async function installRootRedirect(server: HttpServer, traceUrls: string[
});
}

export async function runTraceViewerApp(traceUrls: string[], browserName: string, options: TraceViewerServerOptions & { headless?: boolean }, exitOnClose?: boolean) {
export async function runTraceViewerServer(traceUrls: string[], options: TraceViewerServerOptions & { headless?: boolean }, exitOnClose?: boolean) {
validateTraceUrls(traceUrls);
const server = await startTraceViewerServer(options);
await installRootRedirect(server, traceUrls, options);
return server;
}

export async function runTraceViewerApp(traceUrls: string[], browserName: string, options: TraceViewerServerOptions & { headless?: boolean }, exitOnClose?: boolean) {
const server = await runTraceViewerServer(traceUrls, options);
const page = await openTraceViewerApp(server.urlPrefix('precise'), browserName, options);
if (exitOnClose)
page.on('close', () => gracefullyProcessExitDoNotHang(0));
return page;
}

export async function runTraceInBrowser(traceUrls: string[], options: TraceViewerServerOptions) {
validateTraceUrls(traceUrls);
const server = await startTraceViewerServer(options);
await installRootRedirect(server, traceUrls, options);
const server = await runTraceViewerServer(traceUrls, options);
await openTraceInBrowser(server.urlPrefix('human-readable'));
}

Expand Down
7 changes: 4 additions & 3 deletions packages/trace-viewer/src/ui/workbenchLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const WorkbenchLoader: React.FunctionComponent<{
const [dragOver, setDragOver] = React.useState<boolean>(false);
const [processingErrorMessage, setProcessingErrorMessage] = React.useState<string | null>(null);
const [fileForLocalModeError, setFileForLocalModeError] = React.useState<string | null>(null);
const [embedded, setEmbedded] = React.useState<boolean>(false);

const processTraceFiles = React.useCallback((files: FileList) => {
const blobUrls = [];
Expand Down Expand Up @@ -74,7 +75,7 @@ export const WorkbenchLoader: React.FunctionComponent<{
const params = new URL(window.location.href).searchParams;
const newTraceURLs = params.getAll('trace');
setIsServer(params.has('isServer'));

setEmbedded(params.has('embedded'));
// Don't accept file:// URLs - this means we re opened locally.
for (const url of newTraceURLs) {
if (url.startsWith('file:')) {
Expand Down Expand Up @@ -138,15 +139,15 @@ export const WorkbenchLoader: React.FunctionComponent<{
const showFileUploadDropArea = !!(!isServer && !dragOver && !fileForLocalModeError && (!traceURLs.length || processingErrorMessage));

return <div className='vbox workbench-loader' onDragOver={event => { event.preventDefault(); setDragOver(true); }}>
<div className='hbox header' {...(showFileUploadDropArea ? { inert: 'true' } : {})}>
{!embedded && <div className='hbox header' {...(showFileUploadDropArea ? { inert: 'true' } : {})}>
<div className='logo'>
<img src='playwright-logo.svg' alt='Playwright logo' />
</div>
<div className='product'>Playwright</div>
{model.title && <div className='title'>{model.title}</div>}
<div className='spacer'></div>
<ToolbarButton icon='color-mode' title='Toggle color mode' toggled={false} onClick={() => toggleTheme()}></ToolbarButton>
</div>
</div>}
<div className='progress'>
<div className='inner-progress' style={{ width: progress.total ? (100 * progress.done / progress.total) + '%' : 0 }}></div>
</div>
Expand Down
10 changes: 8 additions & 2 deletions packages/web/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ export function applyTheme() {
document!.defaultView!.addEventListener('blur', event => {
document.body.classList.add('inactive');
}, false);
window.addEventListener('message', ({ data }) => {
if (!data.theme)
return;
if (currentTheme() !== data.theme)
toggleTheme();
}, false);

const currentTheme = settings.getString('theme', 'light-mode');
const theme = settings.getString('theme', 'light-mode');
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
if (currentTheme === 'dark-mode' || prefersDarkScheme.matches)
if (theme === 'dark-mode' || prefersDarkScheme.matches)
document.body.classList.add('dark-mode');
}

Expand Down

0 comments on commit 216aedb

Please sign in to comment.