|
| 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); |
0 commit comments