2020-08-28 19:51:55 +02: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-09-19 02:36:43 +02:00
|
|
|
import { ActionListener, ActionMetadata, BrowserContext, ContextListener, contextListeners, Video } from '../server/browserContext';
|
2020-09-14 16:56:04 +02:00
|
|
|
import type { SnapshotterResource as SnapshotterResource, SnapshotterBlob, SnapshotterDelegate } from './snapshotter';
|
2020-09-19 02:36:43 +02:00
|
|
|
import { ContextCreatedTraceEvent, ContextDestroyedTraceEvent, NetworkResourceTraceEvent, ActionTraceEvent, PageCreatedTraceEvent, PageDestroyedTraceEvent, PageVideoTraceEvent } from './traceTypes';
|
2020-08-28 19:51:55 +02:00
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as util from 'util';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import { calculateSha1, createGuid, mkdirIfNeeded, monotonicTime } from '../utils/utils';
|
2020-09-11 20:34:53 +02:00
|
|
|
import { Page } from '../server/page';
|
2020-09-05 01:31:52 +02:00
|
|
|
import { Snapshotter } from './snapshotter';
|
2020-09-15 18:46:36 +02:00
|
|
|
import { ElementHandle } from '../server/dom';
|
2020-09-11 20:34:53 +02:00
|
|
|
import { helper, RegisteredListener } from '../server/helper';
|
2020-09-12 00:13:37 +02:00
|
|
|
import { DEFAULT_TIMEOUT } from '../utils/timeoutSettings';
|
2020-09-17 18:32:54 +02:00
|
|
|
import { ProgressResult } from '../server/progress';
|
2020-08-28 19:51:55 +02:00
|
|
|
|
|
|
|
|
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
|
|
|
|
|
const fsAppendFileAsync = util.promisify(fs.appendFile.bind(fs));
|
|
|
|
|
const fsAccessAsync = util.promisify(fs.access.bind(fs));
|
|
|
|
|
|
2020-09-18 20:54:00 +02:00
|
|
|
export function installTracer() {
|
|
|
|
|
contextListeners.add(new Tracer());
|
|
|
|
|
}
|
2020-09-05 01:31:52 +02:00
|
|
|
|
2020-09-18 20:54:00 +02:00
|
|
|
class Tracer implements ContextListener {
|
|
|
|
|
private _contextTracers = new Map<BrowserContext, ContextTracer>();
|
2020-09-05 01:31:52 +02:00
|
|
|
|
2020-09-18 20:54:00 +02:00
|
|
|
async onContextCreated(context: BrowserContext): Promise<void> {
|
|
|
|
|
if (!context._options.recordTrace)
|
|
|
|
|
return;
|
2020-09-29 20:51:00 +02:00
|
|
|
const traceStorageDir = path.join(context._browser._options.artifactsPath!, 'trace-resources');
|
2020-09-19 02:36:43 +02:00
|
|
|
const traceFile = path.join(context._artifactsPath!, 'playwright.trace');
|
2020-09-18 20:54:00 +02:00
|
|
|
const contextTracer = new ContextTracer(context, traceStorageDir, traceFile);
|
|
|
|
|
this._contextTracers.set(context, contextTracer);
|
2020-09-05 01:31:52 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-18 20:54:00 +02:00
|
|
|
async onContextDestroyed(context: BrowserContext): Promise<void> {
|
|
|
|
|
const contextTracer = this._contextTracers.get(context);
|
|
|
|
|
if (contextTracer) {
|
|
|
|
|
await contextTracer.dispose().catch(e => {});
|
|
|
|
|
this._contextTracers.delete(context);
|
2020-09-12 00:13:37 +02:00
|
|
|
}
|
2020-09-05 01:31:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 20:54:00 +02:00
|
|
|
class ContextTracer implements SnapshotterDelegate, ActionListener {
|
|
|
|
|
private _context: BrowserContext;
|
2020-09-05 01:31:52 +02:00
|
|
|
private _contextId: string;
|
2020-08-28 19:51:55 +02:00
|
|
|
private _traceStoragePromise: Promise<string>;
|
|
|
|
|
private _appendEventChain: Promise<string>;
|
|
|
|
|
private _writeArtifactChain: Promise<void>;
|
2020-09-11 20:34:53 +02:00
|
|
|
private _snapshotter: Snapshotter;
|
|
|
|
|
private _eventListeners: RegisteredListener[];
|
|
|
|
|
private _disposed = false;
|
|
|
|
|
private _pageToId = new Map<Page, string>();
|
2020-08-28 19:51:55 +02:00
|
|
|
|
2020-09-05 01:31:52 +02:00
|
|
|
constructor(context: BrowserContext, traceStorageDir: string, traceFile: string) {
|
2020-09-18 20:54:00 +02:00
|
|
|
this._context = context;
|
2020-09-05 01:31:52 +02:00
|
|
|
this._contextId = 'context@' + createGuid();
|
2020-08-28 19:51:55 +02:00
|
|
|
this._traceStoragePromise = mkdirIfNeeded(path.join(traceStorageDir, 'sha1')).then(() => traceStorageDir);
|
|
|
|
|
this._appendEventChain = mkdirIfNeeded(traceFile).then(() => traceFile);
|
|
|
|
|
this._writeArtifactChain = Promise.resolve();
|
|
|
|
|
const event: ContextCreatedTraceEvent = {
|
|
|
|
|
type: 'context-created',
|
|
|
|
|
browserName: context._browser._options.name,
|
2020-09-05 01:31:52 +02:00
|
|
|
contextId: this._contextId,
|
2020-08-28 19:51:55 +02:00
|
|
|
isMobile: !!context._options.isMobile,
|
|
|
|
|
deviceScaleFactor: context._options.deviceScaleFactor || 1,
|
|
|
|
|
viewportSize: context._options.viewport || undefined,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
2020-09-05 01:31:52 +02:00
|
|
|
this._snapshotter = new Snapshotter(context, this);
|
2020-09-11 20:34:53 +02:00
|
|
|
this._eventListeners = [
|
|
|
|
|
helper.addEventListener(context, BrowserContext.Events.Page, this._onPage.bind(this)),
|
|
|
|
|
];
|
2020-09-18 20:54:00 +02:00
|
|
|
this._context._actionListeners.add(this);
|
2020-08-28 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-05 01:31:52 +02:00
|
|
|
onBlob(blob: SnapshotterBlob): void {
|
2020-08-28 19:51:55 +02:00
|
|
|
this._writeArtifact(blob.sha1, blob.buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:56:04 +02:00
|
|
|
onResource(resource: SnapshotterResource): void {
|
2020-08-28 19:51:55 +02:00
|
|
|
const event: NetworkResourceTraceEvent = {
|
|
|
|
|
type: 'resource',
|
2020-09-05 01:31:52 +02:00
|
|
|
contextId: this._contextId,
|
2020-09-14 16:56:04 +02:00
|
|
|
pageId: resource.pageId,
|
2020-08-28 19:51:55 +02:00
|
|
|
frameId: resource.frameId,
|
|
|
|
|
url: resource.url,
|
|
|
|
|
contentType: resource.contentType,
|
|
|
|
|
responseHeaders: resource.responseHeaders,
|
|
|
|
|
sha1: resource.sha1,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 16:56:04 +02:00
|
|
|
pageId(page: Page): string {
|
|
|
|
|
return this._pageToId.get(page)!;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 20:54:00 +02:00
|
|
|
async onAfterAction(result: ProgressResult, metadata: ActionMetadata): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
const snapshot = await this._takeSnapshot(metadata.page, typeof metadata.target === 'string' ? undefined : metadata.target);
|
|
|
|
|
const event: ActionTraceEvent = {
|
|
|
|
|
type: 'action',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId: this._pageToId.get(metadata.page),
|
|
|
|
|
action: metadata.type,
|
|
|
|
|
selector: typeof metadata.target === 'string' ? metadata.target : undefined,
|
|
|
|
|
value: metadata.value,
|
|
|
|
|
snapshot,
|
|
|
|
|
startTime: result.startTime,
|
|
|
|
|
endTime: result.endTime,
|
|
|
|
|
stack: metadata.stack,
|
|
|
|
|
logs: result.logs.slice(),
|
|
|
|
|
error: result.error ? result.error.stack : undefined,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
}
|
2020-09-11 06:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-11 20:34:53 +02:00
|
|
|
private _onPage(page: Page) {
|
|
|
|
|
const pageId = 'page@' + createGuid();
|
|
|
|
|
this._pageToId.set(page, pageId);
|
|
|
|
|
|
|
|
|
|
const event: PageCreatedTraceEvent = {
|
|
|
|
|
type: 'page-created',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
|
2020-09-19 02:36:43 +02:00
|
|
|
page.on(Page.Events.VideoStarted, (video: Video) => {
|
|
|
|
|
if (this._disposed)
|
|
|
|
|
return;
|
|
|
|
|
const event: PageVideoTraceEvent = {
|
|
|
|
|
type: 'page-video',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
fileName: path.basename(video._path),
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
|
2020-09-11 20:34:53 +02:00
|
|
|
page.once(Page.Events.Close, () => {
|
|
|
|
|
this._pageToId.delete(page);
|
|
|
|
|
if (this._disposed)
|
|
|
|
|
return;
|
|
|
|
|
const event: PageDestroyedTraceEvent = {
|
|
|
|
|
type: 'page-destroyed',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 00:26:59 +02:00
|
|
|
private async _takeSnapshot(page: Page, target: ElementHandle | undefined, timeout: number = 0): Promise<{ sha1: string, duration: number } | undefined> {
|
2020-09-12 00:13:37 +02:00
|
|
|
if (!timeout) {
|
|
|
|
|
// Never use zero timeout to avoid stalling because of snapshot.
|
|
|
|
|
// Use 20% of the default timeout.
|
|
|
|
|
timeout = (page._timeoutSettings.timeout({}) || DEFAULT_TIMEOUT) / 5;
|
|
|
|
|
}
|
2020-09-11 06:42:09 +02:00
|
|
|
const startTime = monotonicTime();
|
2020-09-17 00:26:59 +02:00
|
|
|
const snapshot = await this._snapshotter.takeSnapshot(page, target, timeout);
|
2020-09-11 06:42:09 +02:00
|
|
|
if (!snapshot)
|
|
|
|
|
return;
|
|
|
|
|
const buffer = Buffer.from(JSON.stringify(snapshot));
|
|
|
|
|
const sha1 = calculateSha1(buffer);
|
2020-08-28 19:51:55 +02:00
|
|
|
this._writeArtifact(sha1, buffer);
|
2020-09-11 06:42:09 +02:00
|
|
|
return { sha1, duration: monotonicTime() - startTime };
|
2020-08-28 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async dispose() {
|
2020-09-11 20:34:53 +02:00
|
|
|
this._disposed = true;
|
2020-09-18 20:54:00 +02:00
|
|
|
this._context._actionListeners.delete(this);
|
2020-09-11 20:34:53 +02:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
|
this._pageToId.clear();
|
2020-09-05 01:31:52 +02:00
|
|
|
this._snapshotter.dispose();
|
|
|
|
|
const event: ContextDestroyedTraceEvent = {
|
|
|
|
|
type: 'context-destroyed',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
|
2020-08-28 19:51:55 +02:00
|
|
|
// Ensure all writes are finished.
|
|
|
|
|
await this._appendEventChain;
|
|
|
|
|
await this._writeArtifactChain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _writeArtifact(sha1: string, buffer: Buffer) {
|
|
|
|
|
// Save all write promises to wait for them in dispose.
|
|
|
|
|
const promise = this._innerWriteArtifact(sha1, buffer);
|
|
|
|
|
this._writeArtifactChain = this._writeArtifactChain.then(() => promise);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async _innerWriteArtifact(sha1: string, buffer: Buffer): Promise<void> {
|
|
|
|
|
const traceDirectory = await this._traceStoragePromise;
|
|
|
|
|
const filePath = path.join(traceDirectory, sha1);
|
|
|
|
|
try {
|
|
|
|
|
await fsAccessAsync(filePath);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// File does not exist - write it.
|
|
|
|
|
await fsWriteFileAsync(filePath, buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _appendTraceEvent(event: any) {
|
|
|
|
|
// Serialize all writes to the trace file.
|
|
|
|
|
const timestamp = monotonicTime();
|
|
|
|
|
this._appendEventChain = this._appendEventChain.then(async traceFile => {
|
|
|
|
|
await fsAppendFileAsync(traceFile, JSON.stringify({...event, timestamp}) + '\n');
|
|
|
|
|
return traceFile;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|