1515# specific language governing permissions and limitations
1616# under the License.
1717
18+ from selenium .webdriver .common .bidi .common import command_builder
19+
1820
1921class NetworkEvent :
2022 """Represents a network event."""
@@ -51,18 +53,6 @@ def __init__(self, conn):
5153 self .callbacks = {}
5254 self .subscriptions = {}
5355
54- def command_builder (self , method , params ):
55- """Build a command iterator to send to the network.
56-
57- Parameters:
58- ----------
59- method (str): The method to execute.
60- params (dict): The parameters to pass to the method.
61- """
62- command = {"method" : method , "params" : params }
63- cmd = yield command
64- return cmd
65-
6656 def _add_intercept (self , phases = [], contexts = None , url_patterns = None ):
6757 """Add an intercept to the network.
6858
@@ -88,7 +78,7 @@ def _add_intercept(self, phases=[], contexts=None, url_patterns=None):
8878 params ["phases" ] = phases
8979 else :
9080 params ["phases" ] = ["beforeRequestSent" ]
91- cmd = self . command_builder ("network.addIntercept" , params )
81+ cmd = command_builder ("network.addIntercept" , params )
9282
9383 result = self .conn .execute (cmd )
9484 self .intercepts .append (result ["intercept" ])
@@ -113,11 +103,11 @@ def _remove_intercept(self, intercept=None):
113103 if intercept is None :
114104 intercepts_to_remove = self .intercepts .copy () # create a copy before iterating
115105 for intercept_id in intercepts_to_remove : # remove all intercepts
116- self .conn .execute (self . command_builder ("network.removeIntercept" , {"intercept" : intercept_id }))
106+ self .conn .execute (command_builder ("network.removeIntercept" , {"intercept" : intercept_id }))
117107 self .intercepts .remove (intercept_id )
118108 else :
119109 try :
120- self .conn .execute (self . command_builder ("network.removeIntercept" , {"intercept" : intercept }))
110+ self .conn .execute (command_builder ("network.removeIntercept" , {"intercept" : intercept }))
121111 self .intercepts .remove (intercept )
122112 except Exception as e :
123113 raise Exception (f"Exception: { e } " )
@@ -192,7 +182,7 @@ def add_request_handler(self, event, callback, url_patterns=None, contexts=None)
192182 else :
193183 params = {}
194184 params ["events" ] = [event_name ]
195- self .conn .execute (self . command_builder ("session.subscribe" , params ))
185+ self .conn .execute (command_builder ("session.subscribe" , params ))
196186 self .subscriptions [event_name ] = [callback_id ]
197187
198188 self .callbacks [callback_id ] = result ["intercept" ]
@@ -220,7 +210,7 @@ def remove_request_handler(self, event, callback_id):
220210 if len (self .subscriptions [event_name ]) == 0 :
221211 params = {}
222212 params ["events" ] = [event_name ]
223- self .conn .execute (self . command_builder ("session.unsubscribe" , params ))
213+ self .conn .execute (command_builder ("session.unsubscribe" , params ))
224214 del self .subscriptions [event_name ]
225215
226216 def clear_request_handlers (self ):
@@ -234,7 +224,7 @@ def clear_request_handlers(self):
234224 del self .callbacks [callback_id ]
235225 params = {}
236226 params ["events" ] = [event_name ]
237- self .conn .execute (self . command_builder ("session.unsubscribe" , params ))
227+ self .conn .execute (command_builder ("session.unsubscribe" , params ))
238228 self .subscriptions = {}
239229
240230 def add_auth_handler (self , username , password ):
@@ -294,26 +284,14 @@ def __init__(
294284 self .timings = timings
295285 self .url = url
296286
297- def command_builder (self , method , params ):
298- """Build a command iterator to send to the network.
299-
300- Parameters:
301- ----------
302- method (str): The method to execute.
303- params (dict): The parameters to pass to the method.
304- """
305- command = {"method" : method , "params" : params }
306- cmd = yield command
307- return cmd
308-
309287 def fail_request (self ):
310288 """Fail this request."""
311289
312290 if not self .request_id :
313291 raise ValueError ("Request not found." )
314292
315293 params = {"request" : self .request_id }
316- self .network .conn .execute (self . command_builder ("network.failRequest" , params ))
294+ self .network .conn .execute (command_builder ("network.failRequest" , params ))
317295
318296 def continue_request (self , body = None , method = None , headers = None , cookies = None , url = None ):
319297 """Continue after intercepting this request."""
@@ -333,7 +311,7 @@ def continue_request(self, body=None, method=None, headers=None, cookies=None, u
333311 if url is not None :
334312 params ["url" ] = url
335313
336- self .network .conn .execute (self . command_builder ("network.continueRequest" , params ))
314+ self .network .conn .execute (command_builder ("network.continueRequest" , params ))
337315
338316 def _continue_with_auth (self , username = None , password = None ):
339317 """Continue with authentication.
@@ -358,4 +336,4 @@ def _continue_with_auth(self, username=None, password=None):
358336 params ["action" ] = "provideCredentials"
359337 params ["credentials" ] = {"type" : "password" , "username" : username , "password" : password }
360338
361- self .network .conn .execute (self . command_builder ("network.continueWithAuth" , params ))
339+ self .network .conn .execute (command_builder ("network.continueWithAuth" , params ))
0 commit comments