forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_accessibility.py
413 lines (365 loc) · 13.7 KB
/
test_accessibility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
def test_accessibility_should_work(page, is_firefox, is_chromium):
page.set_content(
"""<head>
<title>Accessibility Test</title>
</head>
<body>
<h1>Inputs</h1>
<input placeholder="Empty input" autofocus />
<input placeholder="readonly input" readonly />
<input placeholder="disabled input" disabled />
<input aria-label="Input with whitespace" value=" " />
<input value="value only" />
<input aria-placeholder="placeholder" value="and a value" />
<div aria-hidden="true" id="desc">This is a description!</div>
<input aria-placeholder="placeholder" value="and a value" aria-describedby="desc" />
</body>"""
)
# autofocus happens after a delay in chrome these days
page.wait_for_function("document.activeElement.hasAttribute('autofocus')")
if is_firefox:
golden = {
"role": "document",
"name": "Accessibility Test",
"children": [
{"role": "heading", "name": "Inputs", "level": 1},
{"role": "textbox", "name": "Empty input", "focused": True},
{"role": "textbox", "name": "readonly input", "readonly": True},
{"role": "textbox", "name": "disabled input", "disabled": True},
{"role": "textbox", "name": "Input with whitespace", "value": " "},
{"role": "textbox", "name": "", "value": "value only"},
{
"role": "textbox",
"name": "",
"value": "and a value",
}, # firefox doesn't use aria-placeholder for the name
{
"role": "textbox",
"name": "",
"value": "and a value",
"description": "This is a description!",
}, # and here
],
}
elif is_chromium:
golden = {
"role": "WebArea",
"name": "Accessibility Test",
"children": [
{"role": "heading", "name": "Inputs", "level": 1},
{"role": "textbox", "name": "Empty input", "focused": True},
{"role": "textbox", "name": "readonly input", "readonly": True},
{"role": "textbox", "name": "disabled input", "disabled": True},
{"role": "textbox", "name": "Input with whitespace", "value": " "},
{"role": "textbox", "name": "", "value": "value only"},
{"role": "textbox", "name": "placeholder", "value": "and a value"},
{
"role": "textbox",
"name": "placeholder",
"value": "and a value",
"description": "This is a description!",
},
],
}
else:
golden = {
"role": "WebArea",
"name": "Accessibility Test",
"children": [
{"role": "heading", "name": "Inputs", "level": 1},
{"role": "textbox", "name": "Empty input", "focused": True},
{"role": "textbox", "name": "readonly input", "readonly": True},
{"role": "textbox", "name": "disabled input", "disabled": True},
{"role": "textbox", "name": "Input with whitespace", "value": " "},
{"role": "textbox", "name": "", "value": "value only"},
{"role": "textbox", "name": "placeholder", "value": "and a value"},
{
"role": "textbox",
"name": "This is a description!",
"value": "and a value",
}, # webkit uses the description over placeholder for the name
],
}
assert page.accessibility.snapshot() == golden
def test_accessibility_should_work_with_regular_text(page, is_firefox):
page.set_content("<div>Hello World</div>")
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == {
"role": "text leaf" if is_firefox else "text",
"name": "Hello World",
}
def test_accessibility_roledescription(page):
page.set_content('<div tabIndex=-1 aria-roledescription="foo">Hi</div>')
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0]["roledescription"] == "foo"
def test_accessibility_orientation(page):
page.set_content('<a href="" role="slider" aria-orientation="vertical">11</a>')
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0]["orientation"] == "vertical"
def test_accessibility_autocomplete(page):
page.set_content('<div role="textbox" aria-autocomplete="list">hi</div>')
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0]["autocomplete"] == "list"
def test_accessibility_multiselectable(page):
page.set_content('<div role="grid" tabIndex=-1 aria-multiselectable=true>hey</div>')
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0]["multiselectable"]
def test_accessibility_keyshortcuts(page):
page.set_content('<div role="grid" tabIndex=-1 aria-keyshortcuts="foo">hey</div>')
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0]["keyshortcuts"] == "foo"
def test_accessibility_filtering_children_of_leaf_nodes_should_not_report_text_nodes_inside_controls(
page, is_firefox
):
page.set_content(
"""
<div role="tablist">
<div role="tab" aria-selected="true"><b>Tab1</b></div>
<div role="tab">Tab2</div>
</div>"""
)
golden = {
"role": "document" if is_firefox else "WebArea",
"name": "",
"children": [
{"role": "tab", "name": "Tab1", "selected": True},
{"role": "tab", "name": "Tab2"},
],
}
assert page.accessibility.snapshot() == golden
# WebKit rich text accessibility is iffy
@pytest.mark.skip_browser("webkit")
def test_accessibility_filtering_children_of_leaf_nodes_rich_text_editable_fields_should_have_children(
page, is_firefox
):
page.set_content(
"""
<div contenteditable="true">
Edit this image: <img src="fakeimage.png" alt="my fake image">
</div>"""
)
golden = (
{
"role": "section",
"name": "",
"children": [
{"role": "text leaf", "name": "Edit this image: "},
{"role": "text", "name": "my fake image"},
],
}
if is_firefox
else {
"role": "generic",
"name": "",
"value": "Edit this image: ",
"children": [
{"role": "text", "name": "Edit this image:"},
{"role": "img", "name": "my fake image"},
],
}
)
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == golden
# WebKit rich text accessibility is iffy
@pytest.mark.skip_browser("webkit")
def test_accessibility_filtering_children_of_leaf_nodes_rich_text_editable_fields_with_role_should_have_children(
page,
is_firefox,
):
page.set_content(
"""
<div contenteditable="true" role='textbox'>
Edit this image: <img src="fakeimage.png" alt="my fake image">
</div>"""
)
if is_firefox:
golden = {
"role": "textbox",
"name": "",
"value": "Edit this image: my fake image",
"children": [{"role": "text", "name": "my fake image"}],
}
else:
golden = {
"role": "textbox",
"name": "",
"value": "Edit this image: ",
"children": [
{"role": "text", "name": "Edit this image:"},
{"role": "img", "name": "my fake image"},
],
}
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == golden
# Firefox does not support contenteditable="plaintext-only".
# WebKit rich text accessibility is iffy
@pytest.mark.only_browser("chromium")
def test_accessibility_plain_text_field_with_role_should_not_have_children(page):
page.set_content(
"""
<div contenteditable="plaintext-only" role='textbox'>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
)
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == {
"role": "textbox",
"name": "",
"value": "Edit this image:",
}
@pytest.mark.only_browser("chromium")
def test_accessibility_plain_text_field_without_role_should_not_have_content(
page,
):
page.set_content(
"""
<div contenteditable="plaintext-only">Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
)
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == {"role": "generic", "name": ""}
@pytest.mark.only_browser("chromium")
def test_accessibility_plain_text_field_with_tabindex_and_without_role_should_not_have_content(
page,
):
page.set_content(
"""
<div contenteditable="plaintext-only" tabIndex=0>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
)
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == {"role": "generic", "name": ""}
def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_label_should_not_have_children(
page, is_chromium, is_firefox
):
page.set_content(
"""
<div role="textbox" tabIndex=0 aria-checked="true" aria-label="my favorite textbox">
this is the inner content
<img alt="yo" src="fakeimg.png">
</div>"""
)
if is_firefox:
golden = {
"role": "textbox",
"name": "my favorite textbox",
"value": "this is the inner content yo",
}
elif is_chromium:
golden = {
"role": "textbox",
"name": "my favorite textbox",
"value": "this is the inner content ",
}
else:
golden = {
"role": "textbox",
"name": "my favorite textbox",
"value": "this is the inner content ",
}
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == golden
def test_accessibility_checkbox_with_and_tabIndex_and_label_should_not_have_children(
page,
):
page.set_content(
"""
<div role="checkbox" tabIndex=0 aria-checked="true" aria-label="my favorite checkbox">
this is the inner content
<img alt="yo" src="fakeimg.png">
</div>"""
)
golden = {"role": "checkbox", "name": "my favorite checkbox", "checked": True}
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == golden
def test_accessibility_checkbox_without_label_should_not_have_children(
page, is_firefox
):
page.set_content(
"""
<div role="checkbox" aria-checked="true">
this is the inner content
<img alt="yo" src="fakeimg.png">
</div>"""
)
golden = {
"role": "checkbox",
"name": "this is the inner content yo",
"checked": True,
}
snapshot = page.accessibility.snapshot()
assert snapshot["children"][0] == golden
def test_accessibility_should_work_a_button(page):
page.set_content("<button>My Button</button>")
button = page.query_selector("button")
assert page.accessibility.snapshot(root=button) == {
"role": "button",
"name": "My Button",
}
def test_accessibility_should_work_an_input(page):
page.set_content('<input title="My Input" value="My Value">')
input = page.query_selector("input")
assert page.accessibility.snapshot(root=input) == {
"role": "textbox",
"name": "My Input",
"value": "My Value",
}
def test_accessibility_should_work_on_a_menu(page, is_webkit):
page.set_content(
"""
<div role="menu" title="My Menu">
<div role="menuitem">First Item</div>
<div role="menuitem">Second Item</div>
<div role="menuitem">Third Item</div>
</div>
"""
)
menu = page.query_selector('div[role="menu"]')
golden = {
"role": "menu",
"name": "My Menu",
"children": [
{"role": "menuitem", "name": "First Item"},
{"role": "menuitem", "name": "Second Item"},
{"role": "menuitem", "name": "Third Item"},
],
}
if is_webkit:
golden["orientation"] = "vertical"
assert page.accessibility.snapshot(root=menu) == golden
def test_accessibility_should_return_null_when_the_element_is_no_longer_in_DOM(
page,
):
page.set_content("<button>My Button</button>")
button = page.query_selector("button")
page.eval_on_selector("button", "button => button.remove()")
assert page.accessibility.snapshot(root=button) is None
def test_accessibility_should_show_uninteresting_nodes(page):
page.set_content(
"""
<div id="root" role="textbox">
<div>
hello
<div>
world
</div>
</div>
</div>
"""
)
root = page.query_selector("#root")
snapshot = page.accessibility.snapshot(root=root, interesting_only=False)
assert snapshot["role"] == "textbox"
assert "hello" in snapshot["value"]
assert "world" in snapshot["value"]
assert snapshot["children"]