From d4e9d8d591e4fb1e2a33fe3a586a8beaccf263e8 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Fri, 2 Feb 2024 13:22:07 +0100 Subject: [PATCH] fix!: make console warn level compatible with WebDriver BiDi (#11790) --- docs/api/puppeteer.consolemessagetype.md | 2 +- packages/puppeteer-core/src/bidi/Page.ts | 3 ++- packages/puppeteer-core/src/cdp/Page.ts | 26 ++++++++++++++++--- packages/puppeteer-core/src/cdp/WebWorker.ts | 3 +-- .../src/common/ConsoleMessage.ts | 2 +- test/TestExpectations.json | 2 +- test/src/page.spec.ts | 2 +- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/api/puppeteer.consolemessagetype.md b/docs/api/puppeteer.consolemessagetype.md index 18f7ff0d10b..2d8caf82695 100644 --- a/docs/api/puppeteer.consolemessagetype.md +++ b/docs/api/puppeteer.consolemessagetype.md @@ -14,7 +14,7 @@ export type ConsoleMessageType = | 'debug' | 'info' | 'error' - | 'warning' + | 'warn' | 'dir' | 'dirxml' | 'table' diff --git a/packages/puppeteer-core/src/bidi/Page.ts b/packages/puppeteer-core/src/bidi/Page.ts index 8dfac828154..2796fdb64be 100644 --- a/packages/puppeteer-core/src/bidi/Page.ts +++ b/packages/puppeteer-core/src/bidi/Page.ts @@ -33,6 +33,7 @@ import {Coverage} from '../cdp/Coverage.js'; import {EmulationManager as CdpEmulationManager} from '../cdp/EmulationManager.js'; import {FrameTree} from '../cdp/FrameTree.js'; import {Tracing} from '../cdp/Tracing.js'; +import type {ConsoleMessageType} from '../common/ConsoleMessage.js'; import { ConsoleMessage, type ConsoleMessageLocation, @@ -408,7 +409,7 @@ export class BidiPage extends Page { this.emit( PageEvent.Console, new ConsoleMessage( - event.method as any, + event.method as ConsoleMessageType, text, args, getStackTraceLocations(event.stackTrace) diff --git a/packages/puppeteer-core/src/cdp/Page.ts b/packages/puppeteer-core/src/cdp/Page.ts index ee38792c3bc..1ee682a7376 100644 --- a/packages/puppeteer-core/src/cdp/Page.ts +++ b/packages/puppeteer-core/src/cdp/Page.ts @@ -85,6 +85,15 @@ import { } from './utils.js'; import {CdpWebWorker} from './WebWorker.js'; +function convertConsoleMessageLevel(method: string): ConsoleMessageType { + switch (method) { + case 'warning': + return 'warn'; + default: + return method as ConsoleMessageType; + } +} + /** * @internal */ @@ -475,7 +484,12 @@ export class CdpPage extends Page { if (source !== 'worker') { this.emit( PageEvent.Console, - new ConsoleMessage(level, text, [], [{url, lineNumber}]) + new ConsoleMessage( + convertConsoleMessageLevel(level), + text, + [], + [{url, lineNumber}] + ) ); } } @@ -811,7 +825,11 @@ export class CdpPage extends Page { const values = event.args.map(arg => { return createCdpHandle(context._world, arg); }); - this.#addConsoleMessage(event.type, values, event.stackTrace); + this.#addConsoleMessage( + convertConsoleMessageLevel(event.type), + values, + event.stackTrace + ); } async #onBindingCalled( @@ -843,7 +861,7 @@ export class CdpPage extends Page { } #addConsoleMessage( - eventType: ConsoleMessageType, + eventType: string, args: JSHandle[], stackTrace?: Protocol.Runtime.StackTrace ): void { @@ -875,7 +893,7 @@ export class CdpPage extends Page { } } const message = new ConsoleMessage( - eventType, + convertConsoleMessageLevel(eventType), textTokens.join(' '), args, stackTraceLocations diff --git a/packages/puppeteer-core/src/cdp/WebWorker.ts b/packages/puppeteer-core/src/cdp/WebWorker.ts index 552e8a6cf5c..f2ec4cd08bd 100644 --- a/packages/puppeteer-core/src/cdp/WebWorker.ts +++ b/packages/puppeteer-core/src/cdp/WebWorker.ts @@ -8,7 +8,6 @@ import type {Protocol} from 'devtools-protocol'; import type {CDPSession} from '../api/CDPSession.js'; import type {Realm} from '../api/Realm.js'; import {WebWorker} from '../api/WebWorker.js'; -import type {ConsoleMessageType} from '../common/ConsoleMessage.js'; import {TimeoutSettings} from '../common/TimeoutSettings.js'; import {debugError} from '../common/util.js'; @@ -20,7 +19,7 @@ import {CdpJSHandle} from './JSHandle.js'; * @internal */ export type ConsoleAPICalledCallback = ( - eventType: ConsoleMessageType, + eventType: string, handles: CdpJSHandle[], trace?: Protocol.Runtime.StackTrace ) => void; diff --git a/packages/puppeteer-core/src/common/ConsoleMessage.ts b/packages/puppeteer-core/src/common/ConsoleMessage.ts index 85d2db9f753..c2aad7679db 100644 --- a/packages/puppeteer-core/src/common/ConsoleMessage.ts +++ b/packages/puppeteer-core/src/common/ConsoleMessage.ts @@ -35,7 +35,7 @@ export type ConsoleMessageType = | 'debug' | 'info' | 'error' - | 'warning' + | 'warn' | 'dir' | 'dirxml' | 'table' diff --git a/test/TestExpectations.json b/test/TestExpectations.json index d103a3e8a0b..66b46a3009a 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -922,7 +922,7 @@ "testIdPattern": "[page.spec] Page Page.Events.Console should work for different console API calls with logging functions", "platforms": ["darwin", "linux", "win32"], "parameters": ["webDriverBiDi"], - "expectations": ["FAIL"] + "expectations": ["PASS"] }, { "testIdPattern": "[page.spec] Page Page.Events.error should throw when page crashes", diff --git a/test/src/page.spec.ts b/test/src/page.spec.ts index 271296b5dba..507706c2d22 100644 --- a/test/src/page.spec.ts +++ b/test/src/page.spec.ts @@ -445,7 +445,7 @@ describe('Page', function () { messages.map(msg => { return msg.type(); }) - ).toEqual(['trace', 'dir', 'warning', 'error', 'log']); + ).toEqual(['trace', 'dir', 'warn', 'error', 'log']); expect( messages.map(msg => { return msg.text();