feat(trace): record traces when PW_TRACE_DIR is set (#4545)

PW_TRACE_DIR points to a directory that Playwright can
put trace files and resources into.
This does not enable video recording, although it might
in the future.
This commit is contained in:
Dmitry Gozman 2020-11-30 15:25:07 -08:00 committed by GitHub
parent d116787a4d
commit 84dc441a9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,7 +20,7 @@ import { ContextCreatedTraceEvent, ContextDestroyedTraceEvent, NetworkResourceTr
import * as path from 'path'; import * as path from 'path';
import * as util from 'util'; import * as util from 'util';
import * as fs from 'fs'; import * as fs from 'fs';
import { calculateSha1, createGuid, mkdirIfNeeded, monotonicTime } from '../utils/utils'; import { calculateSha1, createGuid, getFromENV, mkdirIfNeeded, monotonicTime } from '../utils/utils';
import { Page } from '../server/page'; import { Page } from '../server/page';
import { Snapshotter } from './snapshotter'; import { Snapshotter } from './snapshotter';
import { ElementHandle } from '../server/dom'; import { ElementHandle } from '../server/dom';
@ -31,6 +31,7 @@ import { ProgressResult } from '../server/progress';
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs)); const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
const fsAppendFileAsync = util.promisify(fs.appendFile.bind(fs)); const fsAppendFileAsync = util.promisify(fs.appendFile.bind(fs));
const fsAccessAsync = util.promisify(fs.access.bind(fs)); const fsAccessAsync = util.promisify(fs.access.bind(fs));
const envTrace = getFromENV('PW_TRACE_DIR');
export function installTracer() { export function installTracer() {
contextListeners.add(new Tracer()); contextListeners.add(new Tracer());
@ -40,10 +41,18 @@ class Tracer implements ContextListener {
private _contextTracers = new Map<BrowserContext, ContextTracer>(); private _contextTracers = new Map<BrowserContext, ContextTracer>();
async onContextCreated(context: BrowserContext): Promise<void> { async onContextCreated(context: BrowserContext): Promise<void> {
if (!context._options._tracePath) let traceStorageDir: string;
let tracePath: string;
if (context._options._tracePath) {
traceStorageDir = context._options._traceResourcesPath || path.join(path.dirname(context._options._tracePath), 'trace-resources');
tracePath = context._options._tracePath;
} else if (envTrace) {
traceStorageDir = envTrace;
tracePath = path.join(envTrace, createGuid() + '.trace');
} else {
return; return;
const traceStorageDir = context._options._traceResourcesPath || path.join(path.dirname(context._options._tracePath), 'trace-resources'); }
const contextTracer = new ContextTracer(context, traceStorageDir, context._options._tracePath); const contextTracer = new ContextTracer(context, traceStorageDir, tracePath);
this._contextTracers.set(context, contextTracer); this._contextTracers.set(context, contextTracer);
} }