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 { readTraceFile, TraceModel } from './traceModel';
|
2021-01-27 19:42:51 -08:00
|
|
|
import type { TraceEvent } from '../../trace/traceTypes';
|
|
|
|
import { SnapshotServer } from './snapshotServer';
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
async load(traceDir: string) {
|
|
|
|
const resourcesDir = path.join(traceDir, 'resources');
|
|
|
|
const model = { contexts: [] };
|
|
|
|
this._document = {
|
|
|
|
model,
|
|
|
|
resourcesDir,
|
|
|
|
};
|
|
|
|
|
|
|
|
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 });
|
2021-01-27 19:42:51 -08:00
|
|
|
const server = await SnapshotServer.create(
|
2021-01-28 09:33:20 -08:00
|
|
|
path.join(__dirname, '..', '..', 'web'),
|
2021-01-27 19:42:51 -08:00
|
|
|
this._document ? this._document.resourcesDir : undefined,
|
|
|
|
this._document ? this._document.model : emptyModel,
|
|
|
|
this._document ? new ScreenshotGenerator(this._document.resourcesDir, this._document.model) : undefined);
|
2021-01-07 16:15:34 -08:00
|
|
|
const uiPage = await browser.newPage({ viewport: null });
|
|
|
|
uiPage.on('close', () => process.exit(0));
|
2021-01-28 09:33:20 -08:00
|
|
|
await uiPage.goto(server.traceViewerUrl('traceViewer/index.html'));
|
2021-01-07 16:15:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|