@@ -102,24 +102,43 @@ public async Task<string> StartServerAndGetUrlAsync(
102102
103103 public async Task < IBrowser > SpawnBrowserAsync (
104104 string browserUrl ,
105- bool headless = true
105+ bool headless = true ,
106+ int timeout = 10000 ,
107+ int maxRetries = 3
106108 ) {
107109 var url = new Uri ( browserUrl ) ;
108110 Playwright = await Microsoft . Playwright . Playwright . CreateAsync ( ) ;
109111 // codespaces: ignore certificate error -> Microsoft.Playwright.PlaywrightException : net::ERR_CERT_AUTHORITY_INVALID
110112 string [ ] chromeArgs = new [ ] { $ "--explicitly-allowed-ports={ url . Port } ", "--ignore-certificate-errors" } ;
111113 _testOutput . WriteLine ( $ "Launching chrome ('{ s_chromePath . Value } ') via playwright with args = { string . Join ( ',' , chromeArgs ) } ") ;
112- Browser = await Playwright . Chromium . LaunchAsync ( new BrowserTypeLaunchOptions {
113- ExecutablePath = s_chromePath . Value ,
114- Headless = headless ,
115- Args = chromeArgs
116- } ) ;
117- Browser . Disconnected += ( sender , e ) =>
114+
115+ int attempt = 0 ;
116+ while ( attempt < maxRetries )
118117 {
119- Browser = null ;
120- _testOutput . WriteLine ( "Browser has been disconnected" ) ;
121- } ;
122- return Browser ;
118+ try
119+ {
120+ Browser = await Playwright . Chromium . LaunchAsync ( new BrowserTypeLaunchOptions {
121+ ExecutablePath = s_chromePath . Value ,
122+ Headless = headless ,
123+ Args = chromeArgs ,
124+ Timeout = timeout
125+ } ) ;
126+ Browser . Disconnected += ( sender , e ) =>
127+ {
128+ Browser = null ;
129+ _testOutput . WriteLine ( "Browser has been disconnected" ) ;
130+ } ;
131+ break ;
132+ }
133+ catch ( System . TimeoutException ex )
134+ {
135+ attempt ++ ;
136+ _testOutput . WriteLine ( $ "Attempt { attempt } failed with TimeoutException: { ex . Message } ") ;
137+ }
138+ }
139+ if ( attempt == maxRetries )
140+ throw new Exception ( $ "Failed to launch browser after { maxRetries } attempts") ;
141+ return Browser ! ;
123142 }
124143
125144 // FIXME: options
0 commit comments