How to use AccesibilityTests method of PuppeteerSharp.Tests.AccesibilityTests.AccesibilityTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.AccesibilityTests.AccesibilityTests.AccesibilityTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

AccessibilityTests.cs

Source: AccessibilityTests.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.PageAccessibility;
3using Xunit;
4using Xunit.Abstractions;
5
6namespace PuppeteerSharp.Tests.AccesibilityTests
7{
8    [Collection("PuppeteerLoaderFixture collection")]
9    public class AccesibilityTests : PuppeteerPageBaseTest
10    {
11        public AccesibilityTests(ITestOutputHelper output) : base(output)
12        {
13        }
14
15        [Fact]
16        public async Task ShouldWork()
17        {
18            await Page.SetContentAsync(@"
19            <head>
20                <title>Accessibility Test</title>
21            </head>
22            <body>
23                <div>Hello World</div>
24                <h1>Inputs</h1>
25                <input placeholder='Empty input' autofocus />
26                <input placeholder='readonly input' readonly />
27                <input placeholder='disabled input' disabled />
28                <input aria-label='Input with whitespace' value='  ' />
29                <input value='value only' />
30                <input aria-placeholder='placeholder' value='and a value' />
31                <div aria-hidden='true' id='desc'>This is a description!</div>
32                <input aria-placeholder='placeholder' value='and a value' aria-describedby='desc' />
33                <select>
34                    <option>First Option</option>
35                    <option>Second Option</option>
36                </select>
37            </body>");
38            Assert.Equal(
39                new SerializedAXNode
40                {
41                    Role = "WebArea",
42                    Name = "Accessibility Test",
43                    Children = new SerializedAXNode[]
44                    {
45                        new SerializedAXNode
46                        {
47                            Role = "text",
48                            Name = "Hello World"
49                        },
50                        new SerializedAXNode
51                        {
52                            Role = "heading",
53                            Name = "Inputs",
54                            Level = 1
55                        },
56                        new SerializedAXNode{
57                            Role = "textbox",
58                            Name = "Empty input",
59                            Focused = true
60                        },
61                        new SerializedAXNode{
62                            Role = "textbox",
63                            Name = "readonly input",
64                            Readonly = true
65                        },
66                        new SerializedAXNode{
67                            Role = "textbox",
68                            Name = "disabled input",
69                            Disabled= true
70                        },
71                        new SerializedAXNode{
72                            Role = "textbox",
73                            Name = "Input with whitespace",
74                            Value= "  "
75                        },
76                        new SerializedAXNode{
77                            Role = "textbox",
78                            Name = "",
79                            Value= "value only"
80                        },
81                        new SerializedAXNode{
82                            Role = "textbox",
83                            Name = "placeholder",
84                            Value= "and a value"
85                        },
86                        new SerializedAXNode{
87                            Role = "textbox",
88                            Name = "placeholder",
89                            Value= "and a value",
90                            Description= "This is a description!"},
91                        new SerializedAXNode{
92                            Role= "combobox",
93                            Name= "",
94                            Value= "First Option",
95                            Children= new SerializedAXNode[]{
96                                new SerializedAXNode
97                                {
98                                    Role = "menuitem",
99                                    Name = "First Option",
100                                    Selected= true
101                                },
102                                new SerializedAXNode
103                                {
104                                    Role = "menuitem",
105                                    Name = "Second Option"
106                                }
107                            }
108                        }
109                    }
110                },
111                await Page.Accessibility.SnapshotAsync());
112        }
113
114        [Fact]
115        public async Task ShouldReportUninterestingNodes()
116        {
117            await Page.SetContentAsync("<textarea autofocus>hi</textarea>");
118            Assert.Equal(
119                new SerializedAXNode
120                {
121                    Role = "textbox",
122                    Name = "",
123                    Value = "hi",
124                    Focused = true,
125                    Multiline = true,
126                    Children = new SerializedAXNode[]
127                    {
128                        new SerializedAXNode
129                        {
130                            Role = "GenericContainer",
131                            Name = "",
132                            Children = new SerializedAXNode[]
133                            {
134                                new SerializedAXNode
135                                {
136                                    Role = "text",
137                                    Name = "hi"
138                                }
139                            }
140                        }
141                    }
142                },
143                FindFocusedNode(await Page.Accessibility.SnapshotAsync(new AccessibilitySnapshotOptions
144                {
145                    InterestingOnly = false
146                })));
147        }
148
149        [Fact]
150        public async Task RoleDescription()
151        {
152            await Page.SetContentAsync("<div tabIndex=-1 aria-roledescription='foo'>Hi</div>");
153            var snapshot = await Page.Accessibility.SnapshotAsync();
154            Assert.Equal("foo", snapshot.Children[0].RoleDescription);
155        }
156
157        [Fact]
158        public async Task Orientation()
159        {
160            await Page.SetContentAsync("<a href='' role='slider' aria-orientation='vertical'>11</a>");
161            var snapshot = await Page.Accessibility.SnapshotAsync();
162            Assert.Equal("vertical", snapshot.Children[0].Orientation);
163        }
164
165        [Fact]
166        public async Task AutoComplete()
167        {
168            await Page.SetContentAsync("<input type='number' aria-autocomplete='list' />");
169            var snapshot = await Page.Accessibility.SnapshotAsync();
170            Assert.Equal("list", snapshot.Children[0].AutoComplete);
171        }
172
173        [Fact]
174        public async Task MultiSelectable()
175        {
176            await Page.SetContentAsync("<div role='grid' tabIndex=-1 aria-multiselectable=true>hey</div>");
177            var snapshot = await Page.Accessibility.SnapshotAsync();
178            Assert.True(snapshot.Children[0].Multiselectable);
179        }
180
181        [Fact]
182        public async Task KeyShortcuts()
183        {
184            await Page.SetContentAsync("<div role='grid' tabIndex=-1 aria-keyshortcuts='foo'>hey</div>");
185            var snapshot = await Page.Accessibility.SnapshotAsync();
186            Assert.Equal("foo", snapshot.Children[0].KeyShortcuts);
187        }
188
189        [Fact]
190        public async Task ShouldNotReportTextNodesInsideControls()
191        {
192            await Page.SetContentAsync(@"
193            <div role='tablist'>
194                <div role='tab' aria-selected='true'><b>Tab1</b></div>
195                <div role='tab'>Tab2</div>
196            </div>");
197            Assert.Equal(
198                new SerializedAXNode
199                {
200                    Role = "WebArea",
201                    Name = "",
202                    Children = new SerializedAXNode[]
203                    {
204                        new SerializedAXNode
205                        {
206                            Role = "tab",
207                            Name = "Tab1",
208                            Selected = true
209                        },
210                        new SerializedAXNode
211                        {
212                            Role = "tab",
213                            Name = "Tab2"
214                        }
215                    }
216                },
217                await Page.Accessibility.SnapshotAsync());
218        }
219
220        [Fact]
221        public async Task RichTextEditableFieldsShouldHaveChildren()
222        {
223            await Page.SetContentAsync(@"
224            <div contenteditable='true'>
225                Edit this image: <img src='fakeimage.png' alt='my fake image'>
226            </div>");
227            Assert.Equal(
228                new SerializedAXNode
229                {
230                    Role = "GenericContainer",
231                    Name = "",
232                    Value = "Edit this image: ",
233                    Children = new SerializedAXNode[]
234                    {
235                        new SerializedAXNode
236                        {
237                            Role = "text",
238                            Name = "Edit this image:"
239                        },
240                        new SerializedAXNode
241                        {
242                            Role = "img",
243                            Name = "my fake image"
244                        }
245                    }
246                },
247                (await Page.Accessibility.SnapshotAsync()).Children[0]);
248        }
249
250        [Fact]
251        public async Task RichTextEditableFieldsWithRoleShouldHaveChildren()
252        {
253            await Page.SetContentAsync(@"
254            <div contenteditable='true' role='textbox'>
255                Edit this image: <img src='fakeimage.png' alt='my fake image'>
256            </div>");
257            Assert.Equal(
258                new SerializedAXNode
259                {
260                    Role = "textbox",
261                    Name = "",
262                    Value = "Edit this image: ",
263                    Children = new SerializedAXNode[]
264                    {
265                        new SerializedAXNode
266                        {
267                            Role = "text",
268                            Name = "Edit this image:"
269                        },
270                        new SerializedAXNode
271                        {
272                            Role = "img",
273                            Name = "my fake image"
274                        }
275                    }
276                },
277                (await Page.Accessibility.SnapshotAsync()).Children[0]);
278        }
279
280        [Fact]
281        public async Task PlainTextFieldWithRoleShouldNotHaveChildren()
282        {
283            await Page.SetContentAsync("<div contenteditable='plaintext-only' role='textbox'>Edit this image:<img src='fakeimage.png' alt='my fake image'></div>");
284            Assert.Equal(
285                new SerializedAXNode
286                {
287                    Role = "textbox",
288                    Name = "",
289                    Value = "Edit this image:"
290                },
291                (await Page.Accessibility.SnapshotAsync()).Children[0]);
292        }
293
294        [Fact]
295        public async Task PlainTextFieldWithTabindexAndWithoutRoleShouldNotHaveContent()
296        {
297            await Page.SetContentAsync("<div contenteditable='plaintext-only' role='textbox' tabIndex=0>Edit this image:<img src='fakeimage.png' alt='my fake image'></div>");
298            Assert.Equal(
299                new SerializedAXNode
300                {
301                    Role = "textbox",
302                    Name = "",
303                    Value = "Edit this image:"
304                },
305                (await Page.Accessibility.SnapshotAsync()).Children[0]);
306        }
307
308        [Fact]
309        public async Task PlainTextFieldWithoutRoleShouldNotHaveContent()
310        {
311            await Page.SetContentAsync(
312                "<div contenteditable='plaintext-only'>Edit this image:<img src='fakeimage.png' alt='my fake image'></div>");
313            var snapshot = await Page.Accessibility.SnapshotAsync();
314            Assert.Equal("GenericContainer", snapshot.Children[0].Role);
315            Assert.Equal(string.Empty, snapshot.Children[0].Name);
316        }
317
318        [Fact]
319        public async Task NonEditableTextboxWithRoleAndTabIndexAndLabelShouldNotHaveChildren()
320        {
321            await Page.SetContentAsync(@"
322            <div role='textbox' tabIndex=0 aria-checked='true' aria-label='my favorite textbox'>
323                this is the inner content
324                <img alt='yo' src='fakeimg.png'>
325            </div>");
326            Assert.Equal(
327                new SerializedAXNode
328                {
329                    Role = "textbox",
330                    Name = "my favorite textbox",
331                    Value = "this is the inner content "
332                },
333                (await Page.Accessibility.SnapshotAsync()).Children[0]);
334        }
335
336        [Fact]
337        public async Task CheckboxWithAndTabIndexAndLabelShouldNotHaveChildren()
338        {
339            await Page.SetContentAsync(@"
340            <div role='checkbox' tabIndex=0 aria-checked='true' aria-label='my favorite checkbox'>
341                this is the inner content
342                <img alt='yo' src='fakeimg.png'>
343            </div>");
344            Assert.Equal(
345                new SerializedAXNode
346                {
347                    Role = "checkbox",
348                    Name = "my favorite checkbox",
349                    Checked = CheckedState.True
350                },
351                (await Page.Accessibility.SnapshotAsync()).Children[0]);
352        }
353
354        [Fact]
355        public async Task CheckboxWithoutLabelShouldNotHaveChildren()
356        {
357            await Page.SetContentAsync(@"
358            <div role='checkbox' aria-checked='true'>
359                this is the inner content
360                <img alt='yo' src='fakeimg.png'>
361            </div>");
362            Assert.Equal(
363                new SerializedAXNode
364                {
365                    Role = "checkbox",
366                    Name = "this is the inner content yo",
367                    Checked = CheckedState.True
368                },
369                (await Page.Accessibility.SnapshotAsync()).Children[0]);
370        }
371
372        private SerializedAXNode FindFocusedNode(SerializedAXNode serializedAXNode)
373        {
374            if (serializedAXNode.Focused)
375            {
376                return serializedAXNode;
377            }
378            foreach (var item in serializedAXNode.Children)
379            {
380                var focusedChild = FindFocusedNode(item);
381                if (focusedChild != null)
382                {
383                    return focusedChild;
384                }
385            }
386
387            return null;
388        }
389    }
390}
391
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
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)