From 67bdb2b43d36d46e48525cf3c9a7a54e5d8388c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Wed, 4 Nov 2026 19:17:21 -0300 Subject: [PATCH 1/2] Stop a frame-scoped query at a nested frame (Closes #1066) A query scoped to a frame matched the buttons of the frame nested in it, because the nested frame's observation hangs under its iframe node and the descendant walk did not stop there. A frame node now ends the walk, as a locator chain does in upstream. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- src/E2E/Engine/Observation.cs | 3 +++ src/E2E/Engine/WebEngine.cs | 1 + src/E2E/Locator/Screen.cs | 25 +++++++++++++++++-- .../NestedFrames/NestedFrameLocatorsTests.cs | 17 +++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/E2E/Engine/Observation.cs b/src/E2E/Engine/Observation.cs index 7cec82430..1267c308c 100644 --- a/src/E2E/Engine/Observation.cs +++ b/src/E2E/Engine/Observation.cs @@ -105,6 +105,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 221e5cfee..1e7fcbf61 100644 --- a/src/E2E/Engine/WebEngine.cs +++ b/src/E2E/Engine/WebEngine.cs @@ -3489,6 +3489,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 9f6d76cd6..b21a84cda 100644 --- a/src/E2E/Locator/Screen.cs +++ b/src/E2E/Locator/Screen.cs @@ -1677,7 +1677,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) @@ -1686,7 +1689,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; } /// diff --git a/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs b/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs index 13f4ff296..b18d66b1f 100644 --- a/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs +++ b/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs @@ -45,6 +45,23 @@ 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)); + } + /// 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); } From fcc562b0e50f055d08c138b4828aec82593fbeca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Wed, 4 Nov 2026 19:44:12 -0300 Subject: [PATCH 2/2] Stop text queries and hasText at a nested frame (Refs #1066) Text and label queries dropped an outer container because a nested frame repeated its text, and a hasText filter read the text of a nested frame. Upstream reads one document for both, so both now stop at a frame node. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- src/E2E/Locator/Screen.cs | 8 ++++- .../NestedFrames/NestedFrameLocatorsTests.cs | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/E2E/Locator/Screen.cs b/src/E2E/Locator/Screen.cs index b21a84cda..ea3b53c69 100644 --- a/src/E2E/Locator/Screen.cs +++ b/src/E2E/Locator/Screen.cs @@ -1651,6 +1651,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)) @@ -1719,7 +1725,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 b18d66b1f..d547a6c3f 100644 --- a/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs +++ b/tests/E2E.Tests/NestedFrames/NestedFrameLocatorsTests.cs @@ -62,6 +62,36 @@ public async Task A_frame_scoped_role_query_stops_at_a_nested_frame() 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); }