File: Integration\Playwright\Infrastructure\ResourcePaneNavigation.cs
Web Access
Project: src\tests\Aspire.Dashboard.Playwright.Tests\Aspire.Dashboard.Playwright.Tests.csproj (Aspire.Dashboard.Playwright.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Text.RegularExpressions;
using Microsoft.Playwright;
 
namespace Aspire.Dashboard.Tests.Integration.Playwright.Infrastructure;
 
internal static class ResourcePaneNavigation
{
    /// <summary>
    /// Matches the dashboard's home URL, "/", where the resource list fills the page as a table.
    /// </summary>
    public static readonly Regex HomeUrl = new(@"^https?://[^/]+/$");
 
    /// <summary>
    /// The brand in the header, which is the home button on desktop.
    /// </summary>
    public static ILocator HomeLink(IPage page) => page.Locator("header a.brand-home");
 
    /// <summary>
    /// What switches between a page with the list docked beside it and the home table: the brand while the list is
    /// docked, and the table's "All resources" row, which docks it again, at home. Exactly one of them matches.
    /// </summary>
    public static ILocator TableToggle(IPage page) =>
        page.Locator(".layout:not(:has(.pane-full-canvas)) header a.brand-home, .pane-full-canvas .resource-row-all .resource-row-link");
 
    /// <summary>
    /// Goes to the home table with the brand, as a user would on desktop.
    /// </summary>
    public static async Task GoHomeAsync(IPage page)
    {
        await HomeLink(page).ClickAsync();
        await Assertions.Expect(page).ToHaveURLAsync(HomeUrl);
        await Assertions.Expect(page.Locator(".resources-layout.pane-full-canvas")).ToBeVisibleAsync();
    }
 
    /// <summary>
    /// Opens the main navigation menu from the header's button, which narrow layouts show in place of the tab strip.
    /// </summary>
    public static async Task<ILocator> OpenNavMenuAsync(IPage page)
    {
        await page.Locator(".navigation-button").ClickAsync();
        var menu = page.Locator("fluent-menu-list.mobile-nav-menu");
        await Assertions.Expect(menu).ToBeVisibleAsync();
        return menu;
    }
 
    /// <summary>
    /// An entry in the main navigation menu, found by its label.
    /// </summary>
    public static ILocator NavMenuItem(ILocator menu, string text) =>
        menu.Locator($"fluent-menu-item[data-tooltip='{text}']");
 
    /// <summary>
    /// Opens the overview of all resources. The dashboard's home page, "/", shows the resource list with no view open:
    /// the full-page table on desktop, the open drawer on narrow layouts. Its "All resources" row opens the overview at
    /// /resources. /resources itself already shows the overview.
    /// </summary>
    public static async Task OpenAllResourcesOverviewAsync(IPage page)
    {
        await Assertions.Expect(page.Locator(".resources-layout")).ToHaveAttributeAsync("data-pane-interactions-ready", "true");
        if (await page.Locator(".resources-layout[data-list-home='true']").CountAsync() > 0)
        {
            await page.Locator(".resource-row-all .resource-row-link").ClickAsync();
            await Assertions.Expect(page.Locator(".resources-layout[data-list-home='true']")).ToHaveCountAsync(0);
        }
    }
 
    /// <summary>
    /// Switches between a page with the resource list docked beside it and the full-page table. The table is the home
    /// page, "/", opened from the brand in the header. Its "All resources" row opens the overview of all resources,
    /// /resources, with the list docked.
    /// </summary>
    public static async Task ToggleResourceListAsync(IPage page)
    {
        await Assertions.Expect(page.Locator(".resources-layout")).ToHaveAttributeAsync("data-pane-interactions-ready", "true");
        var isFullCanvas = await page.Locator(".pane-full-canvas").CountAsync() > 0;
        var origin = new Uri(page.Url).GetLeftPart(UriPartial.Authority);
        await TableToggle(page).ClickAsync();
        await Assertions.Expect(page).ToHaveURLAsync(origin + (isFullCanvas ? "/resources" : "/"));
        await Assertions.Expect(page.Locator(".pane-full-canvas")).ToHaveCountAsync(isFullCanvas ? 0 : 1);
        // The button navigates as well as changing the presentation. Wait for both the router
        // and browser URL before testing the result.
        await page.WaitForFunctionAsync("""
            () => {
                const layout = document.querySelector('.resources-layout');
                return layout.dataset.paneLocation === location.href &&
                    !layout.getAnimations().some(animation => animation.playState === 'running');
            }
            """);
    }
}