Skip to content

Commit

Permalink
feat: add browser.debugInfo
Browse files Browse the repository at this point in the history
This is an experimental method that would provide
debugging information to help debug issues with Puppeteer.
Currently, it only provides the stacktraces for pending protocol
calls.
  • Loading branch information
OrKoN committed Jan 25, 2024
1 parent b53de4e commit 296d8c4
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/puppeteer-core/src/api/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ export interface BrowserEvents extends Record<EventType, unknown> {
[BrowserEvent.TargetDiscovered]: Protocol.Target.TargetInfo;
}

/**
* @public
* @experimental
*/
export interface DebugInfo {
pendingProtocolErrors: Error[];
}

/**
* {@link Browser} represents a browser instance that is either:
*
Expand Down Expand Up @@ -431,4 +439,16 @@ export abstract class Browser extends EventEmitter<BrowserEvents> {
* @internal
*/
abstract get protocol(): ProtocolType;

/**
* Get debug information from Puppeteer.
*
* @remarks
*
* Currently, includes pending CDP calls. In the future, we might add more info.
*
* @public
* @experimental
*/
abstract get debugInfo(): DebugInfo;
}
7 changes: 7 additions & 0 deletions packages/puppeteer-core/src/bidi/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BrowserEvent,
type BrowserCloseCallback,
type BrowserContextOptions,
type DebugInfo,
} from '../api/Browser.js';
import {BrowserContextEvent} from '../api/BrowserContext.js';
import type {Page} from '../api/Page.js';
Expand Down Expand Up @@ -307,4 +308,10 @@ export class BidiBrowser extends Browser {
this.connection.dispose();
}
}

override get debugInfo(): DebugInfo {
return {
pendingProtocolErrors: this.connection.getPendingProtocolErrors(),
};
}
}
4 changes: 4 additions & 0 deletions packages/puppeteer-core/src/bidi/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ export class BidiConnection
this.unbind();
this.#transport.close();
}

getPendingProtocolErrors(): Error[] {
return this.#callbacks.getPendingProtocolErrors();
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/puppeteer-core/src/cdp/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {ChildProcess} from 'child_process';

import type {Protocol} from 'devtools-protocol';

import type {DebugInfo} from '../api/Browser.js';
import {
Browser as BrowserBase,
BrowserEvent,
Expand Down Expand Up @@ -417,6 +418,12 @@ export class CdpBrowser extends BrowserBase {
#getVersion(): Promise<Protocol.Browser.GetVersionResponse> {
return this.#connection.send('Browser.getVersion');
}

override get debugInfo(): DebugInfo {
return {
pendingProtocolErrors: this.#connection.getPendingProtocolErrors(),
};
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/puppeteer-core/src/cdp/CDPSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,11 @@ export class CdpCDPSession extends CDPSession {
override id(): string {
return this.#sessionId;
}

/**
* @internal
*/
getPendingProtocolErrors(): Error[] {
return this.#callbacks.getPendingProtocolErrors();
}
}
12 changes: 12 additions & 0 deletions packages/puppeteer-core/src/cdp/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ export class Connection extends EventEmitter<CDPSessionEvents> {
): Promise<CDPSession> {
return await this._createSession(targetInfo, false);
}

/**
* @internal
*/
getPendingProtocolErrors(): Error[] {
const result: Error[] = [];
result.push(...this.#callbacks.getPendingProtocolErrors());
for (const session of this.#sessions.values()) {
result.push(...session.getPendingProtocolErrors());
}
return result;
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/puppeteer-core/src/common/CallbackRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ export class CallbackRegistry {
}
this.#callbacks.clear();
}

/**
* @internal
*/
getPendingProtocolErrors(): Error[] {
const result: Error[] = [];
for (const callback of this.#callbacks.values()) {
result.push(
new Error(`${callback.label} timed out. Trace: ${callback.error.stack}`)
);
}
return result;
}
}
/**
* @internal
Expand Down
6 changes: 6 additions & 0 deletions test/TestExpectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[debugInfo.spec] *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[device-request-prompt.spec] *",
"platforms": ["darwin", "linux", "win32"],
Expand Down
36 changes: 36 additions & 0 deletions test/src/debugInfo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright 2024 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/

import expect from 'expect';

import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('DebugInfo', function () {
setupTestBrowserHooks();

describe('Browser.debugInfo', function () {
it.only('should work', async () => {

Check failure on line 15 in test/src/debugInfo.spec.ts

View workflow job for this annotation

GitHub Actions / [Required] Inspect code

Unexpected exclusive mocha test
const {page, browser} = await getTestState();

const promise = page.evaluate(() => {
return new Promise(resolve => {
// @ts-expect-error another context
window.resolve = resolve;
});
});
try {
expect(browser.debugInfo.pendingProtocolErrors).toHaveLength(1);
} finally {
await page.evaluate(() => {
// @ts-expect-error another context
window.resolve();
});
}
await promise;
expect(browser.debugInfo.pendingProtocolErrors).toHaveLength(0);
});
});
});

0 comments on commit 296d8c4

Please sign in to comment.