Skip to content

Commit 600f731

Browse files
authored
feat(inspector): render api names from metainfo (#5530)
1 parent d6ac3e6 commit 600f731

File tree

15 files changed

+54
-48
lines changed

15 files changed

+54
-48
lines changed

src/client/android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class AndroidDevice extends ChannelOwner<channels.AndroidDeviceChannel, c
239239
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
240240
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
241241
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
242-
const waiter = Waiter.createForEvent(this, event);
242+
const waiter = Waiter.createForEvent(this, 'androidDevice', event);
243243
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
244244
if (event !== Events.AndroidDevice.Close)
245245
waiter.rejectOnEvent(this, Events.AndroidDevice.Close, new Error('Device closed'));

src/client/browserContext.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
217217
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
218218
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
219219
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
220-
const waiter = Waiter.createForEvent(this, event);
220+
const waiter = Waiter.createForEvent(this, 'browserContext', event);
221221
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
222222
if (event !== Events.BrowserContext.Close)
223223
waiter.rejectOnEvent(this, Events.BrowserContext.Close, new Error('Context closed'));
@@ -256,12 +256,6 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
256256
}
257257
}
258258

259-
async _pause() {
260-
return this._wrapApiCall('browserContext.pause', async (channel: channels.BrowserContextChannel) => {
261-
await channel.pause();
262-
});
263-
}
264-
265259
async _enableRecorder(params: {
266260
language: string,
267261
launchOptions?: LaunchOptions,

src/client/channelOwner.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,7 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
4848
this._logger = this._parent._logger;
4949
}
5050

51-
const base = new EventEmitter();
52-
this._channel = new Proxy(base, {
53-
get: (obj: any, prop) => {
54-
if (prop === 'debugScopeState')
55-
return (params: any) => this._connection.sendMessageToServer(guid, prop, params);
56-
if (typeof prop === 'string') {
57-
const validator = scheme[paramsName(type, prop)];
58-
if (validator)
59-
return (params: any) => this._connection.sendMessageToServer(guid, prop, validator(params, ''));
60-
}
61-
return obj[prop];
62-
},
63-
});
64-
(this._channel as any)._object = this;
51+
this._channel = this._createChannel(new EventEmitter(), '');
6552
this._initializer = initializer;
6653
}
6754

@@ -84,11 +71,29 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
8471
};
8572
}
8673

87-
async _wrapApiCall<T, K extends channels.Channel>(apiName: string, func: (channel: K) => Promise<T>, logger?: Logger): Promise<T> {
74+
_createChannel(base: Object, apiName: string): T {
75+
const channel = new Proxy(base, {
76+
get: (obj: any, prop) => {
77+
if (prop === 'debugScopeState')
78+
return (params: any) => this._connection.sendMessageToServer(this._guid, prop, params, apiName);
79+
if (typeof prop === 'string') {
80+
const validator = scheme[paramsName(this._type, prop)];
81+
if (validator)
82+
return (params: any) => this._connection.sendMessageToServer(this._guid, prop, validator(params, ''), apiName);
83+
}
84+
return obj[prop];
85+
},
86+
});
87+
(channel as any)._object = this;
88+
return channel;
89+
}
90+
91+
async _wrapApiCall<R, C extends channels.Channel>(apiName: string, func: (channel: C) => Promise<R>, logger?: Logger): Promise<R> {
8892
logger = logger || this._logger;
8993
try {
9094
logApiCall(logger, `=> ${apiName} started`);
91-
const result = await func(this._channel as any);
95+
const channel = this._createChannel({}, apiName);
96+
const result = await func(channel as any);
9297
logApiCall(logger, `<= ${apiName} succeeded`);
9398
return result;
9499
} catch (e) {
@@ -99,15 +104,15 @@ export abstract class ChannelOwner<T extends channels.Channel = channels.Channel
99104
}
100105

101106
_waitForEventInfoBefore(waitId: string, name: string, stack: StackFrame[]) {
102-
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { name, waitId, phase: 'before', stack } }).catch(() => {});
107+
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { name, waitId, phase: 'before', stack } }, undefined).catch(() => {});
103108
}
104109

105110
_waitForEventInfoAfter(waitId: string, error?: string) {
106-
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { waitId, phase: 'after', error } }).catch(() => {});
111+
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { waitId, phase: 'after', error } }, undefined).catch(() => {});
107112
}
108113

109114
_waitForEventInfoLog(waitId: string, message: string) {
110-
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { waitId, phase: 'log', message } }).catch(() => {});
115+
this._connection.sendMessageToServer(this._guid, 'waitForEventInfo', { info: { waitId, phase: 'log', message } }, undefined).catch(() => {});
111116
}
112117

113118
private toJSON() {

src/client/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ export class Connection {
7171
return this._objects.get(guid)!;
7272
}
7373

74-
async sendMessageToServer(guid: string, method: string, params: any): Promise<any> {
74+
async sendMessageToServer(guid: string, method: string, params: any, apiName: string | undefined): Promise<any> {
7575
const { stack, frames } = captureStackTrace();
7676
const id = ++this._lastId;
7777
const converted = { id, guid, method, params };
7878
// Do not include metadata in debug logs to avoid noise.
7979
debugLogger.log('channel:command', converted);
80-
this.onmessage({ ...converted, metadata: { stack: frames } });
80+
this.onmessage({ ...converted, metadata: { stack: frames, apiName } });
8181
try {
8282
return await new Promise((resolve, reject) => this._callbacks.set(id, { resolve, reject }));
8383
} catch (e) {

src/client/electron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
101101
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
102102
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
103103
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
104-
const waiter = Waiter.createForEvent(this, event);
104+
const waiter = Waiter.createForEvent(this, 'electronApplication', event);
105105
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
106106
if (event !== Events.ElectronApplication.Close)
107107
waiter.rejectOnEvent(this, Events.ElectronApplication.Close, new Error('Electron application closed'));

src/client/frame.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel, channels.FrameIni
113113
async waitForNavigation(options: WaitForNavigationOptions = {}): Promise<network.Response | null> {
114114
return this._wrapApiCall(this._apiName('waitForNavigation'), async (channel: channels.FrameChannel) => {
115115
const waitUntil = verifyLoadState('waitUntil', options.waitUntil === undefined ? 'load' : options.waitUntil);
116-
const waiter = this._setupNavigationWaiter('waitForNavigation', options);
116+
const waiter = this._setupNavigationWaiter(this._apiName('waitForNavigation'), options);
117117

118118
const toUrl = typeof options.url === 'string' ? ` to "${options.url}"` : '';
119119
waiter.log(`waiting for navigation${toUrl} until "${waitUntil}"`);
@@ -150,7 +150,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel, channels.FrameIni
150150
if (this._loadStates.has(state))
151151
return;
152152
return this._wrapApiCall(this._apiName('waitForLoadState'), async (channel: channels.FrameChannel) => {
153-
const waiter = this._setupNavigationWaiter('waitForLoadState', options);
153+
const waiter = this._setupNavigationWaiter(this._apiName('waitForLoadState'), options);
154154
await waiter.waitForEvent<LifecycleEvent>(this._eventEmitter, 'loadstate', s => {
155155
waiter.log(` "${s}" event fired`);
156156
return s === state;

src/client/network.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export class WebSocket extends ChannelOwner<channels.WebSocketChannel, channels.
365365
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
366366
const timeout = this._page._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
367367
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
368-
const waiter = Waiter.createForEvent(this, event);
368+
const waiter = Waiter.createForEvent(this, 'webSocket', event);
369369
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
370370
if (event !== Events.WebSocket.Error)
371371
waiter.rejectOnEvent(this, Events.WebSocket.Error, new Error('Socket error'));

src/client/page.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
397397
private async _waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions, logLine?: string): Promise<any> {
398398
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
399399
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
400-
const waiter = Waiter.createForEvent(this, event);
400+
const waiter = Waiter.createForEvent(this, 'page', event);
401401
if (logLine)
402402
waiter.log(logLine);
403403
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
@@ -644,7 +644,9 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
644644
}
645645

646646
async pause() {
647-
await this.context()._pause();
647+
return this.context()._wrapApiCall('page.pause', async (channel: channels.BrowserContextChannel) => {
648+
await channel.pause();
649+
});
648650
}
649651

650652
async _pdf(options: PDFOptions = {}): Promise<Buffer> {

src/client/waiter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export class Waiter {
3838
];
3939
}
4040

41-
static createForEvent(channelOwner: ChannelOwner, event: string) {
42-
return new Waiter(channelOwner, `waitForEvent(${event})`);
41+
static createForEvent(channelOwner: ChannelOwner, target: string, event: string) {
42+
return new Waiter(channelOwner, `${target}.waitForEvent(${event})`);
4343
}
4444

4545
async waitForEvent<T = void>(emitter: EventEmitter, event: string, predicate?: (arg: T) => boolean): Promise<T> {

src/protocol/channels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type StackFrame = {
3232

3333
export type Metadata = {
3434
stack?: StackFrame[],
35+
apiName?: string,
3536
};
3637

3738
export type WaitForEventInfo = {

0 commit comments

Comments
 (0)