4646 from_nullable_channel ,
4747)
4848from playwright ._impl ._console_message import ConsoleMessage
49+ from playwright ._impl ._debugger import Debugger
4950from playwright ._impl ._dialog import Dialog
51+ from playwright ._impl ._disposable import Disposable , DisposableStub
5052from playwright ._impl ._errors import Error , TargetClosedError
5153from playwright ._impl ._event_context_manager import EventContextManagerImpl
5254from playwright ._impl ._fetch import APIRequestContext
@@ -122,6 +124,7 @@ def __init__(
122124 self ._base_url : Optional [str ] = self ._options .get ("baseURL" )
123125 self ._videos_dir : Optional [str ] = self ._options .get ("recordVideo" )
124126 self ._tracing = cast (Tracing , from_channel (initializer ["tracing" ]))
127+ self ._debugger : Debugger = from_channel (initializer ["debugger" ])
125128 self ._har_recorders : Dict [str , HarRecordingMetadata ] = {}
126129 self ._request : APIRequestContext = from_channel (initializer ["requestContext" ])
127130 self ._request ._timeout_settings = self ._timeout_settings
@@ -392,16 +395,18 @@ async def set_offline(self, offline: bool) -> None:
392395
393396 async def add_init_script (
394397 self , script : str = None , path : Union [str , Path ] = None
395- ) -> None :
398+ ) -> Disposable :
396399 if path :
397400 script = (await async_readfile (path )).decode ()
398401 if not isinstance (script , str ):
399402 raise Error ("Either path or script parameter must be specified" )
400- await self ._channel .send ("addInitScript" , None , dict (source = script ))
403+ return from_channel (
404+ await self ._channel .send ("addInitScript" , None , dict (source = script ))
405+ )
401406
402407 async def expose_binding (
403408 self , name : str , callback : Callable , handle : bool = None
404- ) -> None :
409+ ) -> Disposable :
405410 for page in self ._pages :
406411 if name in page ._bindings :
407412 raise Error (
@@ -410,16 +415,18 @@ async def expose_binding(
410415 if name in self ._bindings :
411416 raise Error (f'Function "{ name } " has been already registered' )
412417 self ._bindings [name ] = callback
413- await self ._channel .send (
414- "exposeBinding" , None , dict (name = name , needsHandle = handle or False )
418+ return from_channel (
419+ await self ._channel .send (
420+ "exposeBinding" , None , dict (name = name , needsHandle = handle or False )
421+ )
415422 )
416423
417- async def expose_function (self , name : str , callback : Callable ) -> None :
418- await self .expose_binding (name , lambda source , * args : callback (* args ))
424+ async def expose_function (self , name : str , callback : Callable ) -> Disposable :
425+ return await self .expose_binding (name , lambda source , * args : callback (* args ))
419426
420427 async def route (
421428 self , url : URLMatch , handler : RouteHandlerCallback , times : int = None
422- ) -> None :
429+ ) -> DisposableStub :
423430 self ._routes .insert (
424431 0 ,
425432 RouteHandler (
@@ -431,6 +438,7 @@ async def route(
431438 ),
432439 )
433440 await self ._update_interception_patterns ()
441+ return DisposableStub (lambda : self .unroute (url , handler ))
434442
435443 async def unroute (
436444 self , url : URLMatch , handler : Optional [RouteHandlerCallback ] = None
@@ -564,6 +572,9 @@ def expect_event(
564572 waiter .wait_for_event (self , event , predicate )
565573 return EventContextManagerImpl (waiter .result ())
566574
575+ def is_closed (self ) -> bool :
576+ return self ._closing_or_closed
577+
567578 def _on_close (self ) -> None :
568579 self ._closing_or_closed = True
569580 if self ._browser :
@@ -627,6 +638,16 @@ async def storage_state(
627638 await async_writefile (path , json .dumps (result ))
628639 return result
629640
641+ async def set_storage_state (
642+ self , storageState : Union [StorageState , str , Path ]
643+ ) -> None :
644+ state : StorageState
645+ if isinstance (storageState , (str , Path )):
646+ state = json .loads (await async_readfile (storageState ))
647+ elif storageState :
648+ state = storageState
649+ await self ._channel .send ("setStorageState" , None , {"storageState" : state })
650+
630651 def _effective_close_reason (self ) -> Optional [str ]:
631652 if self ._close_reason :
632653 return self ._close_reason
@@ -753,6 +774,10 @@ async def new_cdp_session(self, page: Union[Page, Frame]) -> CDPSession:
753774 def tracing (self ) -> Tracing :
754775 return self ._tracing
755776
777+ @property
778+ def debugger (self ) -> Debugger :
779+ return self ._debugger
780+
756781 @property
757782 def request (self ) -> "APIRequestContext" :
758783 return self ._request
0 commit comments