Repository navigation
Stop a frame-scoped query at a nested frame #1086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
67bdb2b
fcc562b
0155795
fc57d03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<SemanticNode> roots) | |
|
|
||
| public List<SemanticNode> All { get; } = []; | ||
|
|
||
| /// <summary>The candidates that are strict descendants of any of <paramref name="ancestors"/>, in document order.</summary> | ||
| /// <summary> | ||
| /// The candidates that are strict descendants of any of <paramref name="ancestors"/>, 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. | ||
| /// </summary> | ||
| public List<SemanticNode> DescendantsOf(IReadOnlyCollection<SemanticNode> ancestors, IReadOnlyList<SemanticNode> candidates) | ||
| { | ||
| if (ancestors.Count == 0) | ||
|
|
@@ -1687,7 +1696,25 @@ public List<SemanticNode> DescendantsOf(IReadOnlyCollection<SemanticNode> ancest | |
| } | ||
|
|
||
| var set = new HashSet<SemanticNode>(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<SemanticNode> ancestors) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| foreach (var ancestor in Ancestors(node)) | ||
| { | ||
| if (ancestors.Contains(ancestor)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (ancestor.Frame) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -1699,7 +1726,7 @@ public List<SemanticNode> InnermostOnly(List<SemanticNode> matches) | |
| var containers = new HashSet<SemanticNode>(ReferenceEqualityComparer.Instance); | ||
| foreach (var match in matches) | ||
| { | ||
| containers.UnionWith(Ancestors(match)); | ||
| containers.UnionWith(Ancestors(match).TakeWhile(ancestor => !ancestor.Frame)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| return matches.Where(node => !containers.Contains(node)).ToList(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
Comment on lines
+48
to
+50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| await using var session = await WebSemanticsTests.OpenAsync("about:blank", new WebEngineOptions { Headless = true }); | ||
| var page = WebEngine.SurfaceOf(session)!.Page(); | ||
| var nested = "<button>Hello nested iframe</button>"; | ||
| var outer = $"<button>Hello iframe</button><iframe srcdoc=\"{Attribute(nested)}\"></iframe>"; | ||
| await page.SetContentAsync($"<iframe srcdoc=\"{Attribute(outer)}\"></iframe>"); | ||
| 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($"<div>Greeting<iframe srcdoc=\"{Attribute("<p>Greeting</p>")}\"></iframe></div>"); | ||
| 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($"<div data-testid=\"box\"><iframe srcdoc=\"{Attribute("<p>Nested only</p>")}\"></iframe></div>"); | ||
| 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); | ||
| } | ||
|
|
||
| /// <summary>Escapes HTML for a double-quoted attribute value, so a document can carry a nested <c>srcdoc</c>.</summary> | ||
| private static string Attribute(string html) => html.Replace("&", "&", StringComparison.Ordinal).Replace("\"", """, StringComparison.Ordinal); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
rg -n -i 'nested frame|framelocator|hasnottext|hastext' skills/e2e-dotnet/SKILL.md COMPATIBILITY.md docs-site/src/content/docsRepository: hardkoded/e2e-dotnet
Length of output: 18736
Script executed:
Repository: hardkoded/e2e-dotnet
Length of output: 4486
Document the nested-frame boundary in the locator scope docs.
This PR changes what
FrameLocator(...)queries match, but it does not updateskills/e2e-dotnet/SKILL.md, thedocs-site/src/content/docs/guides, orCOMPATIBILITY.md. The current scope text says a locator is "scoped to its matches' descendants" and does not state that a frame query stops at a nested frame. Add that boundary to these three files, using the C# names.Based on learnings: "When the port adds or changes public API or user-visible behavior, update
skills/e2e-dotnet/SKILL.mdand the guides indocs-site/src/content/docs/in the same PR."Source: Learnings