@@ -53,6 +53,7 @@ describe("CDP relay", () => {
5353 ) ;
5454 const written : Array < Record < string , unknown > > = [ ] ;
5555 const relay = makeRelay ( makeTarget ( send ) , ( raw ) => written . push ( JSON . parse ( raw ) ) ) ;
56+ relay . event ( "Target.attachedToTarget" , { sessionId : "iframe-session" } , "" ) ;
5657 relay . receive (
5758 JSON . stringify ( { id : 5 , method : "Page.captureScreenshot" , sessionId : "t3-preview-page" } ) ,
5859 ) ;
@@ -76,6 +77,7 @@ describe("CDP relay", () => {
7677 it ( "resumes paused child frames, so cross-site iframes run" , async ( ) => {
7778 const send = vi . fn ( async ( ) => ( { } ) ) ;
7879 const relay = makeRelay ( makeTarget ( send ) , ( ) => { } ) ;
80+ relay . event ( "Target.attachedToTarget" , { sessionId : "iframe-session" } , "" ) ;
7981 relay . receive (
8082 JSON . stringify ( {
8183 id : 1 ,
@@ -185,6 +187,59 @@ describe("CDP relay popups", () => {
185187 const resultOf = ( written : Array < Record < string , unknown > > , id : number ) =>
186188 written . find ( ( message ) => message [ "id" ] === id ) ! ;
187189
190+ it . each ( [ "browser" , "popup" , "child" ] ) (
191+ "detaches a popup child through its %s session without affecting the other sessions" ,
192+ async ( source ) => {
193+ const pageSend = vi . fn ( async ( ) => ( { } ) ) ;
194+ const popupSend = vi . fn ( async ( ) => ( { } ) ) ;
195+ const written : Array < Record < string , unknown > > = [ ] ;
196+ const relay = makeRelay ( makeTarget ( pageSend ) , ( raw ) => written . push ( JSON . parse ( raw ) ) ) ;
197+ const popup = relay . addPopup ( makePopup ( "POPUP" , popupSend ) ) ;
198+ relay . receive ( JSON . stringify ( { id : 1 , method : "Target.setAutoAttach" } ) ) ;
199+ await settle ( ) ;
200+ const popupSession = "t3-preview-popup-1" ;
201+ popup . event ( "Target.attachedToTarget" , { sessionId : "parent-frame" } , "" ) ;
202+ popup . event ( "Target.attachedToTarget" , { sessionId : "popup-child" } , "parent-frame" ) ;
203+ popup . event ( "Target.attachedToTarget" , { sessionId : "sibling-frame" } , "" ) ;
204+ relay . receive ( JSON . stringify ( { id : 2 , method : "DOM.enable" , sessionId : "popup-child" } ) ) ;
205+ await settle ( ) ;
206+ expect ( popupSend ) . toHaveBeenLastCalledWith ( "DOM.enable" , { } , "popup-child" , undefined ) ;
207+
208+ const sessionId =
209+ source === "browser" ? undefined : source === "popup" ? popupSession : "parent-frame" ;
210+ relay . receive (
211+ JSON . stringify ( {
212+ id : 3 ,
213+ method : "Target.detachFromTarget" ,
214+ params : { sessionId : "popup-child" } ,
215+ sessionId,
216+ } ) ,
217+ ) ;
218+ await settle ( ) ;
219+ expect ( popupSend ) . toHaveBeenLastCalledWith (
220+ "Target.detachFromTarget" ,
221+ { sessionId : "popup-child" } ,
222+ "parent-frame" ,
223+ undefined ,
224+ ) ;
225+ expect ( resultOf ( written , 3 ) ) . toHaveProperty ( "result" ) ;
226+
227+ popupSend . mockClear ( ) ;
228+ relay . receive ( JSON . stringify ( { id : 4 , method : "DOM.enable" , sessionId : "popup-child" } ) ) ;
229+ relay . receive ( JSON . stringify ( { id : 5 , method : "Page.enable" , sessionId : popupSession } ) ) ;
230+ relay . receive ( JSON . stringify ( { id : 6 , method : "DOM.enable" , sessionId : "sibling-frame" } ) ) ;
231+ relay . receive ( JSON . stringify ( { id : 7 , method : "Page.enable" , sessionId : "t3-preview-page" } ) ) ;
232+ await settle ( ) ;
233+ expect ( resultOf ( written , 4 ) ) . toHaveProperty ( "error" ) ;
234+ expect ( popupSend . mock . calls ) . toEqual ( [
235+ [ "Page.enable" , { } , undefined , undefined ] ,
236+ [ "DOM.enable" , { } , "sibling-frame" , undefined ] ,
237+ ] ) ;
238+ expect ( pageSend . mock . calls ) . toEqual ( [ [ "Page.enable" , { } , undefined , undefined ] ] ) ;
239+ for ( const id of [ 5 , 6 , 7 ] ) expect ( resultOf ( written , id ) ) . toHaveProperty ( "result" ) ;
240+ } ,
241+ ) ;
242+
188243 it ( "announces the page's popups after it, as its popups, and routes them separately" , async ( ) => {
189244 const pageSend = vi . fn ( async ( ) => ( { } ) ) ;
190245 const earlySend = vi . fn ( async ( ) => ( { } ) ) ;
0 commit comments