1
0
Fork 0
mirror of https://github.com/VSadov/Satori.git synced 2025-06-09 17:44:48 +09:00

[wasm] Re-try when browser's WBT fail with System.TimeoutException (#104481)

This commit is contained in:
Ilona Tomkowicz 2024-07-08 12:02:48 +00:00 committed by GitHub
parent 819826683d
commit 26b6161c6b
Signed by: github
GPG key ID: B5690EEEBB952194

View file

@ -102,24 +102,43 @@ internal class BrowserRunner : IAsyncDisposable
public async Task<IBrowser> SpawnBrowserAsync(
string browserUrl,
bool headless = true
bool headless = true,
int timeout = 10000,
int maxRetries = 3
) {
var url = new Uri(browserUrl);
Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
// codespaces: ignore certificate error -> Microsoft.Playwright.PlaywrightException : net::ERR_CERT_AUTHORITY_INVALID
string[] chromeArgs = new[] { $"--explicitly-allowed-ports={url.Port}", "--ignore-certificate-errors" };
_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
});
Browser.Disconnected += (sender, e) =>
int attempt = 0;
while (attempt < maxRetries)
{
Browser = null;
_testOutput.WriteLine("Browser has been disconnected");
};
return Browser;
try
{
Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions {
ExecutablePath = s_chromePath.Value,
Headless = headless,
Args = chromeArgs,
Timeout = timeout
});
Browser.Disconnected += (sender, e) =>
{
Browser = null;
_testOutput.WriteLine("Browser has been disconnected");
};
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");
return Browser!;
}
// FIXME: options