using System.Collections.Concurrent; using System.Diagnostics; using System.Net.WebSockets; using System.Text; using System.Text.Json; using System.Text.Json.Nodes; using Febe.Tests.Browser.TestSupport; namespace Febe.Tests.Web.Support; /// /// A headless Chromium that drives Febe's own interface, like tools/schermate: only addresses on 127.0.0.1, never /// Instagram (spec §2). It speaks the DevTools protocol directly, without the command policy of Febe's own client, /// because here the tests click, type and run scripts in Febe's pages. Inconclusive when Chromium is missing. /// internal sealed class UiBrowser : IAsyncDisposable { /// /// Installed before the page scripts: window.__delays holds { match, ms } rules that hold back a matching /// fetch, so that a test can make one answer arrive late (stale answers, slow pages); window.__done[match] /// counts the held-back requests that are over (answered, failed or aborted). /// public const string FetchDelays = """ (() => { const original = window.fetch.bind(window); window.__delays = []; window.__done = {}; window.fetch = async (input, init) => { const url = typeof input === 'string' ? input : input.url; const rule = window.__delays.find((d) => url.includes(d.match)); if (!rule) return original(input, init); try { await new Promise((resolve) => setTimeout(resolve, rule.ms)); return await original(input, init); } finally { // After the page's own continuation of this answer has run. setTimeout(() => { window.__done[rule.match] = (window.__done[rule.match] || 0) + 1; }, 50); } }; })(); """; private readonly Process _process; private readonly ClientWebSocket _socket; private readonly string _profile; private readonly ConcurrentDictionary> _pending = new(); private readonly CancellationTokenSource _stop = new(); private readonly SemaphoreSlim _send = new(1, 1); private readonly Task _reader; private int _id; private UiBrowser(Process process, ClientWebSocket socket, string profile) { _process = process; _socket = socket; _profile = profile; _reader = Task.Run(ReadLoopAsync); } /// Uncaught exceptions and console errors of the pages (shown when a test fails). public ConcurrentQueue Problems { get; } = new(); public static async Task StartAsync(int width = 1280, int height = 900) { if (!OperatingSystem.IsLinux() || RealBrowser.ChromiumPath() is not { } chromium) { Assert.Inconclusive("Chromium non trovato (impostare FEBE_CHROMIUM): le prove dell'interfaccia nel browser non girano."); throw new UnreachableException(); } var profile = Path.Combine(Path.GetTempPath(), "febe-interfaccia-" + Guid.NewGuid().ToString("N")); var port = LocalTestSite.FreePort(); var info = new ProcessStartInfo(chromium) { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, }; foreach (var argument in new[] { "--headless", "--disable-gpu", "--hide-scrollbars", "--no-first-run", "--no-default-browser-check", "--disable-extensions", "--disable-background-networking", "--disable-component-update", "--no-proxy-server", "--mute-audio", $"--window-size={width},{height}", $"--user-data-dir={profile}", $"--remote-debugging-port={port}", "--remote-debugging-address=127.0.0.1", "about:blank", }) { info.ArgumentList.Add(argument); } if (RealBrowser.IsRoot) { info.ArgumentList.Add("--no-sandbox"); } foreach (var (key, value) in RealBrowser.BrowserEnvironment) { if (value is null) { info.Environment.Remove(key); } else { info.Environment[key] = value; } } var process = Process.Start(info) ?? throw new InvalidOperationException("Chromium non parte."); process.OutputDataReceived += (_, _) => { }; process.ErrorDataReceived += (_, _) => { }; process.BeginOutputReadLine(); process.BeginErrorReadLine(); try { using var http = new HttpClient(new HttpClientHandler { UseProxy = false }) { Timeout = TimeSpan.FromSeconds(2) }; string? debugger = null; var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(20); while (debugger is null && DateTime.UtcNow < deadline) { try { using var list = JsonDocument.Parse(await http.GetStringAsync(new Uri($"http://127.0.0.1:{port}/json/list"))); debugger = list.RootElement.EnumerateArray() .Where(t => t.GetProperty("type").GetString() == "page") .Select(t => t.GetProperty("webSocketDebuggerUrl").GetString()) .FirstOrDefault(); } catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException or JsonException) { // Chromium is still starting. } if (debugger is null) { await Task.Delay(100); } } if (debugger is null) { throw new TimeoutException("Chromium non risponde sulla porta di debug."); } var socket = new ClientWebSocket(); socket.Options.Proxy = null; await socket.ConnectAsync(new Uri(debugger), CancellationToken.None); var browser = new UiBrowser(process, socket, profile); await browser.SendAsync("Runtime.enable"); await browser.SendAsync("Page.enable"); await browser.SendAsync("Log.enable"); return browser; } catch { Kill(process); RealBrowser.TryDelete(profile); throw; } } /// One command of the DevTools protocol; its result (an error of the protocol fails the test). public async Task SendAsync(string method, JsonObject? parameters = null) { var id = Interlocked.Increment(ref _id); var answer = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); _pending[id] = answer; var message = new JsonObject { ["id"] = id, ["method"] = method, ["params"] = parameters ?? new JsonObject() }; await _send.WaitAsync(); try { await _socket.SendAsync(Encoding.UTF8.GetBytes(message.ToJsonString()), WebSocketMessageType.Text, endOfMessage: true, CancellationToken.None); } finally { _send.Release(); } return await answer.Task.WaitAsync(TimeSpan.FromSeconds(30)); } /// Runs a script in the page (promises awaited) and returns its value; an exception fails the test. public async Task EvalAsync(string expression) { var result = await SendAsync("Runtime.evaluate", new JsonObject { ["expression"] = expression, ["awaitPromise"] = true, ["returnByValue"] = true, }); if (result.TryGetProperty("exceptionDetails", out var details)) { var text = details.TryGetProperty("exception", out var ex) && ex.TryGetProperty("description", out var d) ? d.GetString() : details.GetProperty("text").GetString(); throw new AssertFailedException($"Errore nella pagina: {text}\n{Report()}"); } return result.GetProperty("result").TryGetProperty("value", out var value) ? value.Clone() : default; } public async Task EvalAsync(string expression) => (await EvalAsync(expression)).Deserialize(WebTestServer.Json)!; /// /// Polls until a condition of the page holds (an expression, or a promise of one; errors while the page changes /// count as «not yet»). /// public async Task WaitAsync(string condition, string what, int milliseconds = 30_000) { var deadline = DateTime.UtcNow + TimeSpan.FromMilliseconds(milliseconds); string? last = null; while (DateTime.UtcNow < deadline) { try { if ((await EvalAsync($"(async () => Boolean(await ({condition})))()")).ValueKind == JsonValueKind.True) { return; } } catch (AssertFailedException ex) { last = ex.Message; } await Task.Delay(50); } Assert.Fail($"timeout: {what}{(last is null ? string.Empty : "\n" + last)}\n{Report()}"); } /// Polls a condition of the test (the server side) while the page works. public async Task WaitForAsync(Func condition, string what, int milliseconds = 30_000) { var deadline = DateTime.UtcNow + TimeSpan.FromMilliseconds(milliseconds); while (DateTime.UtcNow < deadline) { if (condition()) { return; } await Task.Delay(50); } Assert.Fail($"timeout: {what}\n{Report()}"); } /// A script that runs in every new document before its own scripts. public Task AddInitScriptAsync(string source) => SendAsync("Page.addScriptToEvaluateOnNewDocument", new JsonObject { ["source"] = source }); /// Loads an address of the interface and waits until the frame and the first page are drawn. public async Task OpenAsync(Uri address) { await SendAsync("Page.navigate", new JsonObject { ["url"] = address.AbsoluteUri }); await WaitAsync( $"location.href.startsWith({JsonSerializer.Serialize(address.GetLeftPart(UriPartial.Authority))}) && document.readyState === 'complete' && !document.documentElement.hasAttribute('aria-busy') && document.querySelector('main.pagina .corpo') !== null", $"apertura di {address}"); } /// Goes to an internal address the way a link of the interface does (unsaved-changes guard included). public Task NavigateAsync(string path) => EvalAsync($"import('/js/core/shell.js').then((m) => {{ m.navigate({JsonSerializer.Serialize(path)}); return true; }})"); /// Centre of the first element matching the selector, in viewport pixels (scrolled into view first). public async Task<(double X, double Y, double Width, double Height)> BoxAsync(string selector) { var box = await EvalAsync($$""" (() => { const el = document.querySelector({{JsonSerializer.Serialize(selector)}}); if (!el) return null; el.scrollIntoView({ block: 'center', inline: 'center' }); const r = el.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2, width: r.width, height: r.height }; })() """); Assert.AreEqual(JsonValueKind.Object, box.ValueKind, $"elemento non trovato: {selector}"); return (box.GetProperty("x").GetDouble(), box.GetProperty("y").GetDouble(), box.GetProperty("width").GetDouble(), box.GetProperty("height").GetDouble()); } /// A real mouse event (Input.dispatchMouseEvent): «mousePressed», «mouseMoved», «mouseReleased». public Task MouseAsync(string type, double x, double y, int buttons = 0) => SendAsync("Input.dispatchMouseEvent", new JsonObject { ["type"] = type, ["x"] = x, ["y"] = y, ["button"] = type == "mouseMoved" && buttons == 0 ? "none" : "left", ["buttons"] = buttons, ["clickCount"] = type == "mouseMoved" ? 0 : 1, }); /// What went wrong in the pages so far, for failure messages. public string Report() => Problems.IsEmpty ? string.Empty : "Pagina: " + string.Join(" | ", Problems); public async ValueTask DisposeAsync() { await _stop.CancelAsync(); try { using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(2)); await _socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "fine", timeout.Token); } catch (Exception ex) when (ex is WebSocketException or OperationCanceledException or ObjectDisposedException) { } Kill(_process); try { await _reader.WaitAsync(TimeSpan.FromSeconds(5)); } catch (Exception ex) when (ex is TimeoutException or WebSocketException or OperationCanceledException) { } _socket.Dispose(); _process.Dispose(); _stop.Dispose(); _send.Dispose(); RealBrowser.TryDelete(_profile); } private static void Kill(Process process) { try { if (!process.HasExited) { process.Kill(entireProcessTree: true); process.WaitForExit(5000); } } catch (Exception ex) when (ex is InvalidOperationException or System.ComponentModel.Win32Exception) { } } private async Task ReadLoopAsync() { var buffer = new byte[64 * 1024]; using var message = new MemoryStream(); try { while (!_stop.IsCancellationRequested && _socket.State == WebSocketState.Open) { message.SetLength(0); WebSocketReceiveResult result; do { result = await _socket.ReceiveAsync(buffer, _stop.Token); if (result.MessageType == WebSocketMessageType.Close) { return; } message.Write(buffer, 0, result.Count); } while (!result.EndOfMessage); Dispatch(message.ToArray()); } } catch (Exception ex) when (ex is WebSocketException or OperationCanceledException or ObjectDisposedException) { } finally { foreach (var pending in _pending.Values) { pending.TrySetException(new InvalidOperationException("Chromium si è chiuso.")); } } } private void Dispatch(byte[] data) { using var document = JsonDocument.Parse(data); var root = document.RootElement; if (root.TryGetProperty("id", out var idElement) && _pending.TryRemove(idElement.GetInt32(), out var answer)) { if (root.TryGetProperty("error", out var error)) { answer.TrySetException(new InvalidOperationException("DevTools: " + error.GetRawText())); } else { answer.TrySetResult(root.GetProperty("result").Clone()); } return; } if (!root.TryGetProperty("method", out var method)) { return; } var parameters = root.GetProperty("params"); switch (method.GetString()) { case "Runtime.exceptionThrown": var details = parameters.GetProperty("exceptionDetails"); Problems.Enqueue("eccezione: " + (details.TryGetProperty("exception", out var ex) && ex.TryGetProperty("description", out var d) ? d.GetString() : details.GetProperty("text").GetString())); break; case "Runtime.consoleAPICalled" when parameters.GetProperty("type").GetString() == "error": Problems.Enqueue("console: " + string.Join(' ', parameters.GetProperty("args").EnumerateArray().Select(a => a.TryGetProperty("value", out var v) ? v.ToString() : a.TryGetProperty("description", out var s) ? s.GetString() : "?"))); break; } } }