using System.Diagnostics; using Microsoft.Playwright; namespace OpenTheBox.Web.Tests; /// /// Shared fixture that starts the Blazor WASM dev server and Playwright browser once per test collection. /// public class WebAppFixture : IAsyncLifetime { private Process? _serverProcess; public IBrowser Browser { get; private set; } = null!; public IPlaywright Playwright { get; private set; } = null!; public string BaseUrl { get; private set; } = "http://localhost:5280"; public async Task InitializeAsync() { // Start the Blazor WASM dev server _serverProcess = new Process { StartInfo = new ProcessStartInfo { FileName = "dotnet", Arguments = $"run --project src/OpenTheBox.Web --urls {BaseUrl}", WorkingDirectory = FindSolutionRoot(), RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true } }; _serverProcess.Start(); // Wait for the server to be ready (try connecting) using var httpClient = new HttpClient(); for (int i = 0; i < 120; i++) // up to 2 minutes for WASM compilation { try { var response = await httpClient.GetAsync(BaseUrl); if (response.IsSuccessStatusCode) break; } catch { } await Task.Delay(1000); } // Initialize Playwright Playwright = await Microsoft.Playwright.Playwright.CreateAsync(); Browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = true, Args = ["--window-size=1400,900"] }); } public async Task DisposeAsync() { await Browser.DisposeAsync(); Playwright.Dispose(); if (_serverProcess is { HasExited: false }) { _serverProcess.Kill(entireProcessTree: true); _serverProcess.Dispose(); } } private static string FindSolutionRoot() { var dir = new DirectoryInfo(AppContext.BaseDirectory); while (dir != null) { if (dir.GetFiles("OpenTheBox.slnx").Length > 0) return dir.FullName; dir = dir.Parent; } throw new InvalidOperationException("Could not find solution root (OpenTheBox.slnx)"); } } [CollectionDefinition("WebApp")] public class WebAppCollection : ICollectionFixture;