diff --git a/src/E2E/Engine/WebEngine.cs b/src/E2E/Engine/WebEngine.cs index f33d0189c..a5b1a6986 100644 --- a/src/E2E/Engine/WebEngine.cs +++ b/src/E2E/Engine/WebEngine.cs @@ -1161,25 +1161,31 @@ public Task OpenAsync(string url, CancellationToken cancellationToken) => ActedA // A locate reads under what is left of its wait, never longer than the action timeout an observation has. private Task ObserveAsync(CancellationToken cancellationToken, TimeSpan? timeout) => RunAsync("observe", cancellationToken, async () => { - var budget = Budget("observe", cancellationToken, timeout is { } left && left < _actionTimeout ? TimeSpan.FromMilliseconds(Math.Min(Math.Max(left.TotalMilliseconds, MinLocateRead.TotalMilliseconds), _actionTimeout.TotalMilliseconds)) : null); - return await ObserveInTurnAsync(budget, async () => + var budget = ObserveBudget(cancellationToken, timeout); + return await ObserveInTurnAsync(budget, () => ReadObservationAsync(budget)).ConfigureAwait(false); + }); + + private OperationBudget ObserveBudget(CancellationToken cancellationToken, TimeSpan? timeout) => + Budget("observe", cancellationToken, timeout is { } left && left < _actionTimeout ? TimeSpan.FromMilliseconds(Math.Min(Math.Max(left.TotalMilliseconds, MinLocateRead.TotalMilliseconds), _actionTimeout.TotalMilliseconds)) : null); + + /// One read of the page into a tree, publishing its refs. Callers hold their turn (). + private async Task ReadObservationAsync(OperationBudget budget) + { + var walk = new FrameWalk(); + List roots; + try { - var walk = new FrameWalk(); - List roots; - try - { - roots = await CollectAsync(Page.MainFrame, walk, budget).ConfigureAwait(false); - } - catch (Exception ex) when (WebErrors.IsPlaywright(ex)) - { - throw WebErrors.NavigationStaleOr(ex, "observe"); - } + roots = await CollectAsync(Page.MainFrame, walk, budget).ConfigureAwait(false); + } + catch (Exception ex) when (WebErrors.IsPlaywright(ex)) + { + throw WebErrors.NavigationStaleOr(ex, "observe"); + } - _frames = walk.Frames; - _observed = true; - return new Observation { Route = Route, Roots = roots.Select(node => ToNode(node, _testIdAttribute)).ToList(), Truncated = walk.Truncated, ScrollPosition = walk.Scroll, Viewport = walk.Viewport }; - }).ConfigureAwait(false); - }); + _frames = walk.Frames; + _observed = true; + return new Observation { Route = Route, Roots = roots.Select(node => ToNode(node, _testIdAttribute)).ToList(), Truncated = walk.Truncated, ScrollPosition = walk.Scroll, Viewport = walk.Viewport }; + } // Settles when the latest observation in line has finished; never faults. private Task _observing = Task.CompletedTask; @@ -1229,14 +1235,27 @@ public async Task> LocateAsync(LocatorExpression exp if (WebLocators.NeedsPage(expression)) { var forcing = new Forcing(); - projected = await RunAsync("locate", cancellationToken, () => ProjectAsync(expression, timeout, cancellationToken, forcing)).ConfigureAwait(false); - if (forcing.Marked) + // The elements a projection marks are taken by the next observation that publishes, and the observation that lists them is + // the one the projection is read against. Both, and the projection onto it (upstream reads whatever the selector matches; + // here every match must be a node of the observation the caller acts on), run in one turn: another call's observation in + // between would take the marks, or replace the listed elements. + var (listed, onto) = await RunAsync("locate", cancellationToken, async () => { - // Upstream reads whatever the selector matches; here the observation lists those elements first, then the expression is - // projected onto it again, so every match is a node of the observation the caller acts on. - observation = await ObserveAsync(cancellationToken, timeout).ConfigureAwait(false); - projected = await RunAsync("locate", cancellationToken, () => ProjectAsync(expression, timeout, cancellationToken, new Forcing { Mark = false })).ConfigureAwait(false); - } + var budget = ObserveBudget(cancellationToken, timeout); + return await ObserveInTurnAsync(budget, async () => + { + var first = await ProjectAsync(expression, timeout, cancellationToken, forcing).ConfigureAwait(false); + if (!forcing.Marked) + { + return ((Observation?)null, first); + } + + var observed = await ReadObservationAsync(budget).ConfigureAwait(false); + return (observed, await ProjectAsync(expression, timeout, cancellationToken, new Forcing { Mark = false }).ConfigureAwait(false)); + }).ConfigureAwait(false); + }).ConfigureAwait(false); + projected = onto; + observation = listed ?? observation; } _trace?.ShowScreen(observation); diff --git a/src/E2E/Expect/ScreenshotCapture.cs b/src/E2E/Expect/ScreenshotCapture.cs index 506f1e529..e028c82fa 100644 --- a/src/E2E/Expect/ScreenshotCapture.cs +++ b/src/E2E/Expect/ScreenshotCapture.cs @@ -53,24 +53,22 @@ private Task ReadAsync(Func> read, Ca public async Task CaptureAsync(CancellationToken cancellationToken) { var shown = Subject(subject); - var observation = await ReadAsync(screen.ObserveAsync, cancellationToken).ConfigureAwait(false); BoundingBox? box = null; if (subject is not null) { - var node = OneNode(observation); + var node = await screen.Engine.TryReadAsync(subject.Query, cancellationToken, reads).ConfigureAwait(false); if (node is not null && !_scrolled) { try { - await screen.Engine.SessionPerformAsync(node, new LocatorAction.ScrollIntoView(), cancellationToken).ConfigureAwait(false); + await screen.Engine.PerformUntilAsync(subject.Query, new LocatorAction.ScrollIntoView(), reads.Deadline, cancellationToken, anyState: true).ConfigureAwait(false); } - catch (Exception ex) when (ex is EngineException { Code: EngineErrorCodes.NodeStale } or TestException { Code: "LOCATOR_NOT_FOUND" }) + catch (TestException ex) when (ex.Code == "LOCATOR_NOT_FOUND") { } _scrolled = true; - observation = await ReadAsync(screen.ObserveAsync, cancellationToken).ConfigureAwait(false); - node = OneNode(observation); + node = await screen.Engine.TryReadAsync(subject.Query, cancellationToken, reads).ConfigureAwait(false); } if (node is null) @@ -86,6 +84,7 @@ public async Task CaptureAsync(CancellationToken cancellationToken) box = node.Rect; } + var observation = await ReadAsync(screen.ObserveAsync, cancellationToken).ConfigureAwait(false); var shot = await ReadAsync(store.Capture, cancellationToken).ConfigureAwait(false); // Upstream's pixelsProvenMasked: the engine masked at least as many regions as the tree lists secure nodes, or the image is not kept. @@ -103,7 +102,13 @@ public async Task CaptureAsync(CancellationToken cancellationToken) image = ImageOps.FillBoxes(image, secure, (0, 0, 0)); if (call.Masks.Count > 0) { - var masks = call.Masks.SelectMany(mask => LocatorResolver.Resolve(observation, mask.Query)).Where(node => !node.States.Hidden && node.Rect is not null).Select(node => node.Rect!); + var masks = new List(); + foreach (var mask in call.Masks) + { + masks.AddRange((await screen.Engine.ReadAllNowAsync(mask.Query, cancellationToken, reads).ConfigureAwait(false)) + .Where(node => !node.States.Hidden && node.Rect is not null).Select(node => node.Rect!)); + } + image = ImageOps.FillBoxes(image, masks, call.MaskColor); } @@ -116,16 +121,5 @@ public async Task CaptureAsync(CancellationToken cancellationToken) ? new Capture(ImageOps.CropImage(image, clipped), null) : new Capture(null, shown + " is outside the viewport"); } - - private SemanticNode? OneNode(Observation observation) - { - var matches = LocatorResolver.Resolve(observation, subject!.Query); - return matches.Count switch - { - 0 => null, - 1 => matches[0], - _ => throw new TestException("LOCATOR_AMBIGUOUS", subject.Query.Describe() + " matched " + Number(matches.Count) + " nodes."), - }; - } } }