Skip to content

Commit

Permalink
[browser][wbt] Detect when browser gets disconnected and do not try t…
Browse files Browse the repository at this point in the history
…o dispose. (dotnet#101530)
  • Loading branch information
ilonatommy authored Apr 25, 2024
1 parent 5e41dde commit 56048b5
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ public async Task<IBrowser> SpawnBrowserAsync(
// 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)}");
return Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{
Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions{
ExecutablePath = s_chromePath.Value,
Headless = headless,
Args = chromeArgs
});
Browser.Disconnected += (sender, e) =>
{
Browser = null;
_testOutput.WriteLine("Browser has been disconnected");
};
return Browser;
}

// FIXME: options
Expand Down Expand Up @@ -196,8 +202,21 @@ public async Task WaitForProcessExitAsync(TimeSpan timeout)

public async ValueTask DisposeAsync()
{
if (Browser is not null)
await Browser.DisposeAsync();
Playwright?.Dispose();
try
{
if (Browser is not null)
{
await Browser.DisposeAsync();
Browser = null;
}
}
catch (PlaywrightException ex)
{
_testOutput.WriteLine($"PlaywrightException occurred during DisposeAsync: {ex.Message}");
}
finally
{
Playwright?.Dispose();
}
}
}

0 comments on commit 56048b5

Please sign in to comment.