22using System . Net ;
33using System . Runtime . CompilerServices ;
44using Microsoft . Playwright ;
5+ using PrompterOne . Testing ;
56
67namespace PrompterOne . Web . UITests ;
78
@@ -79,11 +80,8 @@ private async Task<IPage> CreateAdditionalPageAsync()
7980
8081 try
8182 {
82- var page = await context . NewPageAsync ( ) ;
83- PreparePage ( page ) ;
84- await PrimeIsolatedBrowserStorageAsync ( page ) ;
85- await WarmUpContextPageIfNeededAsync ( page , BaseAddress ) ;
86- return page ;
83+ await WarmUpContextWithSacrificialPageAsync ( context ) ;
84+ return await CreatePrimedPageAsync ( context ) ;
8785 }
8886 catch ( PlaywrightException exception ) when ( attempt < ContextBootstrapAttemptCount && IsBrowserClosedException ( exception ) )
8987 {
@@ -104,20 +102,13 @@ private async Task<IPage> CreateSharedPageAsync(string contextKey)
104102
105103 try
106104 {
107- var page = await context . NewPageAsync ( ) ;
108- PreparePage ( page ) ;
109-
110105 if ( isNewSharedContext )
111106 {
112- await PrimeIsolatedBrowserStorageAsync ( page ) ;
113- await WarmUpContextPageIfNeededAsync ( page , BaseAddress , warmAllRuntimeRoutes : true ) ;
114- }
115- else
116- {
117- await page . GotoAsync ( $ "{ BaseAddress } { UiTestHostConstants . BlankPagePath } ") ;
107+ await WarmUpContextWithSacrificialPageAsync ( context , warmAllRuntimeRoutes : true ) ;
108+ return await CreatePrimedPageAsync ( context ) ;
118109 }
119110
120- return page ;
111+ return await CreateBlankPageAsync ( context ) ;
121112 }
122113 catch ( PlaywrightException exception ) when ( attempt < ContextBootstrapAttemptCount && IsBrowserClosedException ( exception ) )
123114 {
@@ -241,9 +232,61 @@ private static bool IsBrowserClosedException(PlaywrightException exception) =>
241232 private static bool IsContextWarmupFailure ( InvalidOperationException exception ) =>
242233 exception . Message . StartsWith ( "Browser context warmup failed." , StringComparison . Ordinal ) ;
243234
235+ private async Task < IPage > CreatePrimedPageAsync ( IBrowserContext context )
236+ {
237+ var page = await context . NewPageAsync ( ) ;
238+ PreparePage ( page ) ;
239+ await PrimeIsolatedBrowserStorageAsync ( page ) ;
240+ return page ;
241+ }
242+
243+ private async Task < IPage > CreateBlankPageAsync ( IBrowserContext context )
244+ {
245+ var page = await context . NewPageAsync ( ) ;
246+ PreparePage ( page ) ;
247+ await page . GotoAsync ( $ "{ BaseAddress } { UiTestHostConstants . BlankPagePath } ") ;
248+ return page ;
249+ }
250+
251+ private async Task WarmUpContextWithSacrificialPageAsync ( IBrowserContext context , bool warmAllRuntimeRoutes = false )
252+ {
253+ if ( ! TestEnvironment . IsCiEnvironment )
254+ {
255+ return ;
256+ }
257+
258+ var warmupPage = await context . NewPageAsync ( ) ;
259+
260+ try
261+ {
262+ // Warm a disposable page so the returned test page stays clean and isolated.
263+ PreparePage ( warmupPage ) ;
264+ await PrimeIsolatedBrowserStorageAsync ( warmupPage ) ;
265+ await WarmUpContextPageIfNeededAsync ( warmupPage , BaseAddress , warmAllRuntimeRoutes ) ;
266+ }
267+ finally
268+ {
269+ await ClosePageAsync ( warmupPage ) ;
270+ }
271+ }
272+
244273 private Task PrimeIsolatedBrowserStorageAsync ( IPage page ) =>
245274 PrimeIsolatedBrowserStorageAsync ( page , BaseAddress ) ;
246275
276+ private static async Task ClosePageAsync ( IPage page )
277+ {
278+ try
279+ {
280+ if ( ! page . IsClosed )
281+ {
282+ await page . CloseAsync ( ) ;
283+ }
284+ }
285+ catch
286+ {
287+ }
288+ }
289+
247290 private static void PreparePage ( IPage page )
248291 {
249292 page . SetDefaultNavigationTimeout ( BrowserTestConstants . Timing . DefaultNavigationTimeoutMs ) ;
0 commit comments