Skip to content

Commit

Permalink
[ci] Fix Playwright test times out/fails because target closed (#100074)
Browse files Browse the repository at this point in the history
* Fix: `OnPageLoaded` did not guarantee WASM got already loaded.

* Trigger the test only on first render, otherwise it would be triggered multiple times.

* Add logs to see why the test timed out.

* Even more logging.

* Most logging won't be useful - remove.

* Prevent no logs when error thrown in the app.

* Cleanup.

* typo

* @maraf's feedback - make waiting deterministic
  • Loading branch information
ilonatommy authored Mar 28, 2024
1 parent 91ee24a commit e203933
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/mono/wasm/Wasm.Build.Tests/Blazor/BlazorRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public record BlazorRunOptions
bool CheckCounter = true,
Dictionary<string, string>? ServerEnvironment = null,
Func<IPage, Task>? Test = null,
Action<IPage>? OnPageLoaded = null,
Action<IPage, IConsoleMessage>? OnConsoleMessage = null,
Action<string>? OnServerMessage = null,
Action<string>? OnErrorMessage = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ public async Task BlazorRunTest(string runArgs,
var page = await runner.RunAsync(
runCommand,
runArgs,
onPageLoaded: runOptions.OnPageLoaded,
onConsoleMessage: OnConsoleMessage,
onServerMessage: runOptions.OnServerMessage,
onError: OnErrorMessage,
Expand Down
6 changes: 1 addition & 5 deletions src/mono/wasm/Wasm.Build.Tests/BrowserRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,20 @@ public async Task<IPage> RunAsync(
string args,
bool headless = true,
Action<IPage, IConsoleMessage>? onConsoleMessage = null,
Action<IPage>? onPageLoaded = null,
Action<string>? onServerMessage = null,
Action<string>? onError = null,
Func<string, string>? modifyBrowserUrl = null)
{
var urlString = await StartServerAndGetUrlAsync(cmd, args, onServerMessage);
var browser = await SpawnBrowserAsync(urlString, headless);
var context = await browser.NewContextAsync();
return await RunAsync(context, urlString, headless, onPageLoaded, onConsoleMessage, onError, modifyBrowserUrl);
return await RunAsync(context, urlString, headless, onConsoleMessage, onError, modifyBrowserUrl);
}

public async Task<IPage> RunAsync(
IBrowserContext context,
string browserUrl,
bool headless = true,
Action<IPage>? onPageLoaded = null,
Action<IPage, IConsoleMessage>? onConsoleMessage = null,
Action<string>? onError = null,
Func<string, string>? modifyBrowserUrl = null,
Expand All @@ -150,8 +148,6 @@ public async Task<IPage> RunAsync(
browserUrl = modifyBrowserUrl(browserUrl);

IPage page = await context.NewPageAsync();
if (onPageLoaded is not null)
page.Load += (_, _) => onPageLoaded(page);

if (onConsoleMessage is not null)
page.Console += (_, msg) => onConsoleMessage(page, msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ private async Task<RunResult> RunSdkStyleApp(RunOptions options, BlazorRunHost h
CheckCounter: false,
Config: options.Configuration,
ServerEnvironment: options.ServerEnvironment,
OnPageLoaded: options.OnPageLoaded,
OnConsoleMessage: OnConsoleMessage,
OnServerMessage: OnServerMessage,
BrowserPath: options.BrowserPath,
Expand Down Expand Up @@ -170,7 +169,6 @@ protected record RunOptions(
string? TestScenario = null,
Dictionary<string, string> BrowserQueryString = null,
Dictionary<string, string> ServerEnvironment = null,
Action<IPage> OnPageLoaded = null,
Action<IPage, IConsoleMessage> OnConsoleMessage = null,
Action<string> OnServerMessage = null,
int? ExpectedExitCode = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,28 @@ public async Task SignalRPassMessages(string config, string transport)
ServerEnvironment: new Dictionary<string, string> { ["ASPNETCORE_ENVIRONMENT"] = "Development" },
BrowserPath: "/chat",
BrowserQueryString: new Dictionary<string, string> { ["transport"] = transport, ["message"] = "ping" },
OnPageLoaded: async page => await page.ClickAsync("button#connectButton"),
OnServerMessage: (msg) => { serverOutput.Add(msg); },
OnServerMessage: (msg) => serverOutput.Add(msg),
OnConsoleMessage: async (page, msg) =>
{
consoleOutput.Add(msg.Text);
if (msg.Text.Contains("TestOutput ->"))
_testOutput.WriteLine(msg.Text);
// prevent timeouts with [Long Running Test] on error
if (msg.Text.ToLowerInvariant().Contains("error"))
{
Console.WriteLine(msg.Text);
Console.WriteLine(_testOutput);
throw new Exception(msg.Text);
}
if (msg.Text.Contains("Finished GetQueryParameters"))
{
// first click after render - make sure buttons are available
await page.WaitForSelectorAsync("button#connectButton");
await page.ClickAsync("button#connectButton");
}
if (msg.Text.Contains("SignalR connected"))
await page.ClickAsync("button#subscribeButton");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@
private List<string> chatMessages = new List<string>();
private string wrongQueryError = "Query string with parameters 'message' and 'transport' are required";

// remove when https://github.com/dotnet/runtime/issues/96546 is fixed
// log that rendering is about to start in case we hit the issue before OnAfterRender is called
protected override bool ShouldRender()
{
bool shouldRender = base.ShouldRender();
Helper.TestOutputWriteLine($"ShouldRender = {shouldRender}");
return shouldRender;
}

protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
Helper.TestOutputWriteLine($"OnAfterRender on CurrentManagedThreadId={Environment.CurrentManagedThreadId}");
GetQueryParameters();
}
base.OnAfterRender(firstRender);

Helper.TestOutputWriteLine($"OnAfterRender on CurrentManagedThreadId={Environment.CurrentManagedThreadId}");
GetQueryParameters();
}

private void GetQueryParameters()
Expand All @@ -49,7 +60,7 @@
}
transport = Helper.GetValue(parameters, "transport");
message = $"{transport} {Helper.GetValue(parameters, "message")}" ;
Helper.TestOutputWriteLine($"GetQueryParameters on CurrentManagedThreadId={Environment.CurrentManagedThreadId} finished");
Helper.TestOutputWriteLine($"Finished GetQueryParameters on CurrentManagedThreadId={Environment.CurrentManagedThreadId}.");
}

private async Task Connect()
Expand Down

0 comments on commit e203933

Please sign in to comment.