using System.Text.RegularExpressions; using Febe.Browser.Cdp; using Febe.Tests.Windows.Support; namespace Febe.Tests.Windows; /// /// The start of the browser of Febe on a real Windows (regression of 14 and 19 October 2026: «WebView2 non parte /// (InvalidOperationException: CoreWebView2Environment members can only be accessed from the UI thread.)» at every /// attempt, for ever). The composition starts the browser from a thread-pool thread; every WebView2 object lives on the /// UI thread of WPF. /// [TestClass] public sealed partial class WebView2BrowserStartTests { public TestContext TestContext { get; set; } = null!; [TestInitialize] public void RequireWebView2() => WebView2Runtime.Require(); [TestMethod] [Timeout(120_000, CooperativeCancellation = true)] public async Task StartedFromAThreadPoolThreadTheBrowserIsReadyAtTheFirstAttempt() { var ct = TestContext.CancellationToken; await using var bed = await BrowserBed.CreateAsync("avvio"); await bed.StartAsync(ct); TestContext.WriteLine(bed.Lines.Text); Assert.IsTrue(bed.Browser.IsAvailable, "the browser is available once RunAsync returned"); Assert.IsNull(bed.Browser.LastError); var ready = bed.Lines.Lines.Where(l => l.Contains("Browser di Febe pronto: WebView2 ", StringComparison.Ordinal)).ToList(); Assert.HasCount(1, ready, "one «ready» line: " + bed.Lines.Text); Assert.MatchesRegex(ReadyLine(), ready[0]); Assert.IsFalse(bed.Lines.Any("non parte"), "no failed attempt: " + bed.Lines.Text); Assert.IsFalse(bed.Lines.Any("ERRORE"), "no error at all: " + bed.Lines.Text); // The version is a plain value now: it can be read from any thread. var version = await Task.Run(() => bed.Browser.Host.Version, ct); Assert.IsNotNull(version); StringAssert.Contains(ready[0], version); // The engine's threads use the page through the session: the command hops to the UI thread and back. Assert.AreEqual("about:blank", await Task.Run(() => bed.Browser.Cdp.GetMainFrameUrlAsync(ct), ct)); } [GeneratedRegex(@"\[browser\] Browser di Febe pronto: WebView2 \d+\.\d+\.\d+\.\d+, pagina 1280×900, finestra, fuori vista\.$")] private static partial Regex ReadyLine(); }