Skip to content

Commit dd33baa

Browse files
test: cover reading and navigation browser journeys (#1712)
## Why Existing content smoke checks verified returned HTML but did not exercise how readers find chapters, turn pages, or follow section deep links. This adds browser coverage for that core reading workflow. ## Approach - Share one Playwright journey between local AppHost tests and deployed smoke tests. - Exercise TOC opening, chapter filtering (including no matches), chapter selection, next/previous navigation, section highlighting and viewport positioning, and a fresh anchored URL load. - Assert known placeholder mappings locally and discover deployed links from the actual TOC, without additional login credentials or workflows. - Document targeted execution and request-budget considerations. Generated aliases can share a document. The journey preserves that behavior and checks that arrow buttons omit fragments while TOC links retain anchors. ## Validation - Web and E2E project builds passed with zero warnings or errors. - Chromium journey against local Development Web and disposable SQL Server: **1 passed, 0 failed, 0 skipped**. - `git diff --check` passed; temporary runtime resources were cleaned up. - The AppHost-specific build/run remains blocked by the pre-existing Aspire `NETSDK1228` configuration error. No unrelated Aspire changes were made, and the journey has not been run against a real deployment.
1 parent 74d6c9f commit dd33baa

5 files changed

Lines changed: 201 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.Playwright;
2+
using TUnit.Playwright;
3+
4+
namespace EssentialCSharp.Web.E2ETests;
5+
6+
[Category("Deployed")]
7+
[RequiresDeployedSite]
8+
[CaptureOnFailure]
9+
public class DeployedReadingJourneyTests : PageTest
10+
{
11+
public override IReadOnlyList<string>? TraceContextOrigins =>
12+
[DeployedSite.RequiredBaseUrl.GetLeftPart(UriPartial.Authority)];
13+
14+
[Test]
15+
public async Task Reader_CanFilterContentsTurnPagesAndFollowSectionDeepLinks()
16+
{
17+
await Context.AddCookiesAsync(
18+
[
19+
new Cookie
20+
{
21+
Name = "essential-csharp-consent",
22+
Value = "%7B%22analytics_storage%22%3A%22granted%22%2C%22_version%22%3A%223%22%7D",
23+
Url = DeployedSite.RequiredBaseUrl.GetLeftPart(UriPartial.Authority),
24+
},
25+
]);
26+
27+
await ReadingJourney.RunAsync(Page, DeployedSite.RequiredBaseUrl);
28+
}
29+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System.Text.Json;
2+
using Microsoft.Playwright;
3+
using static Microsoft.Playwright.Assertions;
4+
5+
namespace EssentialCSharp.Web.E2ETests;
6+
7+
internal static class ReadingJourney
8+
{
9+
public static async Task<ReadingJourneyResult> RunAsync(IPage page, Uri baseUrl)
10+
{
11+
await page.AddInitScriptAsync("if (localStorage.getItem('sidebarShown') === null) localStorage.setItem('sidebarShown', 'false');");
12+
await page.GotoAsync(new Uri(baseUrl, "home").ToString());
13+
await page.Locator("#sidebar-toggle-button").ClickAsync();
14+
await Expect(page.Locator("#sidebar")).ToBeVisibleAsync();
15+
16+
string tocJson = await page.EvaluateAsync<string>("() => JSON.stringify(window.TOC_DATA)");
17+
TocEntry[] chapters = JsonSerializer.Deserialize<TocEntry[]>(tocJson, JsonSerializerOptions.Web)
18+
?? throw new InvalidOperationException("The page did not provide table-of-contents data.");
19+
await Assert.That(chapters.Length).IsGreaterThan(1);
20+
TocEntry chapter = chapters[0];
21+
TocEntry section = chapter.Items[0];
22+
ILocator summaries = page.Locator("#toc > ul > li > details > summary");
23+
await Expect(summaries).ToHaveCountAsync(chapters.Length);
24+
await page.GetByRole(AriaRole.Button, new() { Name = "Filter contents", Exact = true }).ClickAsync();
25+
ILocator filter = page.Locator("#toc-filter-input");
26+
await Expect(filter).ToBeVisibleAsync();
27+
await filter.FillAsync("no matching chapter 9e76d102");
28+
await Expect(summaries).ToHaveCountAsync(0);
29+
await filter.FillAsync(chapter.Title);
30+
await Expect(summaries).ToHaveCountAsync(1);
31+
await Expect(summaries).ToHaveTextAsync(chapter.Title);
32+
33+
Uri chapterUrl = Resolve(baseUrl, chapter.Href);
34+
await TocLink(page, chapter.Href).ClickAsync();
35+
await Expect(page).ToHaveURLAsync(chapterUrl.ToString());
36+
await ExpectChapterAsync(page, chapter);
37+
38+
string nextHref = await page.EvaluateAsync<string>("() => window.NEXT_PAGE");
39+
TocEntry next = Flatten(chapters).Single(entry => entry.Href == nextHref);
40+
Uri nextUrl = Resolve(baseUrl, nextHref);
41+
// Arrow buttons intentionally omit fragments; TOC links and keyboard navigation retain them.
42+
await Expect(page.Locator("#next-btn")).ToHaveAttributeAsync("href", next.Key);
43+
await page.Locator("#next-btn").ClickAsync();
44+
await Expect(page).ToHaveURLAsync(WithoutFragment(nextUrl));
45+
await ExpectSectionAsync(page, next, inViewport: false);
46+
47+
string previousHref = await page.EvaluateAsync<string>("() => window.PREVIOUS_PAGE");
48+
await Assert.That(previousHref).IsEqualTo(chapter.Href);
49+
await Expect(page.Locator("#prev-btn")).ToHaveAttributeAsync("href", chapter.Key);
50+
await page.Locator("#prev-btn").ClickAsync();
51+
await Expect(page).ToHaveURLAsync(WithoutFragment(chapterUrl));
52+
await ExpectChapterAsync(page, chapter);
53+
54+
if (section.Items.Length > 0)
55+
{
56+
await TocLink(page, section.Href).Locator("xpath=ancestor::details[1]/summary").ClickAsync();
57+
}
58+
await Expect(TocLink(page, section.Href)).ToBeVisibleAsync();
59+
await TocLink(page, section.Href).ClickAsync();
60+
Uri sectionUrl = Resolve(baseUrl, section.Href);
61+
await Expect(page).ToHaveURLAsync(sectionUrl.ToString());
62+
await ExpectSectionAsync(page, section, inViewport: true);
63+
await Expect(TocLink(page, section.Href)).ToHaveClassAsync(new System.Text.RegularExpressions.Regex("current-section"));
64+
65+
await page.GotoAsync(new Uri(baseUrl, "home").ToString());
66+
IResponse? response = await page.GotoAsync(sectionUrl.ToString());
67+
await Assert.That(response?.Status).IsEqualTo(200);
68+
await Expect(page).ToHaveURLAsync(sectionUrl.ToString());
69+
await ExpectSectionAsync(page, section, inViewport: true);
70+
71+
return new(chapter.Href, section.Href, nextHref, previousHref);
72+
}
73+
74+
private static ILocator TocLink(IPage page, string href) =>
75+
page.Locator($"#toc a[href=\"{EscapeAttribute(href)}\"]");
76+
77+
private static async Task ExpectChapterAsync(IPage page, TocEntry chapter)
78+
{
79+
ILocator heading = page.Locator("#site-layout .container .chapter-title");
80+
await Expect(heading).ToBeVisibleAsync();
81+
await Expect(heading).ToHaveTextAsync(chapter.Title[(chapter.Title.IndexOf(':') + 1)..].Trim());
82+
}
83+
84+
private static async Task ExpectSectionAsync(IPage page, TocEntry section, bool inViewport)
85+
{
86+
string anchor = Uri.UnescapeDataString(new Uri("https://reading.test/" + section.Href).Fragment.TrimStart('#'));
87+
await Assert.That(anchor).IsNotNullOrWhiteSpace();
88+
// Generated pages can repeat an id on a nested span; target the actual section heading container.
89+
ILocator target = page.Locator($"#site-layout .container .section-heading[id=\"{EscapeAttribute(anchor)}\"]");
90+
await Expect(target).ToHaveCountAsync(1);
91+
await Expect(target).ToBeVisibleAsync();
92+
await Expect(target).ToContainTextAsync(section.Title);
93+
if (inViewport)
94+
{
95+
await Expect(target).ToBeInViewportAsync();
96+
}
97+
}
98+
99+
private static Uri Resolve(Uri baseUrl, string href)
100+
{
101+
Uri url = new(baseUrl, href);
102+
if (url.GetLeftPart(UriPartial.Authority) != baseUrl.GetLeftPart(UriPartial.Authority))
103+
{
104+
throw new InvalidOperationException($"Reading link points outside the tested site: {href}");
105+
}
106+
return url;
107+
}
108+
109+
private static string WithoutFragment(Uri url) => new UriBuilder(url) { Fragment = "" }.Uri.ToString();
110+
111+
private static string EscapeAttribute(string value) => value.Replace("\\", "\\\\").Replace("\"", "\\\"");
112+
113+
private static IEnumerable<TocEntry> Flatten(IEnumerable<TocEntry> entries) =>
114+
entries.SelectMany(entry => new[] { entry }.Concat(Flatten(entry.Items)));
115+
116+
internal sealed record TocEntry(string Key, string Href, string Title, TocEntry[] Items);
117+
}
118+
119+
internal sealed record ReadingJourneyResult(string ChapterHref, string SectionHref, string NextHref, string PreviousHref);

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ mode contains no migration tools, browser helpers, local AI, or Development envi
9292
To run only graph checks, use
9393
`dotnet test --project dev\EssentialCSharp.Web.AppHost.Tests --treenode-filter "/*/*/(AppHostGraphTests|WebHostingExtensionsTests)/*"`.
9494

95+
Browser journeys share the fresh-database fixture. The reading journey opens and filters the TOC,
96+
selects a chapter, follows next/previous links, and verifies a rendered section heading both from
97+
its TOC link and a fresh anchored URL load. It uses the repository's `introducing-c` / `hello-world`
98+
placeholder mappings locally; the deployed version discovers links from the real TOC, without
99+
production-only chapter keys or login credentials. Generated aliases can serve the same document:
100+
TOC links retain anchors, while arrow buttons deliberately omit them. The shared journey makes
101+
five content requests (plus two home loads), keeping a deployed suite run below the anonymous
102+
10/minute and 20/hour content limits when run once. Concurrent or repeated runs share the IP budget.
103+
104+
Run only the local reading journey after installing Playwright Chromium:
105+
106+
```powershell
107+
dotnet test --project dev\EssentialCSharp.Web.AppHost.Tests --treenode-filter "/*/*/ReadingJourneyTests/*"
108+
```
109+
110+
`DeployedReadingJourneyTests` runs with the existing `Category=Deployed` promotion gate and
111+
six-hour scheduled checks when `E2E_BASE_URL` is set. No extra workflow or secrets are required.
112+
95113
The AppHost references the Web project so building the AppHost builds Web before the EF migration
96114
resource runs its `--no-build` database update; Web starts only after that update completes successfully.
97115

‎dev/EssentialCSharp.Web.AppHost.Tests/EssentialCSharp.Web.AppHost.Tests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageReference Include="TUnit" />
99
<PackageReference Include="TUnit.Aspire" />
1010
<PackageReference Include="TUnit.Playwright" />
11+
<Compile Include="..\..\EssentialCSharp.Web.E2ETests\ReadingJourney.cs" Link="Journeys\ReadingJourney.cs" />
1112
<PackageReference Include="Aspire.Hosting.Testing" />
1213
<ProjectReference Include="..\EssentialCSharp.Web.AppHost\EssentialCSharp.Web.AppHost.csproj" />
1314
<ProjectReference Include="..\..\EssentialCSharp.Web.Hosting\EssentialCSharp.Web.Hosting.csproj" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using EssentialCSharp.Web.E2ETests;
2+
using Microsoft.Playwright;
3+
using TUnit.Playwright;
4+
5+
namespace EssentialCSharp.Web.AppHost.Tests.Journeys;
6+
7+
[Category("Docker")]
8+
[Category("Journey")]
9+
[RequiresLinuxContainers]
10+
[CaptureOnFailure]
11+
[ClassDataSource<JourneyFixture>(Shared = SharedType.PerTestSession)]
12+
public class ReadingJourneyTests(JourneyFixture fixture) : PageTest
13+
{
14+
[Test]
15+
public async Task Reader_CanFilterContentsTurnPagesAndFollowSectionDeepLinks()
16+
{
17+
await Context.AddCookiesAsync(
18+
[
19+
new Cookie
20+
{
21+
Name = "essential-csharp-consent",
22+
Value = "%7B%22analytics_storage%22%3A%22granted%22%2C%22_version%22%3A%223%22%7D",
23+
Url = fixture.WebUrl.GetLeftPart(UriPartial.Authority),
24+
},
25+
]);
26+
27+
ReadingJourneyResult journey = await ReadingJourney.RunAsync(Page, fixture.WebUrl);
28+
29+
await Assert.That(journey.ChapterHref).IsEqualTo("introducing-c#overview");
30+
await Assert.That(journey.SectionHref).IsEqualTo("hello-world#hello-world");
31+
await Assert.That(journey.NextHref).IsEqualTo("c-syntax-fundamentals#c-syntax-fundamentals");
32+
await Assert.That(journey.PreviousHref).IsEqualTo("introducing-c#overview");
33+
}
34+
}

0 commit comments

Comments
 (0)