Skip to content

Commit

Permalink
Merge branch 'main' into bidi-cookie-ff-125
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Mar 11, 2024
2 parents 6780947 + df1753f commit e271684
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
113 changes: 113 additions & 0 deletions lib/core/engine/command/bidi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import intel from 'intel';
const log = intel.getLogger('browsertime.command.bidi');

/**
* Manages interactions using Bidi. At the moment this only works for Firefox
* but Chrome and maybe other browsers will support it in the future.
* @class
* @hideconstructor
* @see https://w3c.github.io/webdriver-bidi/
*/
export class Bidi {
constructor(engineDelegate, browserName) {
/**
* @private
*/
this.engineDelegate = engineDelegate;
/**
* @private
*/
this.browserName = browserName;
}

/**
* Add a fanction that will get the events that you subscribes.
* @async
* @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 ws = await client.socket;
ws.on('message', f);
} else {
throw new Error('Bidi only supported in Firefox');
}
}

/**
* Retrieves the raw client for Bidi.
* @async
* @example const bidi = await 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() {
if (this.browserName === 'firefox') {
return this.engineDelegate.getBidi();
} else {
throw new Error('Bidi only supported in Firefox');
}
}

/**
* Subscribe to a event.
* @async
* @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();
return client.subscribe(messageType, [
await this.engineDelegate.getWindowHandle()
]);
} else {
throw new Error('Bidi only supported in Firefox');
}
}

/**
* Unsubscribe to an event.
* @async
* @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();
return client.unsubscribe(messageType, [
this.engineDelegate.getWindowHandle()
]);
} else {
throw new Error('Bidi only supported in Firefox');
}
}

/**
* Sends a command using Bidi.
*
* @async
* @example await commands.bidi.send({});
* @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();
return client.send(parameters);
} catch (error) {
log.error('Could not send to Bidi command %j', parameters);
log.verbose(error);
`Could not send to Bidi command ${parameters} `;
}
} else {
throw new Error('Bidi only supported in Firefox');
}
}
}
8 changes: 8 additions & 0 deletions lib/core/engine/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Meta } from './command/meta.js';
import { Watch as StopWatch } from './command/stopWatch.js';
import { Select } from './command/select.js';
import { Debug } from './command/debug.js';
import { Bidi } from './command/bidi.js';
import { AndroidCommand } from './command/android.js';
import { ChromeDevelopmentToolsProtocol } from './command/chromeDevToolsProtocol.js';
import { ChromeTrace } from './command/chromeTrace.js';
Expand Down Expand Up @@ -200,6 +201,13 @@ export class Commands {
*/
this.cdp = cdp;

/**
*
* Use WebDriver Bidi. Availible in Firefox and in the future more browsers.
* @type {Bidi}
*/
this.bidi = new Bidi(engineDelegate, options.browser);

/**
* Provides commands for interacting with an Android device.
* @type {AndroidCommand}
Expand Down
20 changes: 16 additions & 4 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,35 @@ export class Firefox {
* configure what you need.
*/
async afterBrowserStart(runner) {
this.windowId = await runner.getDriver().getWindowHandle();

if (this.firefoxConfig.bidihar) {
const windowId = await runner.getDriver().getWindowHandle();
this.har = new adapters.SeleniumBiDiHarRecorder({
browsingContextIds: [windowId],
browsingContextIds: [this.windowId],
debugLogs:
this.options.verbose >= 2 || this.firefoxConfig.enableBidiHarLog,
driver: runner.getDriver()
});
}
const windowId = await runner.getDriver().getWindowHandle();

this.bidi = new FirefoxBidi(
await runner.getDriver().getBidi(),
windowId,
this.windowId,
this.options
);
}

/**
* Get the Bidi client (used by scripting) for browsers that supports it.
*/
getBidi() {
return this.bidi;
}

getWindowHandle() {
return this.windowId;
}

/**
* Before the first iteration of your tests start.
*/
Expand Down
9 changes: 9 additions & 0 deletions test/data/commandscripts/firefox.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module.exports = async function (context, commands) {

const params = {
method: 'script.addPreloadScript',
params: {
functionDeclaration: 'function(){alert("hepp");}'
}
};

await commands.bidi.send(params);

await commands.profiler.start();
await commands.measure.start('http://127.0.0.1:3000/simple/');
return commands.profiler.stop();
Expand Down

0 comments on commit e271684

Please sign in to comment.