tests(apphost.tests): added support for end to end testing using playwright

This commit is contained in:
Stevan Freeborn
2026-02-23 05:53:17 -06:00
parent fd2cd68d73
commit b45463e56b
11 changed files with 166 additions and 3 deletions
+1
View File
@@ -12,6 +12,7 @@
</Folder>
<Folder Name="/tests/">
<Project Path="tests/FiscalOS.API.Tests/FiscalOS.API.Tests.csproj" />
<Project Path="tests/FiscalOS.AppHost.Tests/FiscalOS.AppHost.Tests.csproj" />
<Project Path="tests/FiscalOS.Core.Tests/FiscalOS.Core.Tests.csproj" />
<Project Path="tests/FiscalOS.Infra.Tests/FiscalOS.Infra.Tests.csproj" />
</Folder>
+5 -3
View File
@@ -1,10 +1,12 @@
using FiscalOS.AppHost;
var builder = DistributedApplication.CreateBuilder(args);
var api = builder.AddProject<Projects.FiscalOS_API>("API")
var api = builder.AddProject<Projects.FiscalOS_API>(ProjectNames.API)
.WithHttpHealthCheck("/health");
builder.AddViteApp("web", "../FiscalOS.Web")
builder.AddViteApp(ProjectNames.Web, "../FiscalOS.Web")
.WithReference(api)
.WithHttpsEndpoint(port: 7061, env: "PORT");
builder.Build().Run();
builder.Build().Run();
+7
View File
@@ -0,0 +1,7 @@
namespace FiscalOS.AppHost;
public static class ProjectNames
{
public const string API = "api";
public const string Web = "web";
}
+3
View File
@@ -3,9 +3,12 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Aspire.Hosting.Testing" Version="13.1.1" />
<PackageVersion Include="AwesomeAssertions" Version="9.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.2" />
<PackageVersion Include="Microsoft.Playwright.Xunit.v3" Version="1.58.0" />
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.3.2" />
<PackageVersion Include="Microsoft.Testing.Extensions.VSTestBridge" Version="2.1.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="xunit.v3.mtp-v2" Version="3.2.2" />
</ItemGroup>
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>FiscalOS.AppHost.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.Testing" />
<PackageReference Include="AwesomeAssertions" />
<PackageReference Include="Microsoft.Playwright.Xunit.v3" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit.v3.mtp-v2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\FiscalOS.AppHost\FiscalOS.AppHost.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,30 @@
using Aspire.Hosting;
using Aspire.Hosting.Testing;
namespace FiscalOS.AppHost.Tests.Infra;
public sealed class AspireFixture : IAsyncLifetime
{
private DistributedApplication _app = null!;
public async ValueTask InitializeAsync()
{
var appHost = await DistributedApplicationTestingBuilder
.CreateAsync<Projects.FiscalOS_AppHost>();
_app = await appHost.BuildAsync();
await _app.StartAsync();
}
public async ValueTask DisposeAsync()
{
await _app.StopAsync();
await _app.DisposeAsync();
}
public Uri GetBaseWebUri()
{
return _app.GetEndpoint(ProjectNames.Web, "https");
}
}
@@ -0,0 +1,48 @@
namespace FiscalOS.AppHost.Tests.Infra;
public abstract class BaseTest(
AspireFixture aspire,
PlaywrightFixture playwright
) : IAsyncLifetime, IClassFixture<AspireFixture>, IClassFixture<PlaywrightFixture>
{
protected AspireFixture AspireFixture { get; } = aspire;
protected PlaywrightFixture PlaywrightFixture { get; } = playwright;
protected IBrowserContext Context { get; private set; } = null!;
protected IPage Page { get; private set; } = null!;
public async ValueTask InitializeAsync()
{
Context = await PlaywrightFixture.Browser.NewContextAsync(new()
{
BaseURL = AspireFixture.GetBaseWebUri().ToString(),
});
Page = await Context.NewPageAsync();
}
public async ValueTask DisposeAsync()
{
await Page.CloseAsync();
await Context.CloseAsync();
await Context.DisposeAsync();
GC.SuppressFinalize(this);
}
public static ILocatorAssertions Expect(ILocator locator)
{
return Assertions.Expect(locator);
}
public static IPageAssertions Expect(IPage page)
{
return Assertions.Expect(page);
}
public static IAPIResponseAssertions Expect(IAPIResponse response)
{
return Assertions.Expect(response);
}
}
@@ -0,0 +1,26 @@
namespace FiscalOS.AppHost.Tests.Infra;
public sealed class PlaywrightFixture : IAsyncLifetime
{
private IPlaywright? _playwright;
internal IBrowser Browser { get; set; } = null!;
public async ValueTask InitializeAsync()
{
_playwright = await Playwright.CreateAsync();
Browser = await _playwright.Chromium.LaunchAsync(new()
{
Headless = false,
});
}
public async ValueTask DisposeAsync()
{
await Browser.CloseAsync();
await Browser.DisposeAsync();
_playwright?.Dispose();
}
}
@@ -0,0 +1,14 @@
namespace FiscalOS.AppHost.Tests;
public class LoginPageTests(
AspireFixture aspire,
PlaywrightFixture playwright
) : BaseTest(aspire, playwright)
{
[Fact]
public async Task LoginPage_WhenNavigatedTo_ItShouldDisplaysLoginButton()
{
await Page.GotoAsync("/public/login");
await Expect(Page.GetByText("Login")).ToBeVisibleAsync();
}
}
+4
View File
@@ -0,0 +1,4 @@
global using FiscalOS.AppHost.Tests.Infra;
global using Microsoft.Playwright;
global using Microsoft.Playwright.Xunit.v3;
@@ -0,0 +1,3 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json"
}