LANCommander/LANCommander.Server.UI.Tests/Tests/GameEditTests.cs
Pat Hartl 9b5195f1d6 Use web-first Playwright assertions in UI tests
Replace non-waiting Assert.True(await locator.IsVisibleAsync()) checks with
auto-retrying Assertions.Expect(locator).ToBeVisibleAsync()/ToBeHiddenAsync()
to remove flakiness against Blazor's async rendering. Table-row/field page
objects (Metadata/Roles/Users/Profile) now expose ILocator helpers instead of
Task<bool>, so deletion checks wait for the element to disappear.

Also: rename ScreenshotHelper.CaptureIfFailedAsync to CaptureAsync (xUnit v2
cannot expose the test outcome to DisposeAsync, so it always captures the final
page state), drop the unused TestConstants.ServerPort/BaseUrl now that ports
bind dynamically, and fix IClassFixture doc comments to ICollectionFixture.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-23 17:51:34 -05:00

124 lines
3.9 KiB
C#

using LANCommander.Server.UI.Tests.Pages;
using Microsoft.Playwright;
using Xunit.Abstractions;
namespace LANCommander.Server.UI.Tests.Tests;
/// <summary>
/// Tests for the game edit page, covering tab navigation, form fields, and persistence.
/// Uses a game created via the service layer (seeded in ConfiguredServerFixture).
/// </summary>
[Collection("Server")]
public class GameEditTests : IAsyncLifetime
{
private readonly ConfiguredServerFixture _fixture;
private readonly ITestOutputHelper _output;
private IBrowserContext _context = null!;
private IPage _page = null!;
public GameEditTests(ConfiguredServerFixture fixture, ITestOutputHelper output)
{
_fixture = fixture;
_output = output;
}
public async Task InitializeAsync()
{
(_context, _page) = await _fixture.CreateLoggedInPageAsync();
}
public async Task DisposeAsync()
{
await ScreenshotHelper.CaptureAsync(_page, _output);
if (_page != null) await _page.CloseAsync();
if (_context != null) await _context.DisposeAsync();
}
private async Task<GameEditPage> NavigateToGameEditAsync()
{
var editPage = new GameEditPage(_page);
await editPage.NavigateToGameByIdAsync(_fixture.TestGameId);
return editPage;
}
[Fact]
public async Task GameEdit_ShowsGeneralTab()
{
var editPage = await NavigateToGameEditAsync();
var title = await editPage.GetTitleAsync();
Assert.False(string.IsNullOrWhiteSpace(title), "Title input should have a value");
Assert.Contains("/Games/", editPage.GetCurrentUrl());
}
[Fact]
public async Task GameEdit_CanModifyTitle()
{
var editPage = await NavigateToGameEditAsync();
const string modifiedTitle = "Test Game Modified";
await editPage.SetTitleAsync(modifiedTitle);
await editPage.SaveAsync();
// Reload the page to verify persistence
await _page.ReloadAsync();
await _page.WaitForSelectorAsync(".ant-form-item", new() { Timeout = 15000 });
var editPageAfterReload = new GameEditPage(_page);
var titleAfterReload = await editPageAfterReload.GetTitleAsync();
Assert.Equal(modifiedTitle, titleAfterReload);
// Restore original title for other tests
await editPageAfterReload.SetTitleAsync(ConfiguredServerFixture.TestGameTitle);
await editPageAfterReload.SaveAsync();
}
[Fact]
public async Task GameEdit_ShowsAllExpectedTabs()
{
var editPage = await NavigateToGameEditAsync();
var expectedTabs = new[] { "General", "Archives", "Media", "Scripts", "Actions", "Keys" };
foreach (var tab in expectedTabs)
{
Assert.True(await editPage.IsTabVisibleAsync(tab), $"Tab '{tab}' should be visible");
}
}
[Fact]
public async Task GameEdit_CanNavigateToMediaTab()
{
var editPage = await NavigateToGameEditAsync();
await editPage.NavigateToTabAsync("Media");
Assert.Contains("/Media", editPage.GetCurrentUrl());
}
[Fact]
public async Task GameEdit_CanNavigateToScriptsTab()
{
var editPage = await NavigateToGameEditAsync();
await editPage.NavigateToTabAsync("Scripts");
Assert.Contains("/Scripts", editPage.GetCurrentUrl());
}
[Fact]
public async Task GameEdit_HasExportButton()
{
var editPage = await NavigateToGameEditAsync();
Assert.True(await editPage.IsExportButtonVisibleAsync(), "Export button should be visible");
}
[Fact]
public async Task GameEdit_HasSaveButton()
{
var editPage = await NavigateToGameEditAsync();
Assert.True(await editPage.IsSaveButtonVisibleAsync(), "Save button should be visible");
}
}