2021-01-08 01:15:34 +01: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';
|
2021-01-26 03:44:46 +01:00
|
|
|
import { actionById, ActionEntry, ContextEntry, PageEntry, TraceModel } from './traceModel';
|
2021-01-28 04:42:51 +01:00
|
|
|
import { SnapshotServer } from './snapshotServer';
|
2021-01-08 01:15:34 +01:00
|
|
|
|
|
|
|
|
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
|
|
|
|
|
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
|
|
|
|
|
|
|
|
|
|
export class ScreenshotGenerator {
|
|
|
|
|
private _traceStorageDir: string;
|
2021-01-22 04:00:32 +01:00
|
|
|
private _browserPromise: Promise<playwright.Browser>;
|
2021-01-28 04:42:51 +01:00
|
|
|
private _serverPromise: Promise<SnapshotServer>;
|
2021-01-08 01:15:34 +01:00
|
|
|
private _traceModel: TraceModel;
|
|
|
|
|
private _rendering = new Map<ActionEntry, Promise<Buffer | undefined>>();
|
2021-01-22 04:00:32 +01:00
|
|
|
private _lock = new Lock(3);
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-01-28 04:42:51 +01:00
|
|
|
constructor(resourcesDir: string, traceModel: TraceModel) {
|
|
|
|
|
this._traceStorageDir = resourcesDir;
|
2021-01-08 01:15:34 +01:00
|
|
|
this._traceModel = traceModel;
|
2021-01-22 04:00:32 +01:00
|
|
|
this._browserPromise = playwright.chromium.launch();
|
2021-01-28 04:42:51 +01:00
|
|
|
this._serverPromise = SnapshotServer.create(undefined, resourcesDir, traceModel, undefined);
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 04:00:32 +01:00
|
|
|
generateScreenshot(actionId: string): Promise<Buffer | undefined> {
|
2021-01-26 03:44:46 +01:00
|
|
|
const { context, action, page } = actionById(this._traceModel, actionId);
|
2021-01-22 04:00:32 +01:00
|
|
|
if (!this._rendering.has(action)) {
|
2021-01-26 03:44:46 +01:00
|
|
|
this._rendering.set(action, this._render(context, page, action).then(body => {
|
2021-01-22 04:00:32 +01:00
|
|
|
this._rendering.delete(action);
|
|
|
|
|
return body;
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
return this._rendering.get(action)!;
|
|
|
|
|
}
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-01-26 03:44:46 +01:00
|
|
|
private async _render(contextEntry: ContextEntry, pageEntry: PageEntry, actionEntry: ActionEntry): Promise<Buffer | undefined> {
|
|
|
|
|
const imageFileName = path.join(this._traceStorageDir, actionEntry.action.timestamp + '-screenshot.png');
|
2021-01-08 01:15:34 +01:00
|
|
|
try {
|
2021-01-22 04:00:32 +01:00
|
|
|
return await fsReadFileAsync(imageFileName);
|
2021-01-08 01:15:34 +01:00
|
|
|
} catch (e) {
|
2021-01-22 04:00:32 +01:00
|
|
|
// fall through
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { action } = actionEntry;
|
2021-01-22 04:00:32 +01:00
|
|
|
const browser = await this._browserPromise;
|
2021-01-28 04:42:51 +01:00
|
|
|
const server = await this._serverPromise;
|
2021-01-22 04:00:32 +01:00
|
|
|
|
|
|
|
|
await this._lock.obtain();
|
|
|
|
|
|
2021-01-08 01:15:34 +01:00
|
|
|
const page = await browser.newPage({
|
|
|
|
|
viewport: contextEntry.created.viewportSize,
|
|
|
|
|
deviceScaleFactor: contextEntry.created.deviceScaleFactor
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
2021-01-28 04:42:51 +01:00
|
|
|
await page.goto(server.snapshotRootUrl());
|
|
|
|
|
await page.evaluate(async () => {
|
|
|
|
|
navigator.serviceWorker.register('/service-worker.js');
|
|
|
|
|
await new Promise(resolve => navigator.serviceWorker.oncontrollerchange = resolve);
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-26 03:44:46 +01:00
|
|
|
const snapshots = action.snapshots || [];
|
|
|
|
|
const snapshotId = snapshots.length ? snapshots[0].snapshotId : undefined;
|
2021-01-28 04:42:51 +01:00
|
|
|
const snapshotUrl = server.snapshotUrl(action.pageId!, snapshotId, action.endTime);
|
|
|
|
|
console.log('Generating screenshot for ' + action.action); // eslint-disable-line no-console
|
|
|
|
|
await page.evaluate(snapshotUrl => (window as any).showSnapshot(snapshotUrl), snapshotUrl);
|
2021-01-08 01:15:34 +01:00
|
|
|
|
2021-01-26 03:44:46 +01:00
|
|
|
try {
|
|
|
|
|
const element = await page.$(action.selector || '*[__playwright_target__]');
|
|
|
|
|
if (element) {
|
|
|
|
|
await element.evaluate(e => {
|
|
|
|
|
e.style.backgroundColor = '#ff69b460';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e); // eslint-disable-line no-console
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
2021-01-22 04:00:32 +01:00
|
|
|
const imageData = await page.screenshot();
|
2021-01-08 01:15:34 +01:00
|
|
|
await fsWriteFileAsync(imageFileName, imageData);
|
|
|
|
|
return imageData;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e); // eslint-disable-line no-console
|
|
|
|
|
} finally {
|
|
|
|
|
await page.close();
|
2021-01-22 04:00:32 +01:00
|
|
|
this._lock.release();
|
2021-01-08 01:15:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-22 04:00:32 +01:00
|
|
|
|
|
|
|
|
class Lock {
|
|
|
|
|
private _maxWorkers: number;
|
|
|
|
|
private _callbacks: (() => void)[] = [];
|
|
|
|
|
private _workers = 0;
|
|
|
|
|
|
|
|
|
|
constructor(maxWorkers: number) {
|
|
|
|
|
this._maxWorkers = maxWorkers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async obtain() {
|
|
|
|
|
while (this._workers === this._maxWorkers)
|
|
|
|
|
await new Promise(f => this._callbacks.push(f));
|
|
|
|
|
++this._workers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
release() {
|
|
|
|
|
--this._workers;
|
|
|
|
|
const callbacks = this._callbacks;
|
|
|
|
|
this._callbacks = [];
|
|
|
|
|
for (const callback of callbacks)
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|