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
27 changes: 27 additions & 0 deletions lib/PuppeteerSharp.Tests/AccessibilityTests/RootOptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using PuppeteerSharp.Nunit;
Expand Down Expand Up @@ -172,6 +173,32 @@ public async Task ShouldGetTheParentElementHandleFromATextNodeAccessibilityNode(
Assert.That(textNodeInnerHtml, Is.EqualTo("<b>Hello, </b> world!"));
}

[Test, PuppeteerTest("accessibility.spec", "elementHandle()", "should get the shadow host ElementHandle from a text node in a shadow root")]
public async Task ShouldGetTheShadowHostElementHandleFromATextNodeInAShadowRoot()
{
await Page.SetContentAsync("<div id=\"host\"></div>");
await Page.EvaluateExpressionAsync(@"
document
.querySelector('#host')
.attachShadow({mode: 'open'}).textContent = 'Shadow text';
");
var host = await Page.QuerySelectorAsync("#host");

var snapshot = await Page.Accessibility.SnapshotAsync(new AccessibilitySnapshotOptions
{
Root = host,
InterestingOnly = false,
});
var textNode = snapshot.Children.First(child => child.Name == "Shadow text");
Assert.That(textNode.Role, Is.EqualTo("StaticText"));

var textNodeHandle = await textNode.ElementHandleAsync();
Assert.That(textNodeHandle, Is.Not.Null);
Assert.That(
await textNodeHandle.EvaluateFunctionAsync<string>("element => element.id"),
Is.EqualTo("host"));
}

[Test, PuppeteerTest("accessibility.spec", "root option", "should work with nested button inside h1 with interestingOnly:true")]
public async Task ShouldWorkWithNestedButtonInsideH1WithInterestingOnlyTrue()
{
Expand Down
10 changes: 8 additions & 2 deletions lib/PuppeteerSharp/PageAccessibility/AXNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,14 @@ internal SerializedAXNode Serialize()

// Since Text nodes are not elements, we want to
// return a handle to the parent element for them.
return await ((ElementHandle)handle).EvaluateFunctionHandleAsync(
"node => node.nodeType === Node.TEXT_NODE ? node.parentElement : node").ConfigureAwait(false) as IElementHandle;
// A text node placed directly in a shadow root has no parent
// element, so fall back to the shadow host.
return await ((ElementHandle)handle).EvaluateFunctionHandleAsync(@"node => {
if (node.nodeType !== Node.TEXT_NODE) {
return node;
}
return node.parentElement ?? node.parentNode?.host ?? null;
}").ConfigureAwait(false) as IElementHandle;
}
: null,
Role = _role,
Expand Down
Loading