Skip to content

Commit

Permalink
[release/8.0-staging][browser] Fix WBT timeout (30000ms exceeded) (#1…
Browse files Browse the repository at this point in the history
…04947)

* Backport re-tries to launch the browser.
  • Loading branch information
ilonatommy authored Jul 16, 2024
1 parent 57a18ff commit a28aa7d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public async Task<IPage> RunAsync(
bool headless = true,
Action<IConsoleMessage>? onConsoleMessage = null,
Action<string>? onError = null,
Func<string, string>? modifyBrowserUrl = null)
Func<string, string>? modifyBrowserUrl = null,
int timeout = 10000,
int maxRetries = 3)
{
TaskCompletionSource<string> urlAvailable = new();
Action<string?> outputHandler = msg =>
Expand Down Expand Up @@ -89,17 +91,33 @@ public async Task<IPage> 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);

Expand Down

0 comments on commit a28aa7d

Please sign in to comment.