11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading . Tasks ;
45using PuppeteerSharp . Helpers ;
6+ using PuppeteerSharp . Messaging ;
57using PuppeteerSharp . Tests . Attributes ;
68using PuppeteerSharp . Xunit ;
79using Xunit ;
@@ -12,8 +14,17 @@ namespace PuppeteerSharp.Tests.HeadfulTests
1214 [ Collection ( TestConstants . TestFixtureCollectionName ) ]
1315 public class HeadfulTests : PuppeteerBaseTest
1416 {
17+ private readonly LaunchOptions _forcedOopifOptions ;
18+
1519 public HeadfulTests ( ITestOutputHelper output ) : base ( output )
1620 {
21+ _forcedOopifOptions = TestConstants . DefaultBrowserOptions ( ) ;
22+ _forcedOopifOptions . Headless = false ;
23+ _forcedOopifOptions . Devtools = true ;
24+ _forcedOopifOptions . Args = new [ ] {
25+ $ "--host-rules=\" MAP oopifdomain 127.0.0.1\" ",
26+ $ "--isolate-origins=\" { TestConstants . ServerUrl . Replace ( "localhost" , "oopifdomain" ) } \" "
27+ } ;
1728 }
1829
1930 [ PuppeteerTest ( "headful.spec.ts" , "HEADFUL" , "background_page target type should be available" ) ]
@@ -117,6 +128,69 @@ await page.EvaluateFunctionHandleAsync(@"() => {
117128 }
118129 }
119130
131+ [ PuppeteerTest ( "headful.spec.ts" , "HEADFUL" , "OOPIF: should expose events within OOPIFs" ) ]
132+ [ SkipBrowserFact ( skipFirefox : true ) ]
133+ public async Task OOPIFShouldExposeEventsWithinOOPIFs ( )
134+ {
135+ await using ( var browser = await Puppeteer . LaunchAsync ( _forcedOopifOptions ) )
136+ await using ( var page = await browser . NewPageAsync ( ) )
137+ {
138+ // Setup our session listeners to observe OOPIF activity.
139+ var session = await page . Target . CreateCDPSessionAsync ( ) ;
140+ var networkEvents = new List < string > ( ) ;
141+ var otherSessions = new List < CDPSession > ( ) ;
142+
143+ await session . SendAsync ( "Target.setAutoAttach" , new TargetSetAutoAttachRequest
144+ {
145+ AutoAttach = true ,
146+ Flatten = true ,
147+ WaitForDebuggerOnStart = true ,
148+ } ) ;
149+
150+ session . SessionAttached += async ( _ , e ) =>
151+ {
152+ otherSessions . Add ( e . Session ) ;
153+
154+ await e . Session . SendAsync ( "Network.enable" ) ;
155+ await e . Session . SendAsync ( "Runtime.runIfWaitingForDebugger" ) ;
156+
157+ e . Session . MessageReceived += ( _ , e ) =>
158+ {
159+ if ( e . MessageID . Equals ( "Network.requestWillBeSent" , StringComparison . CurrentCultureIgnoreCase ) )
160+ {
161+ networkEvents . Add ( e . MessageData . SelectToken ( "request" ) . SelectToken ( "url" ) . ToObject < string > ( ) ) ;
162+ }
163+ } ;
164+ } ;
165+
166+ // Navigate to the empty page and add an OOPIF iframe with at least one request.
167+ await page . GoToAsync ( TestConstants . EmptyPage ) ;
168+ await page . EvaluateFunctionAsync ( @"(frameUrl) => {
169+ const frame = document.createElement('iframe');
170+ frame.setAttribute('src', frameUrl);
171+ document.body.appendChild(frame);
172+ return new Promise((x, y) => {
173+ frame.onload = x;
174+ frame.onerror = y;
175+ });
176+ }" , TestConstants . ServerUrl . Replace ( "localhost" , "oopifdomain" ) + "/one-style.html" ) ;
177+ await page . WaitForSelectorAsync ( "iframe" ) ;
178+
179+ // Ensure we found the iframe session.
180+ Assert . Single ( otherSessions ) ;
181+
182+ // Resume the iframe and trigger another request.
183+ var iframeSession = otherSessions [ 0 ] ;
184+ await iframeSession . SendAsync ( "Runtime.evaluate" , new RuntimeEvaluateRequest {
185+ Expression = "fetch('/fetch')" ,
186+ AwaitPromise = true ,
187+ } ) ;
188+ await browser . CloseAsync ( ) ;
189+
190+ Assert . Contains ( $ "http://oopifdomain:{ TestConstants . Port } /fetch", networkEvents ) ;
191+ }
192+ }
193+
120194 [ PuppeteerTest ( "headful.spec.ts" , "HEADFUL" , "should close browser with beforeunload page" ) ]
121195 [ SkipBrowserFact ( skipFirefox : true ) ]
122196 public async Task ShouldCloseBrowserWithBeforeunloadPage ( )
@@ -143,7 +217,7 @@ public async Task ShouldOpenDevtoolsWhenDevtoolsTrueOptionIsGiven()
143217 var context = await browser . CreateIncognitoBrowserContextAsync ( ) ;
144218 await Task . WhenAll (
145219 context . NewPageAsync ( ) ,
146- context . WaitForTargetAsync ( target => target . Url . Contains ( "devtools://" ) ) ) ;
220+ browser . WaitForTargetAsync ( target => target . Url . Contains ( "devtools://" ) ) ) ;
147221 }
148222 }
149223 }
0 commit comments