Files
Febe/tests/Febe.Tests/Browser/BrowserIntegrationTests.cs
Alby96andClaude Opus 5.5 0775a1460f
verifica / controlli (push) Successful in 25s
verifica / prove (push) Successful in 25m9s
verifica / installatore (push) Successful in 7m37s
verifica / immagine (push) Successful in 20m21s
Prove che cadevano con il runner carico (#46)
Le prove cadevano a ogni carico della CI, ogni volta prove diverse. Dietro alcune c'erano
difetti veri: FebeDatabase lasciava i file aperti dopo DisposeAsync (comandi SQLite non
chiusi, pool di lettura chiuso dopo lo scrittore), e il WAL spariva durante lo spostamento
della cartella Dati; la scheda dell'account senza statistiche leggeva i file di tutta la
libreria; l'arresto di FebeApp non aspettava i lavori aggiunti da un avvio in corso.
Chromium ha 120 s per avviarsi e aprire la prima scheda, e il ricollegamento smette appena
il processo cambia.

Nelle prove i limiti di tempo diventano protezioni contro un blocco: attese per condizione
con un aiuto comune, limiti per classe nella prova di scala con il tempo migliore, porta 0 e
stderr di Chromium nel browser delle prove, e la prova di «Scollega» di WebView2 controlla
che il processo esca davvero. Provato su Windows con tutti i core occupati.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:50:40 +01:00

408 lines
19 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Nodes;
using Febe.Browser.Capture;
using Febe.Browser.Cdp;
using Febe.Browser.Download;
using Febe.Browser.Gestures;
using Febe.Browser.Hosting;
using Febe.Browser.RemoteView;
using Febe.Browser.Targets;
using Febe.Core.Abstractions;
using Febe.Core.Collection;
using Febe.Tests.Browser.TestSupport;
using Febe.Tests.Support;
namespace Febe.Tests.Browser;
/// <summary>
/// The browser layer against a real Chromium on a private Xvfb display and a local site served by the test.
/// Never Instagram. One browser is shared by the whole class; the tests run one at a time.
/// </summary>
[TestClass]
[DoNotParallelize]
public sealed class BrowserIntegrationTests
{
private const string Targets = """
{
"site": "prova-integrazione",
"targets": {
"avanti": { "role": "button", "name": "^avanti$" },
"coperto": { "role": "button", "name": "^coperto$" },
"lontano": { "role": "button", "name": "^lontano$" },
"profilo": { "role": "link", "href": "/profilo/{username}/" },
"storia": { "role": "link", "href": "/stories/{username}/**", "name": "storia" },
"inesistente": { "role": "button", "name": "^non c'è$" },
"campo": { "role": "textbox", "name": "^campo$" }
}
}
""";
private static RealBrowser? s_browser;
private static string? s_unavailable;
public TestContext TestContext { get; set; } = null!;
private static RealBrowser Browser => s_browser ?? throw new InvalidOperationException("Browser non avviato.");
[ClassInitialize]
public static async Task StartBrowserAsync(TestContext context)
{
s_unavailable = RealBrowser.UnavailableReason();
if (s_unavailable is null)
{
s_browser = await RealBrowser.StartAsync();
}
}
[ClassCleanup]
public static async Task StopBrowserAsync()
{
if (s_browser is not null)
{
await s_browser.DisposeAsync();
s_browser = null;
}
}
[TestInitialize]
public void RequireBrowser()
{
if (s_unavailable is not null)
{
Assert.Inconclusive(s_unavailable);
}
}
[TestMethod]
public async Task ThePageSeesAnOrdinaryVisibleBrowser()
{
await Browser.GoAsync("/ambiente");
var seen = await Browser.Cdp.EvaluateAsync("""
({
webdriver: navigator.webdriver,
visibility: document.visibilityState,
hidden: document.hidden,
focus: document.hasFocus(),
language: navigator.language,
languages: navigator.languages,
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
userAgent: navigator.userAgent,
width: window.innerWidth,
height: window.innerHeight,
outerWidth: window.outerWidth,
outerHeight: window.outerHeight
})
""");
TestContext.WriteLine("S6 (Chromium su Xvfb): " + seen.GetRawText());
await new HumanGestures(Browser.Cdp, Browser.Clock, new SeededRandom(9)).MoveToAsync(new PagePoint(300, 300));
TestContext.WriteLine("Fuoco dopo il primo movimento del puntatore: " + (await Browser.Cdp.EvaluateAsync("document.hasFocus()")).GetRawText());
TestContext.WriteLine("Sandbox disattivata (utente root): " + RealBrowser.IsRoot);
Assert.IsFalse(seen.GetProperty("webdriver").GetBoolean(), "With a fixed debugging port navigator.webdriver stays false.");
Assert.AreEqual("visible", seen.GetProperty("visibility").GetString());
Assert.IsFalse(seen.GetProperty("hidden").GetBoolean());
Assert.AreEqual("Europe/Rome", seen.GetProperty("timeZone").GetString(), "The time zone comes from TZ.");
Assert.IsFalse(seen.GetProperty("userAgent").GetString()!.Contains("Headless", StringComparison.OrdinalIgnoreCase));
}
[TestMethod]
public void TheDebugPortListensOnLoopbackOnly()
{
Assert.IsTrue(LoopbackPortCheck.IsLoopbackOnly(Browser.Port, out var listeners));
TestContext.WriteLine("Punti di ascolto della porta di debug: " + string.Join(", ", listeners));
}
[TestMethod]
public async Task ForbiddenCommandsAreRejectedAndChangeNothing()
{
await Browser.GoAsync("/ambiente");
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.SendAsync("Emulation.setTimezoneOverride", new JsonObject { ["timezoneId"] = "America/New_York" }));
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.SendAsync("Network.setUserAgentOverride", new JsonObject { ["userAgent"] = "Febe" }));
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.SendAsync("Network.setExtraHTTPHeaders", new JsonObject { ["headers"] = new JsonObject() }));
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.SendAsync("Page.addScriptToEvaluateOnNewDocument", new JsonObject { ["source"] = "1" }));
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.SendAsync("Fetch.enable"));
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.SendAsync("Target.createTarget", new JsonObject { ["url"] = "about:blank" }));
await Assert.ThrowsExactlyAsync<CdpCommandNotAllowedException>(() => Browser.Cdp.EvaluateAsync("window.scrollBy(0, 500)"));
Assert.AreEqual("Europe/Rome", await Browser.EvalAsync<string>("Intl.DateTimeFormat().resolvedOptions().timeZone"));
Assert.AreEqual(0, await Browser.EvalAsync<double>("window.scrollY"));
}
[TestMethod]
public async Task WheelBurstsReallyScrollThePage()
{
await Browser.GoAsync("/lunga");
Assert.AreEqual(0, await Browser.EvalAsync<double>("window.scrollY"));
var gestures = new HumanGestures(Browser.Cdp, Browser.Clock, new SeededRandom(1));
var requested = await gestures.ScrollBurstAsync();
requested += await gestures.ScrollBurstAsync();
// The scroll runs in the compositor after the events: by condition, not after a fixed time (#46).
await TestWait.UntilAsync(
async () => await Browser.EvalAsync<double>("window.scrollY") > 200 && await Browser.EvalAsync<int>("(window.__wheel && window.__wheel.count) || 0") >= 6,
"la pagina scesa con la rotella");
var scrolled = await Browser.EvalAsync<double>("window.scrollY");
var wheel = await Browser.Cdp.EvaluateAsync("window.__wheel");
TestContext.WriteLine($"Pixel chiesti {requested}, pagina scesa di {scrolled}, eventi rotella {wheel.GetRawText()}");
Assert.IsTrue(scrolled > 200, $"The page scrolled by {scrolled} px.");
Assert.IsTrue(wheel.GetProperty("count").GetInt32() >= 6);
Assert.IsTrue(wheel.GetProperty("trusted").GetBoolean(), "Wheel events are trusted, as real ones.");
await gestures.ScrollBackAsync();
await TestWait.UntilAsync(async () => await Browser.EvalAsync<double>("window.scrollY") < scrolled, "la pagina risalita");
Assert.IsTrue(await Browser.EvalAsync<double>("window.scrollY") < scrolled, "A scroll back goes up.");
}
[TestMethod]
public async Task PointerClicksTheButtonFoundThroughTheTargetMap()
{
await Browser.GoAsync("/bottoni");
var map = TargetMap.Parse(Targets);
var resolver = new TargetResolver(Browser.Cdp, map);
var gestures = new HumanGestures(Browser.Cdp, Browser.Clock, new SeededRandom(2));
var actions = new TargetActions(resolver, gestures);
var avanti = await resolver.ResolveAsync("avanti");
Assert.IsTrue(avanti.Found, avanti.Message);
// The body keeps its default 8 px margin.
Assert.AreEqual(new PageRect(408, 308, 160, 50), avanti.Element!.Rect, "The visible «Avanti», not the hidden one.");
Assert.AreEqual(1, avanti.Matches);
var click = await actions.ClickAsync("avanti");
Assert.IsTrue(click.Clicked);
var clicks = await Browser.Cdp.EvaluateAsync("window.__clicks");
TestContext.WriteLine("Clic registrati dalla pagina: " + clicks.GetRawText());
var first = clicks.EnumerateArray().Single();
Assert.AreEqual("b2", first.GetProperty("id").GetString());
Assert.IsTrue(first.GetProperty("trusted").GetBoolean());
Assert.AreEqual(click.At!.Value.X, first.GetProperty("x").GetDouble(), 1);
Assert.IsTrue(await Browser.EvalAsync<int>("window.__moves") >= 10, "The pointer travelled: many mousemove events.");
var covered = await actions.ClickAsync("coperto");
Assert.IsFalse(covered.Clicked);
Assert.AreEqual(TargetStatus.Obstructed, covered.Lookup.Status);
var missing = await actions.ClickAsync("inesistente");
Assert.IsFalse(missing.Clicked);
Assert.AreEqual(TargetStatus.NotFound, missing.Lookup.Status);
var profile = await resolver.ResolveAsync("profilo", new Dictionary<string, string> { ["username"] = "alice" });
Assert.IsTrue(profile.Found, "Link shape /profilo/{username}/ matches.");
Assert.IsFalse((await resolver.ResolveAsync("profilo", new Dictionary<string, string> { ["username"] = "bob" })).Found);
Assert.IsTrue((await resolver.ResolveAsync("storia", new Dictionary<string, string> { ["username"] = "alice" })).Found, "Name from content plus link shape.");
var far = await actions.ClickAsync("lontano", maxScrollBursts: 12);
Assert.IsTrue(far.Clicked, far.Lookup.Message);
Assert.IsTrue(await Browser.EvalAsync<double>("window.scrollY") > 1000, "Wheel bursts brought it into view.");
var ids = (await Browser.Cdp.EvaluateAsync("window.__clicks.map((c) => c.id)")).Deserialize<string[]>();
CollectionAssert.AreEqual(new[] { "b2", "b5" }, ids, "Only the two targets were clicked.");
}
[TestMethod]
public async Task RemoteViewDeliversFramesAndForwardsTyping()
{
await Browser.GoAsync("/animata");
await using var view = new RemoteViewSession(Browser.Cdp, Browser.Host.UserDriving);
await view.StartAsync(new ScreencastOptions(Quality: 60, MaxWidth: 1280, MaxHeight: 900));
Assert.IsTrue(Browser.Host.UserIsDriving);
var frames = new List<ScreencastFrame>();
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15)))
{
await foreach (var frame in view.Screencast.ReadFramesAsync(cts.Token))
{
frames.Add(frame);
if (frames.Count >= 5)
{
break;
}
}
}
TestContext.WriteLine($"Fotogrammi: {frames.Count}, primo {frames[0].Jpeg.Length} byte, pagina {frames[0].DeviceWidth}×{frames[0].DeviceHeight}");
Assert.IsTrue(frames.Count >= 5, "Several frames: each one was acknowledged.");
Assert.IsTrue(frames.All(f => f.Jpeg.Length > 100 && f.Jpeg[0] == 0xFF && f.Jpeg[1] == 0xD8), "JPEG frames.");
// S4 locally: click into a field through the remote view and type.
await view.NavigateAsync(Browser.Site.Url("/modulo"));
Assert.IsTrue(await Browser.Cdp.WaitForLoadAsync(Browser.Clock, TimeSpan.FromSeconds(60)), "La pagina del modulo non ha finito di caricare.");
await Browser.WaitForPaintAsync();
await Task.Delay(RealBrowser.SettleAfterLoad);
var field = await new TargetResolver(Browser.Cdp, TargetMap.Parse(Targets)).ResolveAsync("campo");
Assert.IsTrue(field.Found, field.Message);
var frameNow = view.Screencast.LastFrame!;
var nx = field.Element!.Rect.Center.X / frameNow.DeviceWidth;
var ny = field.Element.Rect.Center.Y / frameNow.DeviceHeight;
await view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Move, nx, ny));
await view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Down, nx, ny, RemoteMouseButton.Left));
await view.MouseAsync(new RemoteMouseEvent(RemoteMouseAction.Up, nx, ny, RemoteMouseButton.Left));
foreach (var ch in new[] { "C", "i", "a", "o", " ", "è" })
{
var key = Keyboard.ForText(ch);
await view.KeyAsync(new RemoteKeyEvent(true, key));
await view.KeyAsync(new RemoteKeyEvent(false, key));
}
Assert.AreEqual("Ciao è", await Browser.EvalAsync<string>("document.getElementById('campo').value"));
await view.StopAsync();
Assert.IsFalse(Browser.Host.UserIsDriving);
}
[TestMethod]
public async Task PreviewIsASingleJpegFrame()
{
await Browser.GoAsync("/animata");
var jpeg = await PagePreview.CaptureAsync(Browser.Cdp, 50);
Assert.IsTrue(jpeg.Length > 1000 && jpeg[0] == 0xFF && jpeg[1] == 0xD8);
}
[TestMethod]
public async Task DownloadThroughTheBrowserIsIntact()
{
await Browser.GoAsync("/");
var fetcher = new BrowserFileFetcher(Browser.Cdp, Browser.Hosts);
var target = Path.Combine(Path.GetTempPath(), "febe-scaricamento-" + Guid.NewGuid().ToString("N") + ".tmp");
try
{
var watch = Stopwatch.StartNew();
FetchResult result;
await using (var file = File.Create(target))
{
result = await fetcher.FetchAsync(new Uri(Browser.Site.CdnUrl("/file.bin")), file, null, CancellationToken.None);
}
watch.Stop();
TestContext.WriteLine($"Scaricati {result.Bytes} byte in {watch.Elapsed.TotalSeconds:F1} s attraverso il browser.");
Assert.AreEqual(FetchOutcome.Completed, result.Outcome, result.Error);
Assert.AreEqual(LocalTestSite.BigFileBytes, result.Bytes);
await using var read = File.OpenRead(target);
Assert.AreEqual(LocalTestSite.BigFileSha256, Convert.ToHexString(await SHA256.HashDataAsync(read)), "SHA-256 matches: the file is intact.");
var site = await fetcher.FetchAsync(new Uri(Browser.Site.Url("/api/dati.json")), Stream.Null, null, CancellationToken.None);
Assert.AreEqual(FetchOutcome.HostNotAllowed, site.Outcome, "Only CDN addresses are downloaded.");
var missing = await fetcher.FetchAsync(new Uri(Browser.Site.CdnUrl("/manca.bin")), Stream.Null, null, CancellationToken.None);
Assert.AreEqual(FetchOutcome.HttpError, missing.Outcome);
Assert.AreEqual(404, missing.HttpStatus);
}
finally
{
File.Delete(target);
}
}
[TestMethod]
public async Task ResponseTapDeliversJsonBodies()
{
var consumer = new Collector();
await using var tap = new ResponseTap(Browser.Cdp, Browser.Hosts, Browser.Clock);
tap.AddConsumer(consumer);
await tap.StartAsync();
await Browser.GoAsync("/cattura");
await WaitUntilAsync(() => consumer.Bodies.Any(b => b.Response.Path == "/api/dati.json"));
var (response, json) = consumer.Bodies.First(b => b.Response.Path == "/api/dati.json");
Assert.AreEqual(HostKind.Site, response.HostKind);
Assert.AreEqual(200, response.Status);
Assert.AreEqual("application/json", response.MimeType);
using var doc = JsonDocument.Parse(json);
Assert.AreEqual("mondo", doc.RootElement.GetProperty("elementi")[1].GetProperty("testo").GetString());
Assert.IsTrue(tap.Stats.JsonBodies >= 1);
}
[TestMethod]
public async Task StopSignalsComeFromStatusAndNavigation()
{
var breaker = new RecordingBreaker();
var detector = new SignalDetector(SignalRules.LoadTestSite(), breaker, Browser.Clock);
await using var tap = new ResponseTap(Browser.Cdp, Browser.Hosts, Browser.Clock);
tap.AddObserver(detector);
tap.AddConsumer(detector);
await tap.StartAsync();
await Browser.GoAsync("/cattura-429");
await WaitUntilAsync(() => breaker.Signals.Any(s => s.Code == "http-429"));
var throttle = breaker.Signals.First(s => s.Code == "http-429");
Assert.AreEqual(SignalLevel.Throttle, throttle.Level);
Assert.AreEqual(Browser.Site.Url("/api/troppe"), throttle.Url);
await Browser.GoAsync("/vai-a-verifica");
await WaitUntilAsync(() => breaker.Signals.Any(s => s.Code == "challenge-url"));
var challenge = breaker.Signals.First(s => s.Code == "challenge-url");
Assert.AreEqual(SignalLevel.Challenge, challenge.Level);
Assert.AreEqual(Browser.Site.Url("/verifica"), challenge.Url, "Address without query string.");
await Browser.GoAsync("/accesso");
await WaitUntilAsync(() => breaker.Signals.Any(s => s.Code == "login-url"));
Assert.AreEqual(SignalLevel.Disconnect, breaker.Signals.First(s => s.Code == "login-url").Level);
await Browser.GoAsync("/cattura-attendi");
await WaitUntilAsync(() => breaker.Signals.Any(s => s.Code == "wait-text"));
TestContext.WriteLine("Segnali: " + string.Join(", ", breaker.Signals.Select(s => $"{s.Level}/{s.Code}")));
Assert.AreEqual(4, breaker.Signals.Select(s => s.Code).Distinct().Count());
}
[TestMethod]
public async Task ARouteChangeInsideTheAppIsSeenToo()
{
var breaker = new RecordingBreaker();
var detector = new SignalDetector(SignalRules.LoadTestSite(), breaker, Browser.Clock);
await using var tap = new ResponseTap(Browser.Cdp, Browser.Hosts, Browser.Clock);
tap.AddObserver(detector);
await tap.StartAsync();
await Browser.GoAsync("/spa");
await WaitUntilAsync(() => breaker.Signals.Any(s => s.Code == "challenge-url"));
Assert.AreEqual(Browser.Site.Url("/verifica/interna"), breaker.Signals.Single().Url, "history.pushState to a verification page is a signal.");
}
[TestMethod]
public async Task SessionCookieCheckOnlyTellsWhetherItExists()
{
await Browser.GoAsync("/cookie");
var site = Browser.Site.Url("/");
Assert.IsTrue(await SessionCookieProbe.HasSessionCookieAsync(Browser.Cdp, new SessionCookieOptions("sessionid", site)));
Assert.IsFalse(await SessionCookieProbe.HasSessionCookieAsync(Browser.Cdp, new SessionCookieOptions("altro", site)));
Assert.IsFalse(Browser.Log.Any(l => l.Contains("valore-finto", StringComparison.Ordinal)), "The value never reaches the log.");
}
[TestMethod]
public async Task RestLeavesTheBrowserOnTheBlankPage()
{
await Browser.GoAsync("/ambiente");
await Browser.Host.RestAsync();
await Browser.Cdp.WaitForLoadAsync(Browser.Clock, TimeSpan.FromSeconds(5));
Assert.AreEqual("about:blank", await Browser.Cdp.GetMainFrameUrlAsync());
}
private static async Task WaitUntilAsync(Func<bool> condition, int seconds = 10)
{
var watch = Stopwatch.StartNew();
while (!condition() && watch.Elapsed < TimeSpan.FromSeconds(seconds))
{
await Task.Delay(50);
}
Assert.IsTrue(condition(), "Condition not reached in time.");
}
private sealed class Collector : IResponseConsumer
{
public ConcurrentQueue<(ResponseDescriptor Response, string Json)> Bodies { get; } = new();
public ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
{
Bodies.Enqueue((response, body.RootElement.GetRawText()));
return ValueTask.CompletedTask;
}
}
}