From 74cbb1f68b2f702e988210c90a9ba22a4d67b32e Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Wed, 13 Mar 2024 01:42:09 +0100 Subject: [PATCH] Bidi command fixes (#2101) --- lib/core/engine/command/bidi.js | 35 ++++++++++++++++++++++++-------- lib/firefox/webdriver/firefox.js | 7 +------ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/core/engine/command/bidi.js b/lib/core/engine/command/bidi.js index a20f01e0f..d718e7f10 100644 --- a/lib/core/engine/command/bidi.js +++ b/lib/core/engine/command/bidi.js @@ -23,12 +23,15 @@ export class Bidi { /** * Add a fanction that will get the events that you subscribes. * @async + * @example await commands.bidi.onMessage(function(event) { + * console.log(event); + * }); * @param {Function} f - The callback function to handle incoming messages. The function will get an event passed on to it. Remember to subscribe to the event. * @throws {Error} Throws an error if the method is called in a browser other than Firefox. */ async onMessage(f) { if (this.browserName === 'firefox') { - const client = await this.engineDelegate.getBidi(); + const client = this.engineDelegate.getBidi(); const ws = await client.socket; ws.on('message', f); } else { @@ -38,12 +41,11 @@ export class Bidi { /** * Retrieves the raw client for Bidi. - * @async - * @example const bidi = await commands.bidi.getRawClient(); + * @example const bidi = commands.bidi.getRawClient(); * @returns {Promise} A promise that resolves to the Bidi client. * @throws {Error} Throws an error if the browser is not supported. */ - async getRawClient() { + getRawClient() { if (this.browserName === 'firefox') { return this.engineDelegate.getBidi(); } else { @@ -54,13 +56,15 @@ export class Bidi { /** * Subscribe to a event. * @async + * @example // Subscribe to requests before they are sent + * await commands.bidi.subscribe('network.beforeRequestSent'); * @param {string} messageType The type of message to subscribe to. * @returns {Promise} A promise that resolves you have subscribed. * @throws {Error} Throws an error if the method is called in a browser other than Firefox. */ async subscribe(messageType) { if (this.browserName === 'firefox') { - const client = await this.engineDelegate.getBidi(); + const client = this.engineDelegate.getBidi(); return client.subscribe(messageType, [ await this.engineDelegate.getWindowHandle() ]); @@ -72,13 +76,15 @@ export class Bidi { /** * Unsubscribe to an event. * @async + * @example // Unsubscribe to requests before they are sent + * await commands.bidi.unsubscribe('network.beforeRequestSent'); * @param {string} messageType The type of message to unsubscribe to. * @returns {Promise} A promise that resolves you have unsubscribed. * @throws {Error} Throws an error if the method is called in a browser other than Firefox. */ async unsubscribe(messageType) { if (this.browserName === 'firefox') { - const client = await this.engineDelegate.getBidi(); + const client = this.engineDelegate.getBidi(); return client.unsubscribe(messageType, [ this.engineDelegate.getWindowHandle() ]); @@ -91,7 +97,14 @@ export class Bidi { * Sends a command using Bidi. * * @async - * @example await commands.bidi.send({}); + * @example + * const params = { + * method: 'script.addPreloadScript', + * params: { + * functionDeclaration: "function() {alert('hello')}" + * } + * }; + * await commands.bidi.send(params); * @param {Object} parameters - The paramaters for the command. * @throws {Error} Throws an error if the browser is not supported or if the command fails. * @returns {Promise} A promise that resolves when the command has been sent. @@ -99,10 +112,14 @@ export class Bidi { async send(parameters) { if (this.browserName === 'firefox') { try { - const client = await this.engineDelegate.getBidi(); + const client = this.engineDelegate.getBidi(); return client.send(parameters); } catch (error) { - log.error('Could not send to Bidi command %j', parameters); + log.error( + 'Could not send to Bidi command %j, error: %s', + parameters, + error + ); log.verbose(error); `Could not send to Bidi command ${parameters} `; } diff --git a/lib/firefox/webdriver/firefox.js b/lib/firefox/webdriver/firefox.js index e7272278f..a9db0bf1a 100644 --- a/lib/firefox/webdriver/firefox.js +++ b/lib/firefox/webdriver/firefox.js @@ -13,7 +13,6 @@ import { GeckoProfiler } from '../geckoProfiler.js'; import { MemoryReport } from '../memoryReport.js'; import { PerfStats } from '../perfStats.js'; import { NetworkManager } from '../networkManager.js'; -import { FirefoxBidi } from '../firefoxBidi.js'; import { getHAR } from '../getHAR.js'; const log = intel.getLogger('browsertime.firefox'); @@ -64,11 +63,7 @@ export class Firefox { }); } - this.bidi = new FirefoxBidi( - await runner.getDriver().getBidi(), - this.windowId, - this.options - ); + this.bidi = await runner.getDriver().getBidi(); } /**