Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
26 changes: 22 additions & 4 deletions tests/FluentCMS.Components.Browser/Harness/GalleryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,30 @@ public async Task<GalleryPage> 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).
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<string>("() => { 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}.");
}
}
}

private static async Task CheckAppearanceAsync(IPage page, string name, string expected, string property)
Expand Down
12 changes: 11 additions & 1 deletion tests/FluentCMS.Components.Browser/Harness/KeyboardDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,23 @@ public async Task AssertFocusAsync(string selector)

private async Task<string> 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 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 = MathF.Floor(box.Y + box.Height / 2), Width = 1, Height = 1 },
Clip = new() { X = x, Y = y, Width = 1, Height = 1 },
Scale = ScreenshotScale.Css,
});
return await page.EvaluateAsync<string>("""
Expand Down
25 changes: 25 additions & 0 deletions tests/FluentCMS.Components.Browser/Samples/HarnessGuardTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentCMS.Components.Browser.Harness;
using Microsoft.Playwright;

namespace FluentCMS.Components.Browser.Samples;

Expand Down Expand Up @@ -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();
Assert.True((await page.GotoAsync(new Uri(gallery.BaseAddress, $"{mode.Segment()}/field").AbsoluteUri))?.Ok);

await GalleryFixture.WaitUntilReadyAsync(page, mode);

Assert.True(await page.EvaluateAsync<bool>("() => document.activeElement === document.querySelector('main h1')"), "Ready came before the heading had focus.");
}

[Theory]
[InlineData(",")]
[InlineData(" , , ")]
Expand Down
Loading