How to use SerializedAXNode class of PuppeteerSharp.PageAccessibility package

Best Puppeteer-sharp code snippet using PuppeteerSharp.PageAccessibility.SerializedAXNode

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Accessibility.cs

Source: Accessibility.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using PuppeteerSharp.Messaging;
5
6namespace PuppeteerSharp.PageAccessibility
7{
8    /// <summary>
9    /// The Accessibility class provides methods for inspecting Chromium's accessibility tree. 
10    /// The accessibility tree is used by assistive technology such as screen readers or switches.
11    /// 
12    /// Accessibility is a very platform-specific thing. On different platforms, there are different screen readers that might have wildly different output.
13    /// Blink - Chrome's rendering engine - has a concept of "accessibility tree", which is than translated into different platform-specific APIs. 
14    /// Accessibility namespace gives users access to the Blink Accessibility Tree.
15    /// Most of the accessibility tree gets filtered out when converting from Blink AX Tree to Platform-specific AX-Tree or by assistive technologies themselves. 
16    /// By default, Puppeteer tries to approximate this filtering, exposing only the "interesting" nodes of the tree.
17    /// </summary>
18    public class Accessibility
19    {
20        private readonly CDPSession _client;
21
22        /// <summary>
23        /// Initializes a new instance of the <see cref="PuppeteerSharp.PageAccessibility.Accessibility"/> class.
24        /// </summary>
25        /// <param name="client">Client.</param>
26        public Accessibility(CDPSession client) => _client = client;
27
28        /// <summary>
29        /// Snapshots the async.
30        /// </summary>
31        /// <returns>The async.</returns>
32        /// <param name="options">Options.</param>
33        public async Task<SerializedAXNode> SnapshotAsync(AccessibilitySnapshotOptions options = null)
34        {
35            var response = await _client.SendAsync<AccessibilityGetFullAXTreeResponse>("Accessibility.getFullAXTree").ConfigureAwait(false);
36            var nodes = response.Nodes;
37            int? backendNodeId = null;
38            if (options?.Root != null)
39            {
40                var node = await _client.SendAsync<DomDescribeNodeResponse>("DOM.describeNode", new DomDescribeNodeRequest
41                {
42                    ObjectId = options.Root.RemoteObject.ObjectId
43                }).ConfigureAwait(false);
44                backendNodeId = node.Node.BackendNodeId;
45            }
46            var defaultRoot = AXNode.CreateTree(nodes);
47            var needle = defaultRoot;
48            if (backendNodeId.HasValue)
49            {
50                needle = defaultRoot.Find(node => node.Payload.BackendDOMNodeId == backendNodeId);
51                if (needle == null)
52                {
53                    return null;
54                }
55            }
56
57            if (options?.InterestingOnly == false)
58            {
59                return SerializeTree(needle)[0];
60            }
61
62            var interestingNodes = new List<AXNode>();
63            CollectInterestingNodes(interestingNodes, defaultRoot, false);
64            if (!interestingNodes.Contains(needle))
65            {
66                return null;
67            }
68            return SerializeTree(needle, interestingNodes)[0];
69        }
70
71        private void CollectInterestingNodes(List<AXNode> collection, AXNode node, bool insideControl)
72        {
73            if (node.IsInteresting(insideControl))
74            {
75                collection.Add(node);
76            }
77            if (node.IsLeafNode())
78            {
79                return;
80            }
81            insideControl = insideControl || node.IsControl();
82            foreach (var child in node.Children)
83            {
84                CollectInterestingNodes(collection, child, insideControl);
85            }
86        }
87
88        private SerializedAXNode[] SerializeTree(AXNode node, List<AXNode> whitelistedNodes = null)
89        {
90            var children = new List<SerializedAXNode>();
91            foreach (var child in node.Children)
92            {
93                children.AddRange(SerializeTree(child, whitelistedNodes));
94            }
95            if (whitelistedNodes?.Contains(node) == false)
96            {
97                return children.ToArray();
98            }
99
100            var serializedNode = node.Serialize();
101            if (children.Count > 0)
102            {
103                serializedNode.Children = children.ToArray();
104            }
105            return new[] { serializedNode };
106        }
107    }
108}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)