Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: rework log api calls functionality to not replace objects (#518)

Dmitry Gozman734c752fea38
diff --git a/index.js b/index.js
index 644f5b919f..4d8e9ca4a4 100644
--- a/index.js
+++ b/index.js
@@ -20,7 +20,7 @@ const packageJson = require('./package.json');
for (const className in api) {
if (typeof api[className] === 'function')
- helper.installAsyncStackHooks(api[className]);
+ helper.installApiHooks(className, api[className]);
}
module.exports = {
diff --git a/src/dom.ts b/src/dom.ts
index b17933f90f..8f43d09032 100644
--- a/src/dom.ts
+++ b/src/dom.ts
@@ -123,7 +123,6 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {
super(context, remoteObject);
this._context = context;
this._page = context.frame._page;
- return helper.logPublicApiCalls('handle', this);
}
asElement(): ElementHandle<T> | null {
diff --git a/src/frames.ts b/src/frames.ts
index 3597961b95..fccb725512 100644
--- a/src/frames.ts
+++ b/src/frames.ts
@@ -303,8 +303,6 @@ export class Frame {
if (this._parentFrame)
this._parentFrame._childFrames.add(this);
-
- return helper.logPublicApiCalls('frame', this);
}
async goto(url: string, options?: GotoOptions): Promise<network.Response | null> {
diff --git a/src/helper.ts b/src/helper.ts
index 8644653c3f..062736bc11 100644
--- a/src/helper.ts
+++ b/src/helper.ts
@@ -41,12 +41,24 @@ class Helper {
}
}
- static installAsyncStackHooks(classType: any) {
+ static installApiHooks(className: string, classType: any) {
+ const log = platform.debug('pw:api');
for (const methodName of Reflect.ownKeys(classType.prototype)) {
const method = Reflect.get(classType.prototype, methodName);
- if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function' || method.constructor.name !== 'AsyncFunction')
+ if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
+ continue;
+ const isAsync = method.constructor.name === 'AsyncFunction';
+ if (!isAsync && !log.enabled)
continue;
Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) {
+ if (log.enabled) {
+ if (args.length)
+ log(`${className}.${methodName} %o`, args);
+ else
+ log(`${className}.${methodName}`);
+ }
+ if (!isAsync)
+ return method.call(this, ...args);
const syncStack: any = {};
Error.captureStackTrace(syncStack);
return method.call(this, ...args).catch((e: any) => {
@@ -60,26 +72,6 @@ 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(this: any, ...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 3b693e22dc..0b25f68607 100644
--- a/src/javascript.ts
+++ b/src/javascript.ts
@@ -16,7 +16,6 @@
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<any>;
@@ -58,7 +57,6 @@ export class JSHandle<T = any> {
constructor(context: ExecutionContext, remoteObject: any) {
this._context = context;
this._remoteObject = remoteObject;
- return helper.logPublicApiCalls('handle', this);
}
evaluate: types.EvaluateOn<T> = (pageFunction, ...args) => {
diff --git a/src/page.ts b/src/page.ts
index 1cfe984a91..041dfc4677 100644
--- a/src/page.ts
+++ b/src/page.ts
@@ -139,7 +139,6 @@ 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() {
@@ -539,7 +538,6 @@ export class Worker {
this._url = url;
this._executionContextCallback = () => {};
this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
- return helper.logPublicApiCalls('worker', this);
}
_createExecutionContext(delegate: js.ExecutionContextDelegate) {