Skip to content

Commit

Permalink
Bidi command fixes (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Mar 13, 2024
1 parent 03c80e2 commit 74cbb1f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
35 changes: 26 additions & 9 deletions lib/core/engine/command/bidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Object>} 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 {
Expand All @@ -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<Object>} 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()
]);
Expand All @@ -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<Object>} 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()
]);
Expand All @@ -91,18 +97,29 @@ 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<Object>} A promise that resolves when the command has been sent.
*/
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} `;
}
Expand Down
7 changes: 1 addition & 6 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit 74cbb1f

Please sign in to comment.