diff --git a/packages/puppeteer-core/src/common/bidi/Browser.ts b/packages/puppeteer-core/src/common/bidi/Browser.ts index 4c544352953..07df5b56e37 100644 --- a/packages/puppeteer-core/src/common/bidi/Browser.ts +++ b/packages/puppeteer-core/src/common/bidi/Browser.ts @@ -245,8 +245,7 @@ export class BidiBrowser extends Browser { if (this.#connection.closed) { return; } - // TODO: implement browser.close. - // await this.#connection.send('browser.close', {}); + await this.#connection.send('browser.close', {}); this.#connection.dispose(); await this.#closeCallback?.call(null); } diff --git a/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts b/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts index 628d601901a..4b7eadd2b99 100644 --- a/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts +++ b/packages/puppeteer-core/src/common/bidi/BrowsingContext.ts @@ -236,21 +236,34 @@ export class BrowsingContext extends Realm { } } - async reload(options: WaitForOptions & {timeout: number}): Promise { + async reload( + options: WaitForOptions & {timeout: number} + ): Promise { const {waitUntil = 'load', timeout} = options; const readinessState = lifeCycleToReadinessState.get( getWaitUntilSingle(waitUntil) ) as Bidi.BrowsingContext.ReadinessState; - await waitWithTimeout( - this.connection.send('browsingContext.reload', { - context: this.#id, - wait: readinessState, - }), - 'Navigation', - timeout - ); + try { + const {result} = await waitWithTimeout( + this.connection.send('browsingContext.reload', { + context: this.#id, + wait: readinessState, + }), + 'Navigation', + timeout + ); + + return result.navigation; + } catch (error) { + if (error instanceof ProtocolError) { + error.message += ` at ${this.url}`; + } else if (error instanceof TimeoutError) { + error.message = 'Navigation timeout of ' + timeout + ' ms exceeded'; + } + throw error; + } } async setContent( diff --git a/packages/puppeteer-core/src/common/bidi/Connection.ts b/packages/puppeteer-core/src/common/bidi/Connection.ts index b8876e2521c..4dbfbd85de8 100644 --- a/packages/puppeteer-core/src/common/bidi/Connection.ts +++ b/packages/puppeteer-core/src/common/bidi/Connection.ts @@ -52,6 +52,11 @@ interface Commands { returnType: Bidi.EmptyResult; }; + 'browser.close': { + params: Bidi.EmptyParams; + returnType: Bidi.EmptyResult; + }; + 'browsingContext.activate': { params: Bidi.BrowsingContext.ActivateParameters; returnType: Bidi.EmptyResult; @@ -74,7 +79,7 @@ interface Commands { }; 'browsingContext.reload': { params: Bidi.BrowsingContext.ReloadParameters; - returnType: Bidi.EmptyResult; + returnType: Bidi.BrowsingContext.NavigateResult; }; 'browsingContext.print': { params: Bidi.BrowsingContext.PrintParameters; @@ -281,6 +286,7 @@ export class BidiConnection extends EventEmitter { this.#closed = true; this.#transport.onmessage = undefined; this.#transport.onclose = undefined; + this.#callbacks.clear(); } diff --git a/packages/puppeteer-core/src/common/bidi/Page.ts b/packages/puppeteer-core/src/common/bidi/Page.ts index 27832648249..af6568783c5 100644 --- a/packages/puppeteer-core/src/common/bidi/Page.ts +++ b/packages/puppeteer-core/src/common/bidi/Page.ts @@ -410,23 +410,13 @@ export class BidiPage extends Page { override async reload( options?: WaitForOptions ): Promise { - const [response] = await Promise.all([ - this.waitForResponse(response => { - return ( - response.request().isNavigationRequest() && - response.url() === this.url() - ); - }), - this.mainFrame() - .context() - .reload({ - ...options, - timeout: - options?.timeout ?? this.#timeoutSettings.navigationTimeout(), - }), - ]); - - return response; + const navigationId = await this.mainFrame() + .context() + .reload({ + ...options, + timeout: options?.timeout ?? this.#timeoutSettings.navigationTimeout(), + }); + return this.getNavigationResponse(navigationId); } override setDefaultNavigationTimeout(timeout: number): void { @@ -494,8 +484,7 @@ export class BidiPage extends Page { await this.#cdpEmulationManager.emulateViewport(viewport); this.#viewport = viewport; if (needsReload) { - // TODO: reload seems to hang in BiDi. - // await this.reload(); + await this.reload(); } } diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index 1d2d12451b4..8757931dab6 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -231,8 +231,9 @@ export const withSourcePuppeteerURLIfNone = >( } const original = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => { - // First element is the function. Second element is the caller of this - // function. Third element is the caller of the caller of this function + // First element is the function. + // Second element is the caller of this function. + // Third element is the caller of the caller of this function // which is precisely what we want. return stack[2]; }; diff --git a/test/TestExpectations.json b/test/TestExpectations.json index 2a405121e06..a62bf742a7f 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -1863,7 +1863,7 @@ "testIdPattern": "[emulation.spec] Emulation Page.viewport should detect touch when applying viewport with touches", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["FAIL"] + "expectations": ["PASS"] }, { "testIdPattern": "[emulation.spec] Emulation Page.viewport should get the proper viewport size", @@ -1881,7 +1881,7 @@ "testIdPattern": "[emulation.spec] Emulation Page.viewport should support touch emulation", "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], - "expectations": ["FAIL"] + "expectations": ["PASS"] }, { "testIdPattern": "[evaluation.spec] Evaluation specs \"after each\" hook for \"should transfer 100Mb of data from page to node.js\"",