-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into bidi-cookie-ff-125
- Loading branch information
Showing
4 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters