From a8b9920a14f7137797c92201ecbe7266a5d0369d Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 13 Jan 2020 09:14:28 -0800 Subject: [PATCH] feat(logging): log the API calls (#464) --- src/chromium/crConnection.ts | 2 +- src/dom.ts | 1 + src/firefox/ffConnection.ts | 2 +- src/frames.ts | 2 ++ src/helper.ts | 22 +++++++++++++++++++++- src/javascript.ts | 2 ++ src/page.ts | 3 +++ src/webkit/wkConnection.ts | 4 ++-- 8 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/chromium/crConnection.ts b/src/chromium/crConnection.ts index 02bd3ba489..a49a7a8177 100644 --- a/src/chromium/crConnection.ts +++ b/src/chromium/crConnection.ts @@ -20,7 +20,7 @@ import { ConnectionTransport } from '../transport'; import { assert } from '../helper'; import { Protocol } from './protocol'; -const debugProtocol = platform.debug('playwright:protocol'); +const debugProtocol = platform.debug('pw:protocol'); export const ConnectionEvents = { Disconnected: Symbol('ConnectionEvents.Disconnected') diff --git a/src/dom.ts b/src/dom.ts index cc16cf659c..05a4ec04b7 100644 --- a/src/dom.ts +++ b/src/dom.ts @@ -122,6 +122,7 @@ export class ElementHandle extends js.JSHandle { constructor(context: FrameExecutionContext, remoteObject: any) { super(context, remoteObject); this._page = context.frame._page; + return helper.logPublicApiCalls('handle', this); } asElement(): ElementHandle | null { diff --git a/src/firefox/ffConnection.ts b/src/firefox/ffConnection.ts index 354839fd30..301cbf19cf 100644 --- a/src/firefox/ffConnection.ts +++ b/src/firefox/ffConnection.ts @@ -20,7 +20,7 @@ import * as platform from '../platform'; import { ConnectionTransport } from '../transport'; import { Protocol } from './protocol'; -const debugProtocol = platform.debug('playwright:protocol'); +const debugProtocol = platform.debug('pw:protocol'); export const ConnectionEvents = { Disconnected: Symbol('Disconnected'), diff --git a/src/frames.ts b/src/frames.ts index 15d30b9cf8..57c7e542fe 100644 --- a/src/frames.ts +++ b/src/frames.ts @@ -293,6 +293,8 @@ export class Frame { if (this._parentFrame) this._parentFrame._childFrames.add(this); + + return helper.logPublicApiCalls('frame', this); } async goto(url: string, options?: GotoOptions): Promise { diff --git a/src/helper.ts b/src/helper.ts index 89b5d0044a..2a1f1f52ad 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -18,7 +18,7 @@ import { TimeoutError } from './errors'; import * as platform from './platform'; -export const debugError = platform.debug(`playwright:error`); +export const debugError = platform.debug(`pw:error`); export type RegisteredListener = { emitter: platform.EventEmitterType; @@ -60,6 +60,26 @@ class Helper { } } + static logPublicApiCalls(className: string, object: any): any { + const log = platform.debug('pw:api'); + if (!log.enabled) + return object; + return new Proxy(object, { + get(target, key: string) { + const value = target[key]; + if (typeof key === 'string' && key.startsWith('_') || typeof value !== 'function') + return value; + return function(...args: any) { + if (args.length) + log(`${className}.${key} %o`, args); + else + log(`${className}.${key}`); + return value.apply(this, args); + }; + } + }); + } + static addEventListener( emitter: platform.EventEmitterType, eventName: (string | symbol), diff --git a/src/javascript.ts b/src/javascript.ts index 2d09043a88..02efdc272e 100644 --- a/src/javascript.ts +++ b/src/javascript.ts @@ -16,6 +16,7 @@ import * as types from './types'; import * as dom from './dom'; +import { helper } from './helper'; export interface ExecutionContextDelegate { evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: string | Function, ...args: any[]): Promise; @@ -57,6 +58,7 @@ export class JSHandle { constructor(context: ExecutionContext, remoteObject: any) { this._context = context; this._remoteObject = remoteObject; + return helper.logPublicApiCalls('handle', this); } evaluate: types.EvaluateOn = (pageFunction, ...args) => { diff --git a/src/page.ts b/src/page.ts index 6eecf2d823..5aaeb12c35 100644 --- a/src/page.ts +++ b/src/page.ts @@ -137,6 +137,7 @@ export class Page extends platform.EventEmitter { if (delegate.pdf) this.pdf = delegate.pdf.bind(delegate); this.coverage = delegate.coverage(); + return helper.logPublicApiCalls('page', this); } _didClose() { @@ -537,7 +538,9 @@ export class Worker { constructor(url: string) { this._url = url; this._executionContextPromise = new Promise(x => this._executionContextCallback = x); + return helper.logPublicApiCalls('worker', this); } + _createExecutionContext(delegate: js.ExecutionContextDelegate) { this._existingExecutionContext = new js.ExecutionContext(delegate); this._executionContextCallback(this._existingExecutionContext); diff --git a/src/webkit/wkConnection.ts b/src/webkit/wkConnection.ts index 0de057614b..3556993c38 100644 --- a/src/webkit/wkConnection.ts +++ b/src/webkit/wkConnection.ts @@ -20,8 +20,8 @@ import * as platform from '../platform'; import { ConnectionTransport } from '../transport'; import { Protocol } from './protocol'; -const debugProtocol = platform.debug('playwright:protocol'); -const debugWrappedMessage = platform.debug('wrapped'); +const debugProtocol = platform.debug('pw:protocol'); +const debugWrappedMessage = platform.debug('pw:wrapped'); // WKBrowserServer uses this special id to issue Browser.close command which we // should ignore.