diff --git a/src/E2E/Engine/Observation.cs b/src/E2E/Engine/Observation.cs index 771e335c6..eef862efe 100644 --- a/src/E2E/Engine/Observation.cs +++ b/src/E2E/Engine/Observation.cs @@ -114,6 +114,9 @@ public sealed class SemanticNode internal string? AttributeOf(string name) => name == "href" && WholeHref is not null ? WholeHref : Attributes.TryGetValue(name, out var value) ? value : name == TestIdAttribute ? TestId : null; + /// Whether the node is a frame element, whose children are its own document's nodes. A query under another node stops at it. + internal bool Frame { get; init; } + /// The node's box in viewport CSS pixels, when the platform measures one. public BoundingBox? Rect { get; init; } diff --git a/src/E2E/Engine/WebEngine.cs b/src/E2E/Engine/WebEngine.cs index 7727c0472..79232892e 100644 --- a/src/E2E/Engine/WebEngine.cs +++ b/src/E2E/Engine/WebEngine.cs @@ -3560,6 +3560,7 @@ private static SemanticNode ToNode(WebNode dto, string testIdAttribute) Placeholder = dto.Placeholder, InputPurpose = dto.InputPurpose, Level = dto.Level, + Frame = dto.Frame, States = new NodeStates { Checked = dto.Checked, diff --git a/src/E2E/Locator/Screen.cs b/src/E2E/Locator/Screen.cs index 0c9116b58..0f5aa1e37 100644 --- a/src/E2E/Locator/Screen.cs +++ b/src/E2E/Locator/Screen.cs @@ -1652,6 +1652,12 @@ private static bool SubtreeHasText(SemanticNode node, TextMatch expected, bool e return true; } + // A frame node's children are another document's, which an element's text does not include. + if (node.Frame) + { + return false; + } + foreach (var child in node.Children) { if (SubtreeHasText(child, expected, exact, includeValue)) @@ -1678,7 +1684,10 @@ public TreeIndex(IEnumerable roots) public List All { get; } = []; - /// The candidates that are strict descendants of any of , in document order. + /// + /// The candidates that are strict descendants of any of , in document order. A frame node's children are + /// its own document's, so a candidate under a nested frame is no descendant of a node above that frame, as in a locator chain. + /// public List DescendantsOf(IReadOnlyCollection ancestors, IReadOnlyList candidates) { if (ancestors.Count == 0) @@ -1687,7 +1696,25 @@ public List DescendantsOf(IReadOnlyCollection ancest } var set = new HashSet(ancestors, ReferenceEqualityComparer.Instance); - return candidates.Where(node => Ancestors(node).Any(set.Contains)).ToList(); + return candidates.Where(node => InsideAny(node, set)).ToList(); + } + + private bool InsideAny(SemanticNode node, HashSet ancestors) + { + foreach (var ancestor in Ancestors(node)) + { + if (ancestors.Contains(ancestor)) + { + return true; + } + + if (ancestor.Frame) + { + return false; + } + } + + return false; } /// @@ -1699,7 +1726,7 @@ public List InnermostOnly(List matches) var containers = new HashSet(ReferenceEqualityComparer.Instance); foreach (var match in matches) { - containers.UnionWith(Ancestors(match)); + containers.UnionWith(Ancestors(match).TakeWhile(ancestor => !ancestor.Frame)); } return matches.Where(node => !containers.Contains(node)).ToList(); diff --git a/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs b/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs index 13f4ff296..d547a6c3f 100644 --- a/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs +++ b/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs @@ -45,6 +45,53 @@ public async Task Reports_an_observed_nodes_box_in_the_top_level_viewport_past_e Assert.Equal(expected.Height, node.Rect.Height, 3); } + // .NET-only: upstream's locator-frame and selectors-frame testbed tests cover this page; the port has no testbed here. + [Fact] + public async Task A_frame_scoped_role_query_stops_at_a_nested_frame() + { + await using var session = await WebSemanticsTests.OpenAsync("about:blank", new WebEngineOptions { Headless = true }); + var page = WebEngine.SurfaceOf(session)!.Page(); + var nested = ""; + var outer = $""; + await page.SetContentAsync($""); + await page.FrameLocator("iframe").FrameLocator("iframe").GetByRole(AriaRole.Button).WaitForAsync(); + + var inFrame = new LocatorExpression { Kind = "frame", Selector = "iframe", Source = new LocatorExpression { Kind = "role", Role = "button" } }; + var nodes = await session.LocateAsync(inFrame, TimeSpan.FromSeconds(10), CancellationToken.None); + + Assert.Equal(["Hello iframe"], nodes.Select(node => node.Name)); + } + + // .NET-only, as the test above: upstream's text engine and hasText filter read one document, so the frame's text is not the box's. + [Fact] + public async Task A_text_query_keeps_an_outer_match_that_a_nested_frame_repeats() + { + await using var session = await WebSemanticsTests.OpenAsync("about:blank", new WebEngineOptions { Headless = true }); + var page = WebEngine.SurfaceOf(session)!.Page(); + await page.SetContentAsync($"
Greeting
"); + await page.FrameLocator("iframe").GetByText("Greeting").WaitForAsync(); + + var nodes = await session.LocateAsync(new LocatorExpression { Kind = "text", Value = "Greeting" }, TimeSpan.FromSeconds(10), CancellationToken.None); + + Assert.Equal(2, nodes.Count); + } + + [Fact] + public async Task A_has_text_filter_does_not_read_the_text_of_a_nested_frame() + { + await using var session = await WebSemanticsTests.OpenAsync("about:blank", new WebEngineOptions { Headless = true }); + var page = WebEngine.SurfaceOf(session)!.Page(); + await page.SetContentAsync($"
"); + await page.FrameLocator("iframe").GetByText("Nested only").WaitForAsync(); + + var box = new LocatorExpression { Kind = "testid", Value = "box" }; + var withText = await session.LocateAsync(new LocatorExpression { Kind = "filter", Source = box, HasText = "Nested only" }, TimeSpan.FromSeconds(10), CancellationToken.None); + var withoutText = await session.LocateAsync(new LocatorExpression { Kind = "filter", Source = box, HasNotText = "Nested only" }, TimeSpan.FromSeconds(10), CancellationToken.None); + + Assert.Empty(withText); + Assert.Single(withoutText); + } + /// Escapes HTML for a double-quoted attribute value, so a document can carry a nested srcdoc. private static string Attribute(string html) => html.Replace("&", "&", StringComparison.Ordinal).Replace("\"", """, StringComparison.Ordinal); }