2021-01-07 16:15:34 -08:00
|
|
|
/**
|
|
|
|
* 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 * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as playwright from '../../..';
|
|
|
|
import * as util from 'util';
|
|
|
|
import { ScreenshotGenerator } from './screenshotGenerator';
|
|
|
|
import { SnapshotRouter } from './snapshotRouter';
|
|
|
|
import { readTraceFile, TraceModel } from './traceModel';
|
2021-01-25 18:44:46 -08:00
|
|
|
import type { ActionTraceEvent, TraceEvent } from '../../trace/traceTypes';
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
|
|
|
|
|
2021-01-20 19:16:23 -08:00
|
|
|
type TraceViewerDocument = {
|
|
|
|
resourcesDir: string;
|
|
|
|
model: TraceModel;
|
|
|
|
snapshotRouter: SnapshotRouter;
|
|
|
|
screenshotGenerator: ScreenshotGenerator;
|
|
|
|
};
|
|
|
|
|
|
|
|
const emptyModel: TraceModel = {
|
|
|
|
contexts: [
|
|
|
|
{
|
|
|
|
startTime: 0,
|
|
|
|
endTime: 1,
|
|
|
|
created: {
|
|
|
|
timestamp: Date.now(),
|
|
|
|
type: 'context-created',
|
|
|
|
browserName: 'none',
|
|
|
|
contextId: '<empty>',
|
|
|
|
deviceScaleFactor: 1,
|
|
|
|
isMobile: false,
|
|
|
|
viewportSize: { width: 800, height: 600 },
|
|
|
|
},
|
|
|
|
destroyed: {
|
|
|
|
timestamp: Date.now(),
|
|
|
|
type: 'context-destroyed',
|
|
|
|
contextId: '<empty>',
|
|
|
|
},
|
|
|
|
name: '<empty>',
|
|
|
|
filePath: '',
|
|
|
|
pages: [],
|
|
|
|
resourcesByUrl: new Map()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2021-01-07 16:15:34 -08:00
|
|
|
class TraceViewer {
|
2021-01-20 19:16:23 -08:00
|
|
|
private _document: TraceViewerDocument | undefined;
|
2021-01-07 16:15:34 -08:00
|
|
|
|
2021-01-20 19:16:23 -08:00
|
|
|
constructor() {
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 19:16:23 -08:00
|
|
|
async load(traceDir: string) {
|
|
|
|
const resourcesDir = path.join(traceDir, 'resources');
|
|
|
|
const model = { contexts: [] };
|
|
|
|
this._document = {
|
|
|
|
model,
|
|
|
|
resourcesDir,
|
|
|
|
snapshotRouter: new SnapshotRouter(resourcesDir),
|
|
|
|
screenshotGenerator: new ScreenshotGenerator(resourcesDir, model),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const name of fs.readdirSync(traceDir)) {
|
|
|
|
if (!name.endsWith('.trace'))
|
|
|
|
continue;
|
|
|
|
const filePath = path.join(traceDir, name);
|
|
|
|
const traceContent = await fsReadFileAsync(filePath, 'utf8');
|
|
|
|
const events = traceContent.split('\n').map(line => line.trim()).filter(line => !!line).map(line => JSON.parse(line)) as TraceEvent[];
|
|
|
|
readTraceFile(events, model, filePath);
|
|
|
|
}
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async show() {
|
|
|
|
const browser = await playwright.chromium.launch({ headless: false });
|
|
|
|
const uiPage = await browser.newPage({ viewport: null });
|
|
|
|
uiPage.on('close', () => process.exit(0));
|
|
|
|
await uiPage.exposeBinding('readFile', async (_, path: string) => {
|
|
|
|
return fs.readFileSync(path).toString();
|
|
|
|
});
|
2021-01-26 20:06:05 +01:00
|
|
|
await uiPage.exposeBinding('readResource', async (_, sha1: string) => {
|
|
|
|
if (!this._document)
|
|
|
|
return;
|
|
|
|
|
|
|
|
return fs.readFileSync(path.join(this._document.resourcesDir, sha1)).toString('base64');
|
|
|
|
});
|
2021-01-26 15:09:17 -08:00
|
|
|
await uiPage.exposeBinding('renderSnapshot', async (_, arg: { action: ActionTraceEvent, snapshot: { snapshotId?: string, snapshotTime?: number } }) => {
|
2021-01-25 18:44:46 -08:00
|
|
|
const { action, snapshot } = arg;
|
2021-01-20 19:16:23 -08:00
|
|
|
if (!this._document)
|
|
|
|
return;
|
2021-01-07 16:15:34 -08:00
|
|
|
try {
|
2021-01-20 19:16:23 -08:00
|
|
|
const contextEntry = this._document.model.contexts.find(entry => entry.created.contextId === action.contextId)!;
|
2021-01-25 18:44:46 -08:00
|
|
|
const pageEntry = contextEntry.pages.find(entry => entry.created.pageId === action.pageId)!;
|
2021-01-26 15:09:17 -08:00
|
|
|
const pageUrl = await this._document.snapshotRouter.selectSnapshot(contextEntry, pageEntry, snapshot.snapshotId, snapshot.snapshotTime);
|
2021-01-07 16:15:34 -08:00
|
|
|
|
|
|
|
// TODO: fix Playwright bug where frame.name is lost (empty).
|
|
|
|
const snapshotFrame = uiPage.frames()[1];
|
|
|
|
try {
|
2021-01-25 18:44:46 -08:00
|
|
|
await snapshotFrame.goto(pageUrl);
|
2021-01-07 16:15:34 -08:00
|
|
|
} catch (e) {
|
|
|
|
if (!e.message.includes('frame was detached'))
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-21 19:00:32 -08:00
|
|
|
const element = await snapshotFrame.$(action.selector || '*[__playwright_target__]').catch(e => undefined);
|
2021-01-07 16:15:34 -08:00
|
|
|
if (element) {
|
|
|
|
await element.evaluate(e => {
|
|
|
|
e.style.backgroundColor = '#ff69b460';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e); // eslint-disable-line no-console
|
|
|
|
}
|
|
|
|
});
|
2021-01-20 19:16:23 -08:00
|
|
|
await uiPage.exposeBinding('getTraceModel', () => this._document ? this._document.model : emptyModel);
|
2021-01-07 16:15:34 -08:00
|
|
|
await uiPage.route('**/*', (route, request) => {
|
2021-01-20 19:16:23 -08:00
|
|
|
if (request.frame().parentFrame() && this._document) {
|
|
|
|
this._document.snapshotRouter.route(route);
|
2021-01-07 16:15:34 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2021-01-25 14:49:51 -08:00
|
|
|
const url = new URL(request.url());
|
2021-01-20 19:16:23 -08:00
|
|
|
if (this._document && request.url().includes('action-preview')) {
|
2021-01-07 16:15:34 -08:00
|
|
|
const fullPath = url.pathname.substring('/action-preview/'.length);
|
|
|
|
const actionId = fullPath.substring(0, fullPath.indexOf('.png'));
|
2021-01-20 19:16:23 -08:00
|
|
|
this._document.screenshotGenerator.generateScreenshot(actionId).then(body => {
|
2021-01-07 16:15:34 -08:00
|
|
|
if (body)
|
|
|
|
route.fulfill({ contentType: 'image/png', body });
|
|
|
|
else
|
|
|
|
route.fulfill({ status: 404 });
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2021-01-21 19:00:32 -08:00
|
|
|
const filePath = path.join(__dirname, 'web', url.pathname.substring(1));
|
2021-01-07 16:15:34 -08:00
|
|
|
const body = fs.readFileSync(filePath);
|
|
|
|
route.fulfill({
|
|
|
|
contentType: extensionToMime[path.extname(url.pathname).substring(1)] || 'text/plain',
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e); // eslint-disable-line no-console
|
|
|
|
route.fulfill({
|
|
|
|
status: 404
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await uiPage.goto('http://trace-viewer/index.html');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:16:23 -08:00
|
|
|
export async function showTraceViewer(traceDir: string) {
|
|
|
|
const traceViewer = new TraceViewer();
|
|
|
|
if (traceDir)
|
|
|
|
await traceViewer.load(traceDir);
|
2021-01-07 16:15:34 -08:00
|
|
|
await traceViewer.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
const extensionToMime: { [key: string]: string } = {
|
|
|
|
'css': 'text/css',
|
|
|
|
'html': 'text/html',
|
|
|
|
'jpeg': 'image/jpeg',
|
|
|
|
'jpg': 'image/jpeg',
|
|
|
|
'js': 'application/javascript',
|
|
|
|
'png': 'image/png',
|
|
|
|
'ttf': 'font/ttf',
|
|
|
|
'svg': 'image/svg+xml',
|
|
|
|
'webp': 'image/webp',
|
|
|
|
'woff': 'font/woff',
|
|
|
|
'woff2': 'font/woff2',
|
|
|
|
};
|