Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions src/E2E/Engine/WebEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Observation> 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);

/// <summary>One read of the page into a tree, publishing its refs. Callers hold their turn (<see cref="ObserveInTurnAsync"/>).</summary>
private async Task<Observation> ReadObservationAsync(OperationBudget budget)
{
var walk = new FrameWalk();
List<WebNode> roots;
try
{
var walk = new FrameWalk();
List<WebNode> 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;
Expand Down Expand Up @@ -1229,14 +1235,27 @@ public async Task<IReadOnlyList<SemanticNode>> 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);
Expand Down
30 changes: 12 additions & 18 deletions src/E2E/Expect/ScreenshotCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,22 @@ private Task<T> ReadAsync<T>(Func<TimeSpan, CancellationToken, Task<T>> read, Ca
public async Task<Capture> 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)
Expand All @@ -86,6 +84,7 @@ public async Task<Capture> 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.
Expand All @@ -103,7 +102,13 @@ public async Task<Capture> 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<BoundingBox>();
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);
}

Expand All @@ -116,16 +121,5 @@ public async Task<Capture> 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."),
};
}
}
}
Loading