@@ -29,6 +29,11 @@ const NetworkEvent = {
2929 FETCH_ERROR : 'network.fetchError' ,
3030}
3131
32+ const CacheBehavior = Object . freeze ( {
33+ DEFAULT : 'default' ,
34+ BYPASS : 'bypass' ,
35+ } )
36+
3237/**
3338 * Represents all commands and events of Network module.
3439 * Described in https://w3c.github.io/webdriver-bidi/#module-network.
@@ -355,6 +360,40 @@ class Network {
355360 await this . bidi . send ( command )
356361 }
357362
363+ /**
364+ * Sets the cache behavior for network requests.
365+ *
366+ * @param {string } behavior - The cache behavior ("default" or "bypass")
367+ * @param {Array<string> } [contexts] - Optional array of browsing context IDs
368+ * @returns {Promise<void> } A promise that resolves when the cache behavior is set
369+ * @throws {Error } If behavior is invalid or context IDs are invalid
370+ */
371+ async setCacheBehavior ( behavior , contexts = null ) {
372+ if ( ! Object . values ( CacheBehavior ) . includes ( behavior ) ) {
373+ throw new Error ( `Cache behavior must be either "${ CacheBehavior . DEFAULT } " or "${ CacheBehavior . BYPASS } "` )
374+ }
375+
376+ const command = {
377+ method : 'network.setCacheBehavior' ,
378+ params : {
379+ cacheBehavior : behavior ,
380+ } ,
381+ }
382+
383+ if ( contexts !== null ) {
384+ if (
385+ ! Array . isArray ( contexts ) ||
386+ contexts . length === 0 ||
387+ contexts . some ( ( c ) => typeof c !== 'string' || c . trim ( ) === '' )
388+ ) {
389+ throw new Error ( 'Contexts must be an array of non-empty strings' )
390+ }
391+ command . params . contexts = contexts
392+ }
393+
394+ await this . bidi . send ( command )
395+ }
396+
358397 /**
359398 * Unsubscribes from network events for all browsing contexts.
360399 * @returns {Promise<void> } A promise that resolves when the network connection is closed.
@@ -389,4 +428,4 @@ async function getNetworkInstance(driver, browsingContextIds = null) {
389428 return instance
390429}
391430
392- module . exports = getNetworkInstance
431+ module . exports = { Network : getNetworkInstance , CacheBehavior }
0 commit comments