From e9ed46e402146bc83dd13611efb2a445ef53430f Mon Sep 17 00:00:00 2001 From: Amir Pournasserian Date: Tue, 1 Dec 2026 10:25:15 -0500 Subject: [PATCH 1/3] Wait for the router's heading focus before a page is ready --- CONTRIBUTING.md | 2 +- .../Harness/GalleryFixture.cs | 18 ++++++++++--- .../Samples/HarnessGuardTests.cs | 25 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d70a1a7..9169be3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -202,7 +202,7 @@ $env:FLUENTCMS_BROWSER = "firefox" dotnet test --project tests/FluentCMS.Components.Browser --filter-class "*AccessibilitySamples" ``` -A test is written once for the three modes. The mode comes from `[ModeData]`. `OpenAsync` requires a successful document response at the address it asked for (a redirect fails), extends a query that the path brings, and waits for the requested mode; in an interactive mode it also waits for `data-interactive="true"`. Before an axe scan starts, the fixture checks that the document's computed colour scheme and direction, and its density, match the request. A 404 page or a second LTR scan in place of RTL fails the test. +A test is written once for the three modes. The mode comes from `[ModeData]`. `OpenAsync` requires a successful document response at the address it asked for (a redirect fails), extends a query that the path brings, and waits for the requested mode; in an interactive mode it also waits for `data-interactive="true"` and for the router to put focus on the `h1`, which can come later. Before an axe scan starts, the fixture checks that the document's computed colour scheme and direction, and its density, match the request. A 404 page or a second LTR scan in place of RTL fails the test. For example: diff --git a/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs b/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs index 03f3e0c..86ddb39 100644 --- a/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs +++ b/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs @@ -125,12 +125,22 @@ public async Task OpenAsync(RenderMode mode, string path, GalleryOp } } - // Returns when the document in the page is ready: in an interactive mode, when it has become interactive. - // A page that is a shell of its own carries the same attributes on its shell, since the main region is the shell's. - public static Task WaitUntilReadyAsync(IPage page, RenderMode mode) + // Returns when the document in the page is ready: in an interactive mode, when it has become interactive and the + // router has put focus on the heading. A page that is a shell of its own carries the same attributes on its shell, + // since the main region is the shell's. + public static async Task WaitUntilReadyAsync(IPage page, RenderMode mode) { string ready = $"[data-render-mode='{mode.Segment()}']" + (mode.IsInteractive() ? "[data-interactive='true']" : ""); - return page.WaitForSelectorAsync(ready, new PageWaitForSelectorOptions { Timeout = (float)InteractiveTimeout.TotalMilliseconds }); + await page.WaitForSelectorAsync(ready, new PageWaitForSelectorOptions { Timeout = (float)InteractiveTimeout.TotalMilliseconds }); + if (mode.IsInteractive()) + { + // FocusOnNavigate (Selector "h1") focuses the heading through a call that can come after the render that + // made the page interactive. A key sent before it would lose its focus to the heading (#182, #193). + await page.WaitForFunctionAsync( + "() => { const heading = document.querySelector('h1'); return !heading || document.activeElement === heading; }", + null, + new PageWaitForFunctionOptions { Timeout = (float)InteractiveTimeout.TotalMilliseconds }); + } } private static async Task CheckAppearanceAsync(IPage page, string name, string expected, string property) diff --git a/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs b/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs index 1f2db94..de73209 100644 --- a/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs +++ b/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs @@ -1,4 +1,5 @@ using FluentCMS.Components.Browser.Harness; +using Microsoft.Playwright; namespace FluentCMS.Components.Browser.Samples; @@ -68,6 +69,30 @@ public async Task OpenRejectsAPageInAnotherDensity(RenderMode mode) Assert.Contains("density: expected 'compact', got 'comfortable'", error.Message); } + // The router's focus on the heading is held back here, as a slow circuit or runtime holds it, so the page says it is + // interactive well before the heading takes focus. Ready must not come before the heading has it (#193). + [Theory] + [ModeData(RenderMode.Server, RenderMode.Wasm)] + public async Task ReadyWaitsForTheRouterToFocusTheHeading(RenderMode mode) + { + await using IBrowserContext context = await gallery.Browser.NewContextAsync(); + await context.AddInitScriptAsync(""" + (() => { + const focus = HTMLElement.prototype.focus; + HTMLElement.prototype.focus = function (...options) { + if (this.localName !== 'h1') return focus.apply(this, options); + setTimeout(() => focus.apply(this, options), 1000); + }; + })(); + """); + IPage page = await context.NewPageAsync(); + await page.GotoAsync(new Uri(gallery.BaseAddress, $"{mode.Segment()}/field").AbsoluteUri); + + await GalleryFixture.WaitUntilReadyAsync(page, mode); + + Assert.True(await page.EvaluateAsync("() => document.activeElement === document.querySelector('main h1')")); + } + [Theory] [InlineData(",")] [InlineData(" , , ")] From 4dc0cc923430fa602288f3510101bb7e51a47fe5 Mon Sep 17 00:00:00 2001 From: Amir Pournasserian Date: Tue, 1 Dec 2026 10:25:15 -0500 Subject: [PATCH 2/3] Let the page's own scroll settle before reading a focus pixel --- .../FluentCMS.Components.Browser/Harness/KeyboardDriver.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs b/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs index 8cad127..4ea5c6c 100644 --- a/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs +++ b/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs @@ -169,13 +169,18 @@ public async Task AssertFocusAsync(string selector) private async Task ReadPixelAsync(ILocator indicator, int xFromLeft) { + // Firefox scrolls to the caret of typed text after the type has returned; under load that scroll can land after + // the one below and take the indicator out of the window again (#193). + await Scrolling.AtRestAsync(page); await indicator.ScrollIntoViewIfNeededAsync(); // The scroll that the Tab before may have started can still be moving the indicator. await Scrolling.AtRestAsync(page); var box = await indicator.BoundingBoxAsync() ?? throw Fail($"No visible box for {indicator}."); + float y = MathF.Floor(box.Y + box.Height / 2); + Scrolling.AssertInWindow(page, y, $"The point read on {indicator}"); byte[] png = await page.ScreenshotAsync(new() { - Clip = new() { X = MathF.Floor(box.X + xFromLeft), Y = MathF.Floor(box.Y + box.Height / 2), Width = 1, Height = 1 }, + Clip = new() { X = MathF.Floor(box.X + xFromLeft), Y = y, Width = 1, Height = 1 }, Scale = ScreenshotScale.Css, }); return await page.EvaluateAsync(""" From d388045195043e943281c398bcb5f342221c634c Mon Sep 17 00:00:00 2001 From: Amir Pournasserian Date: Tue, 1 Dec 2026 11:09:02 -0500 Subject: [PATCH 3/3] Name the page and the point when a ready wait or a pixel read fails --- .../Harness/GalleryFixture.cs | 16 ++++++++++++---- .../Harness/KeyboardDriver.cs | 11 ++++++++--- .../Samples/HarnessGuardTests.cs | 4 ++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs b/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs index 86ddb39..78bdb1f 100644 --- a/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs +++ b/tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs @@ -136,10 +136,18 @@ public static async Task WaitUntilReadyAsync(IPage page, RenderMode mode) { // FocusOnNavigate (Selector "h1") focuses the heading through a call that can come after the render that // made the page interactive. A key sent before it would lose its focus to the heading (#182, #193). - await page.WaitForFunctionAsync( - "() => { const heading = document.querySelector('h1'); return !heading || document.activeElement === heading; }", - null, - new PageWaitForFunctionOptions { Timeout = (float)InteractiveTimeout.TotalMilliseconds }); + try + { + await page.WaitForFunctionAsync( + "() => { const heading = document.querySelector('h1'); return !heading || document.activeElement === heading; }", + null, + new PageWaitForFunctionOptions { Timeout = (float)InteractiveTimeout.TotalMilliseconds }); + } + catch (TimeoutException) + { + string active = await page.EvaluateAsync("() => { const el = document.activeElement; return el ? el.localName + (el.id ? '#' + el.id : '') : 'nothing'; }"); + throw new InvalidOperationException($"{page.Url}: the router never put focus on the h1; focus is on {active}."); + } } } diff --git a/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs b/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs index 4ea5c6c..482c811 100644 --- a/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs +++ b/tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs @@ -176,11 +176,16 @@ private async Task ReadPixelAsync(ILocator indicator, int xFromLeft) // The scroll that the Tab before may have started can still be moving the indicator. await Scrolling.AtRestAsync(page); var box = await indicator.BoundingBoxAsync() ?? throw Fail($"No visible box for {indicator}."); - float y = MathF.Floor(box.Y + box.Height / 2); - Scrolling.AssertInWindow(page, y, $"The point read on {indicator}"); + float x = MathF.Floor(box.X + xFromLeft), y = MathF.Floor(box.Y + box.Height / 2); + var window = page.ViewportSize ?? throw Fail("The page has no viewport size."); + if (x < 0 || x >= window.Width || y < 0 || y >= window.Height) + { + throw Fail($"The point read on {indicator} is at ({x}, {y}), outside the window of {window.Width}x{window.Height}."); + } + byte[] png = await page.ScreenshotAsync(new() { - Clip = new() { X = MathF.Floor(box.X + xFromLeft), Y = y, Width = 1, Height = 1 }, + Clip = new() { X = x, Y = y, Width = 1, Height = 1 }, Scale = ScreenshotScale.Css, }); return await page.EvaluateAsync(""" diff --git a/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs b/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs index de73209..2efed62 100644 --- a/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs +++ b/tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs @@ -86,11 +86,11 @@ await context.AddInitScriptAsync(""" })(); """); IPage page = await context.NewPageAsync(); - await page.GotoAsync(new Uri(gallery.BaseAddress, $"{mode.Segment()}/field").AbsoluteUri); + Assert.True((await page.GotoAsync(new Uri(gallery.BaseAddress, $"{mode.Segment()}/field").AbsoluteUri))?.Ok); await GalleryFixture.WaitUntilReadyAsync(page, mode); - Assert.True(await page.EvaluateAsync("() => document.activeElement === document.querySelector('main h1')")); + Assert.True(await page.EvaluateAsync("() => document.activeElement === document.querySelector('main h1')"), "Ready came before the heading had focus."); } [Theory]