3636 Geolocation ,
3737 SetCookieParam ,
3838 StorageState ,
39+ WebErrorLocation ,
3940)
40- from playwright ._impl ._artifact import Artifact
4141from playwright ._impl ._cdp_session import CDPSession
4242from playwright ._impl ._clock import Clock
4343from playwright ._impl ._connection import (
5757from playwright ._impl ._helper import (
5858 HarContentPolicy ,
5959 HarMode ,
60- HarRecordingMetadata ,
6160 RouteFromHarNotFoundPolicy ,
6261 RouteHandler ,
6362 RouteHandlerCallback ,
@@ -95,7 +94,13 @@ class BrowserContext(ChannelOwner):
9594 Close = "close" ,
9695 Console = "console" ,
9796 Dialog = "dialog" ,
97+ Download = "download" ,
98+ FrameAttached = "frameattached" ,
99+ FrameDetached = "framedetached" ,
100+ FrameNavigated = "framenavigated" ,
98101 Page = "page" ,
102+ PageClose = "pageclose" ,
103+ PageLoad = "pageload" ,
99104 WebError = "weberror" ,
100105 ServiceWorker = "serviceworker" ,
101106 Request = "request" ,
@@ -125,7 +130,6 @@ def __init__(
125130 self ._videos_dir : Optional [str ] = self ._options .get ("recordVideo" )
126131 self ._tracing = cast (Tracing , from_channel (initializer ["tracing" ]))
127132 self ._debugger : Debugger = cast (Debugger , from_channel (initializer ["debugger" ]))
128- self ._har_recorders : Dict [str , HarRecordingMetadata ] = {}
129133 self ._request : APIRequestContext = from_channel (initializer ["requestContext" ])
130134 self ._request ._timeout_settings = self ._timeout_settings
131135 self ._clock = Clock (self )
@@ -171,6 +175,10 @@ def __init__(
171175 lambda params : self ._on_page_error (
172176 parse_error (params ["error" ]["error" ]),
173177 from_nullable_channel (params ["page" ]),
178+ cast (
179+ WebErrorLocation ,
180+ params .get ("location" ) or {"url" : "" , "line" : 0 , "column" : 0 },
181+ ),
174182 ),
175183 )
176184 self ._channel .on (
@@ -321,7 +329,7 @@ async def _initialize_har_from_options(
321329 content_policy : HarContentPolicy = record_har_content or (
322330 "omit" if record_har_omit_content is True else default_policy
323331 )
324- await self ._record_into_har (
332+ await self ._tracing . _record_into_har (
325333 har = record_har_path ,
326334 page = None ,
327335 url = record_har_url_filter ,
@@ -404,9 +412,7 @@ async def add_init_script(
404412 await self ._channel .send ("addInitScript" , None , dict (source = script ))
405413 )
406414
407- async def expose_binding (
408- self , name : str , callback : Callable , handle : bool = None
409- ) -> Disposable :
415+ async def expose_binding (self , name : str , callback : Callable ) -> Disposable :
410416 for page in self ._pages :
411417 if name in page ._bindings :
412418 raise Error (
@@ -416,9 +422,7 @@ async def expose_binding(
416422 raise Error (f'Function "{ name } " has been already registered' )
417423 self ._bindings [name ] = callback
418424 return from_channel (
419- await self ._channel .send (
420- "exposeBinding" , None , dict (name = name , needsHandle = handle or False )
421- )
425+ await self ._channel .send ("exposeBinding" , None , dict (name = name ))
422426 )
423427
424428 async def expose_function (self , name : str , callback : Callable ) -> Disposable :
@@ -483,35 +487,6 @@ async def unroute_all(
483487 await self ._unroute_internal (self ._routes , [], behavior )
484488 self ._dispose_har_routers ()
485489
486- async def _record_into_har (
487- self ,
488- har : Union [Path , str ],
489- page : Optional [Page ] = None ,
490- url : Union [Pattern [str ], str ] = None ,
491- update_content : HarContentPolicy = None ,
492- update_mode : HarMode = None ,
493- ) -> None :
494- update_content = update_content or "attach"
495- params : Dict [str , Any ] = {
496- "options" : {
497- "zip" : str (har ).endswith (".zip" ),
498- "content" : update_content ,
499- "urlGlob" : url if isinstance (url , str ) else None ,
500- "urlRegexSource" : url .pattern if isinstance (url , Pattern ) else None ,
501- "urlRegexFlags" : (
502- escape_regex_flags (url ) if isinstance (url , Pattern ) else None
503- ),
504- "mode" : update_mode or "minimal" ,
505- }
506- }
507- if page :
508- params ["page" ] = page ._channel
509- har_id = await self ._channel .send ("harStart" , None , params )
510- self ._har_recorders [har_id ] = {
511- "path" : str (har ),
512- "content" : update_content ,
513- }
514-
515490 async def route_from_har (
516491 self ,
517492 har : Union [Path , str ],
@@ -522,7 +497,7 @@ async def route_from_har(
522497 updateMode : HarMode = None ,
523498 ) -> None :
524499 if update :
525- await self ._record_into_har (
500+ await self ._tracing . _record_into_har (
526501 har = har ,
527502 page = None ,
528503 url = url ,
@@ -602,27 +577,7 @@ async def close(self, reason: str = None) -> None:
602577 await self .request .dispose (reason = reason )
603578
604579 async def _inner_close () -> None :
605- for har_id , params in self ._har_recorders .items ():
606- har = cast (
607- Artifact ,
608- from_channel (
609- await self ._channel .send ("harExport" , None , {"harId" : har_id })
610- ),
611- )
612- # Server side will compress artifact if content is attach or if file is .zip.
613- is_compressed = params .get ("content" ) == "attach" or params [
614- "path"
615- ].endswith (".zip" )
616- need_compressed = params ["path" ].endswith (".zip" )
617- if is_compressed and not need_compressed :
618- tmp_path = params ["path" ] + ".tmp"
619- await har .save_as (tmp_path )
620- await self ._connection .local_utils .har_unzip (
621- zipFile = tmp_path , harFile = params ["path" ]
622- )
623- else :
624- await har .save_as (params ["path" ])
625- await har .delete ()
580+ await self ._tracing ._export_all_hars ()
626581
627582 await self ._channel ._connection .wrap_api_call (_inner_close , True )
628583 await self ._channel .send ("close" , None , {"reason" : reason })
@@ -732,10 +687,12 @@ def _on_dialog(self, dialog: Dialog) -> None:
732687 else :
733688 asyncio .create_task (dialog .dismiss ())
734689
735- def _on_page_error (self , error : Error , page : Optional [Page ]) -> None :
690+ def _on_page_error (
691+ self , error : Error , page : Optional [Page ], location : WebErrorLocation
692+ ) -> None :
736693 self .emit (
737694 BrowserContext .Events .WebError ,
738- WebError (self ._loop , self ._dispatcher_fiber , page , error ),
695+ WebError (self ._loop , self ._dispatcher_fiber , page , error , location ),
739696 )
740697 if page :
741698 page .emit (Page .Events .PageError , error )
0 commit comments