diff --git a/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs b/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs index d72a844abf1e78..19da797813a609 100644 --- a/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs +++ b/src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs @@ -41,7 +41,9 @@ public async Task RunAsync( bool headless = true, Action? onConsoleMessage = null, Action? onError = null, - Func? modifyBrowserUrl = null) + Func? modifyBrowserUrl = null, + int timeout = 10000, + int maxRetries = 3) { TaskCompletionSource urlAvailable = new(); Action outputHandler = msg => @@ -89,17 +91,33 @@ public async Task RunAsync( Playwright = await Microsoft.Playwright.Playwright.CreateAsync(); string[] chromeArgs = new[] { $"--explicitly-allowed-ports={url.Port}" }; _testOutput.WriteLine($"Launching chrome ('{s_chromePath.Value}') via playwright with args = {string.Join(',', chromeArgs)}"); - Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{ - ExecutablePath = s_chromePath.Value, - Headless = headless, - Args = chromeArgs - }); - + int attempt = 0; + while (attempt < maxRetries) + { + try + { + Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{ + ExecutablePath = s_chromePath.Value, + Headless = headless, + Args = chromeArgs, + Timeout = timeout + }); + break; + } + catch (System.TimeoutException ex) + { + attempt++; + _testOutput.WriteLine($"Attempt {attempt} failed with TimeoutException: {ex.Message}"); + } + } + if (attempt == maxRetries) + throw new Exception($"Failed to launch browser after {maxRetries} attempts"); + string browserUrl = urlAvailable.Task.Result; if (modifyBrowserUrl != null) browserUrl = modifyBrowserUrl(browserUrl); - IPage page = await Browser.NewPageAsync(); + IPage page = await Browser!.NewPageAsync(); if (onConsoleMessage is not null) page.Console += (_, msg) => onConsoleMessage(msg);