Converting Tests back to C# but still with Playwright

This commit is contained in:
2026-06-03 14:45:18 -04:00
parent 85834466f1
commit 46150d3a69
209 changed files with 1503 additions and 683 deletions
+106
View File
@@ -0,0 +1,106 @@
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Tests.Helpers;
public static class LocalServer
{
private static Process? _process;
public static string? BaseUrl { get; private set; }
public static async Task StartAsync()
{
if (_process != null) return;
var root = FindProjectRoot();
var webProject = Path.Combine(root, "Web");
Console.WriteLine($"[DEBUG_LOG] Starting local server for project: {webProject}");
_process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"run --project \"{webProject}\" --urls http://127.0.0.1:0",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = root
}
};
var tcs = new TaskCompletionSource<string>();
_process.OutputDataReceived += (s, e) =>
{
if (string.IsNullOrEmpty(e.Data)) return;
Console.WriteLine($"[SERVER] {e.Data}");
var match = Regex.Match(e.Data, @"Now listening on:\s+(http://127.0.0.1:\d+)");
if (match.Success)
{
tcs.TrySetResult(match.Groups[1].Value);
}
};
_process.ErrorDataReceived += (s, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
Console.Error.WriteLine($"[SERVER ERROR] {e.Data}");
};
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
// Wait for the URL to be parsed from output
BaseUrl = await Task.WhenAny(tcs.Task, Task.Delay(30000)) == tcs.Task
? tcs.Task.Result
: null;
if (BaseUrl == null)
{
Stop();
throw new Exception("Timeout waiting for local server to start and provide a URL.");
}
Console.WriteLine($"[DEBUG_LOG] Local server started at: {BaseUrl}");
}
public static void Stop()
{
if (_process != null && !_process.HasExited)
{
Console.WriteLine("[DEBUG_LOG] Stopping local server...");
_process.Kill(true);
_process.Dispose();
_process = null;
}
}
private static string FindProjectRoot()
{
var current = AppDomain.CurrentDomain.BaseDirectory;
while (!string.IsNullOrEmpty(current) && !File.Exists(Path.Combine(current, "IGP.sln")))
{
var parent = Path.GetDirectoryName(current);
if (parent == current) break;
current = parent;
}
if (string.IsNullOrEmpty(current) || !File.Exists(Path.Combine(current, "IGP.sln")))
{
// Fallback to searching up from current directory if BaseDirectory fails
current = Directory.GetCurrentDirectory();
while (!string.IsNullOrEmpty(current) && !File.Exists(Path.Combine(current, "IGP.sln")))
{
var parent = Path.GetDirectoryName(current);
if (parent == current) break;
current = parent;
}
}
return current ?? throw new Exception("Could not find project root containing IGP.sln");
}
}
+50
View File
@@ -0,0 +1,50 @@
using Microsoft.Playwright;
using Tests.Pages;
using Tests.Shared;
namespace Tests.Helpers;
public class Website
{
public IPage Page { get; }
public bool RunAgainstProduction { get; }
public string BaseUrl { get; }
public Website(IPage page)
{
Page = page;
RunAgainstProduction = Environment.GetEnvironmentVariable("RUN_AGAINST_PRODUCTION") == "true";
BaseUrl = RunAgainstProduction ? "https://igpfanreference.ca" : (LocalServer.BaseUrl ?? "http://localhost:5234");
NavigationBar = new NavigationBar(this);
SearchDialog = new SearchDialog(this);
BuildCalculatorPage = new BuildCalculatorPage(this);
HarassCalculatorPage = new HarassCalculatorPage(this);
DatabasePage = new DatabasePage(this);
DatabaseSinglePage = new DatabaseSinglePage(this);
}
public ILocator Locator(string selector) => Page.Locator(selector);
public ILocator FindById(string id) => Page.Locator($"#{id}");
public NavigationBar NavigationBar { get; }
public SearchDialog SearchDialog { get; }
public BuildCalculatorPage BuildCalculatorPage { get; }
public HarassCalculatorPage HarassCalculatorPage { get; }
public DatabasePage DatabasePage { get; }
public DatabaseSinglePage DatabaseSinglePage { get; }
public async Task GotoAsync(string? path = null)
{
var url = path is null ? BaseUrl : $"{BaseUrl}/{path}";
await Page.GotoAsync(url);
}
public async Task ClickElementAsync(ILocator locator) => await locator.ClickAsync();
public async Task EnterInputAsync(ILocator locator, string value)
{
await locator.FillAsync(value);
await locator.PressAsync("Enter");
}
}