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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-09 04:49:57 +01:00
|
|
|
import fs from 'fs';
|
2021-02-11 15:36:15 +01:00
|
|
|
import path from 'path';
|
2020-08-28 19:51:55 +02:00
|
|
|
import * as util from 'util';
|
2021-02-24 22:39:51 +01:00
|
|
|
import { createGuid, getFromENV, mkdirIfNeeded, monotonicTime } from '../../../utils/utils';
|
2021-03-10 20:43:26 +01:00
|
|
|
import { BrowserContext } from '../../browserContext';
|
2021-02-24 22:39:51 +01:00
|
|
|
import { Dialog } from '../../dialog';
|
2021-03-10 20:43:26 +01:00
|
|
|
import { ElementHandle } from '../../dom';
|
2021-02-24 22:39:51 +01:00
|
|
|
import { Frame, NavigationEvent } from '../../frames';
|
2021-03-09 04:49:57 +01:00
|
|
|
import { helper, RegisteredListener } from '../../helper';
|
2021-02-24 22:39:51 +01:00
|
|
|
import { CallMetadata, InstrumentationListener, SdkObject } from '../../instrumentation';
|
2021-03-09 04:49:57 +01:00
|
|
|
import { Page } from '../../page';
|
|
|
|
|
import { PersistentSnapshotter } from '../../snapshot/persistentSnapshotter';
|
|
|
|
|
import * as trace from '../common/traceEvents';
|
2020-08-28 19:51:55 +02:00
|
|
|
|
|
|
|
|
const fsAppendFileAsync = util.promisify(fs.appendFile.bind(fs));
|
2021-03-10 20:43:26 +01:00
|
|
|
const envTrace = getFromENV('PWTRACE_RESOURCE_DIR');
|
2020-08-28 19:51:55 +02:00
|
|
|
|
2021-02-09 23:44:48 +01:00
|
|
|
export class Tracer implements InstrumentationListener {
|
2020-09-18 20:54:00 +02:00
|
|
|
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> {
|
2021-03-10 20:43:26 +01:00
|
|
|
const traceDir = context._options._traceDir;
|
2021-01-21 04:16:23 +01:00
|
|
|
if (!traceDir)
|
2020-09-18 20:54:00 +02:00
|
|
|
return;
|
2021-03-10 20:43:26 +01:00
|
|
|
const resourcesDir = envTrace || path.join(traceDir, 'resources');
|
2021-03-09 04:49:57 +01:00
|
|
|
const tracePath = path.join(traceDir, createGuid());
|
2021-03-10 20:43:26 +01:00
|
|
|
const contextTracer = new ContextTracer(context, resourcesDir, tracePath);
|
2021-02-26 23:16:32 +01:00
|
|
|
await contextTracer.start();
|
2020-09-18 20:54:00 +02:00
|
|
|
this._contextTracers.set(context, contextTracer);
|
2020-09-05 01:31:52 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 22:32:07 +01:00
|
|
|
async onContextDidDestroy(context: BrowserContext): Promise<void> {
|
2020-09-18 20:54:00 +02:00
|
|
|
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
|
|
|
}
|
2021-02-09 23:44:48 +01:00
|
|
|
|
2021-03-10 20:43:26 +01:00
|
|
|
async onBeforeInputAction(sdkObject: SdkObject, metadata: CallMetadata, element: ElementHandle): Promise<void> {
|
|
|
|
|
this._contextTracers.get(sdkObject.attribution.context!)?._captureSnapshot('action', sdkObject, metadata, element);
|
2021-02-11 06:55:46 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 20:43:26 +01:00
|
|
|
async onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata, element?: ElementHandle): Promise<void> {
|
|
|
|
|
this._contextTracers.get(sdkObject.attribution.context!)?._captureSnapshot('before', sdkObject, metadata, element);
|
2021-02-09 23:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-11 06:55:46 +01:00
|
|
|
async onAfterCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
|
2021-03-10 20:43:26 +01:00
|
|
|
this._contextTracers.get(sdkObject.attribution.context!)?._captureSnapshot('after', sdkObject, metadata);
|
2021-02-11 06:55:46 +01:00
|
|
|
this._contextTracers.get(sdkObject.attribution.context!)?.onAfterCall(sdkObject, metadata);
|
2021-02-09 23:44:48 +01:00
|
|
|
}
|
2020-09-05 01:31:52 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-26 03:44:46 +01:00
|
|
|
const snapshotsSymbol = Symbol('snapshots');
|
|
|
|
|
|
2021-02-11 06:55:46 +01:00
|
|
|
// This is an official way to pass snapshots between onBefore/AfterInputAction and onAfterCall.
|
2021-03-09 04:49:57 +01:00
|
|
|
function snapshotsForMetadata(metadata: CallMetadata): { title: string, snapshotName: string }[] {
|
2021-01-26 03:44:46 +01:00
|
|
|
if (!(metadata as any)[snapshotsSymbol])
|
|
|
|
|
(metadata as any)[snapshotsSymbol] = [];
|
|
|
|
|
return (metadata as any)[snapshotsSymbol];
|
|
|
|
|
}
|
2021-01-19 23:45:26 +01:00
|
|
|
|
2021-03-09 04:49:57 +01:00
|
|
|
class ContextTracer {
|
2020-09-05 01:31:52 +02:00
|
|
|
private _contextId: string;
|
2020-08-28 19:51:55 +02:00
|
|
|
private _appendEventChain: Promise<string>;
|
2021-03-09 04:49:57 +01:00
|
|
|
private _snapshotter: PersistentSnapshotter;
|
2020-09-11 20:34:53 +02:00
|
|
|
private _eventListeners: RegisteredListener[];
|
|
|
|
|
private _disposed = false;
|
2020-08-28 19:51:55 +02:00
|
|
|
|
2021-03-10 20:43:26 +01:00
|
|
|
constructor(context: BrowserContext, resourcesDir: string, tracePrefix: string) {
|
2021-03-09 04:49:57 +01:00
|
|
|
const traceFile = tracePrefix + '-actions.trace';
|
2020-09-05 01:31:52 +02:00
|
|
|
this._contextId = 'context@' + createGuid();
|
2020-08-28 19:51:55 +02:00
|
|
|
this._appendEventChain = mkdirIfNeeded(traceFile).then(() => traceFile);
|
2021-01-16 03:30:55 +01:00
|
|
|
const event: trace.ContextCreatedTraceEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
2020-08-28 19:51:55 +02:00
|
|
|
type: 'context-created',
|
2021-01-30 01:00:56 +01:00
|
|
|
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,
|
2021-01-28 19:50:57 +01:00
|
|
|
debugName: context._options._debugName,
|
2020-08-28 19:51:55 +02:00
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
2021-03-10 20:43:26 +01:00
|
|
|
this._snapshotter = new PersistentSnapshotter(context, tracePrefix, resourcesDir);
|
2020-09-11 20:34:53 +02:00
|
|
|
this._eventListeners = [
|
|
|
|
|
helper.addEventListener(context, BrowserContext.Events.Page, this._onPage.bind(this)),
|
|
|
|
|
];
|
2020-08-28 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-26 23:16:32 +01:00
|
|
|
async start() {
|
2021-03-09 04:49:57 +01:00
|
|
|
await this._snapshotter.start();
|
2021-01-26 03:44:46 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-11 20:22:59 +01:00
|
|
|
async _captureSnapshot(name: 'before' | 'after' | 'action', sdkObject: SdkObject, metadata: CallMetadata, element?: ElementHandle): Promise<void> {
|
2021-02-09 23:44:48 +01:00
|
|
|
if (!sdkObject.attribution.page)
|
|
|
|
|
return;
|
2021-03-09 04:49:57 +01:00
|
|
|
const snapshotName = `${name}@${metadata.id}`;
|
|
|
|
|
snapshotsForMetadata(metadata).push({ title: name, snapshotName });
|
2021-03-10 20:43:26 +01:00
|
|
|
this._snapshotter.captureSnapshot(sdkObject.attribution.page, snapshotName, element);
|
2021-01-26 03:44:46 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-11 06:55:46 +01:00
|
|
|
async onAfterCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
|
2021-02-09 23:44:48 +01:00
|
|
|
if (!sdkObject.attribution.page)
|
|
|
|
|
return;
|
2021-01-26 03:44:46 +01:00
|
|
|
const event: trace.ActionTraceEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
|
|
|
|
type: 'action',
|
|
|
|
|
contextId: this._contextId,
|
2021-03-10 20:43:26 +01:00
|
|
|
metadata,
|
2021-01-26 03:44:46 +01:00
|
|
|
snapshots: snapshotsForMetadata(metadata),
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
2020-09-11 06:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-11 20:34:53 +02:00
|
|
|
private _onPage(page: Page) {
|
2021-03-01 21:20:04 +01:00
|
|
|
const pageId = page.uniqueId;
|
2020-09-11 20:34:53 +02:00
|
|
|
|
2021-01-16 03:30:55 +01:00
|
|
|
const event: trace.PageCreatedTraceEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
2020-09-11 20:34:53 +02:00
|
|
|
type: 'page-created',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
|
2021-01-16 03:30:55 +01:00
|
|
|
page.on(Page.Events.Dialog, (dialog: Dialog) => {
|
|
|
|
|
if (this._disposed)
|
|
|
|
|
return;
|
|
|
|
|
const event: trace.DialogOpenedEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
|
|
|
|
type: 'dialog-opened',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
dialogType: dialog.type(),
|
|
|
|
|
message: dialog.message(),
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
page.on(Page.Events.InternalDialogClosed, (dialog: Dialog) => {
|
|
|
|
|
if (this._disposed)
|
|
|
|
|
return;
|
|
|
|
|
const event: trace.DialogClosedEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
|
|
|
|
type: 'dialog-closed',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
dialogType: dialog.type(),
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
page.mainFrame().on(Frame.Events.Navigation, (navigationEvent: NavigationEvent) => {
|
|
|
|
|
if (this._disposed || page.mainFrame().url() === 'about:blank')
|
|
|
|
|
return;
|
|
|
|
|
const event: trace.NavigationEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
|
|
|
|
type: 'navigation',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
url: navigationEvent.url,
|
|
|
|
|
sameDocument: !navigationEvent.newDocument,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
page.on(Page.Events.Load, () => {
|
|
|
|
|
if (this._disposed || page.mainFrame().url() === 'about:blank')
|
|
|
|
|
return;
|
|
|
|
|
const event: trace.LoadEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
|
|
|
|
type: 'load',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
|
2020-09-11 20:34:53 +02:00
|
|
|
page.once(Page.Events.Close, () => {
|
|
|
|
|
if (this._disposed)
|
|
|
|
|
return;
|
2021-01-16 03:30:55 +01:00
|
|
|
const event: trace.PageDestroyedTraceEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
2020-09-11 20:34:53 +02:00
|
|
|
type: 'page-destroyed',
|
|
|
|
|
contextId: this._contextId,
|
|
|
|
|
pageId,
|
|
|
|
|
};
|
|
|
|
|
this._appendTraceEvent(event);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 19:51:55 +02:00
|
|
|
async dispose() {
|
2020-09-11 20:34:53 +02:00
|
|
|
this._disposed = true;
|
|
|
|
|
helper.removeEventListeners(this._eventListeners);
|
2021-03-22 17:59:39 +01:00
|
|
|
await this._snapshotter.dispose();
|
2021-01-16 03:30:55 +01:00
|
|
|
const event: trace.ContextDestroyedTraceEvent = {
|
|
|
|
|
timestamp: monotonicTime(),
|
2020-09-05 01:31:52 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _appendTraceEvent(event: any) {
|
|
|
|
|
// Serialize all writes to the trace file.
|
|
|
|
|
this._appendEventChain = this._appendEventChain.then(async traceFile => {
|
2021-01-16 03:30:55 +01:00
|
|
|
await fsAppendFileAsync(traceFile, JSON.stringify(event) + '\n');
|
2020-08-28 19:51:55 +02:00
|
|
|
return traceFile;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|