-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtest_a11y.py
More file actions
438 lines (347 loc) · 13.5 KB
/
test_a11y.py
File metadata and controls
438 lines (347 loc) · 13.5 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import pytest
from dash import Dash, Input, Output
from dash.dcc import Dropdown
from dash.html import Div, Label, P
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
def test_a11y001_label_focuses_dropdown(dash_duo):
app = Dash(__name__)
app.layout = Label(
[
P("Click me", id="label"),
Dropdown(
id="dropdown",
options=[1, 2, 3],
multi=True,
placeholder="Testing label that wraps a dropdown can trigger the dropdown",
),
],
)
dash_duo.start_server(app)
dash_duo.wait_for_element("#dropdown")
with pytest.raises(TimeoutException):
dash_duo.wait_for_element(".dash-dropdown-options", timeout=0.25)
dash_duo.find_element("#label").click()
dash_duo.wait_for_element(".dash-dropdown-options")
assert dash_duo.get_logs() == []
def test_a11y002_label_with_htmlFor_can_focus_dropdown(dash_duo):
app = Dash(__name__)
app.layout = Div(
[
Label("Click me", htmlFor="dropdown", id="label"),
Dropdown(
id="dropdown",
options=[1, 2, 3],
multi=True,
placeholder="Testing label with `htmlFor` triggers the dropdown",
),
],
)
dash_duo.start_server(app)
dash_duo.wait_for_element("#dropdown")
with pytest.raises(TimeoutException):
dash_duo.wait_for_element(".dash-dropdown-options", timeout=0.25)
dash_duo.find_element("#label").click()
dash_duo.wait_for_element(".dash-dropdown-options")
assert dash_duo.get_logs() == []
def test_a11y003_keyboard_navigation(dash_duo):
def send_keys(key):
actions = ActionChains(dash_duo.driver)
actions.send_keys(key)
actions.perform()
app = Dash(__name__)
app.layout = Div(
[
Dropdown(
id="dropdown",
options=[i for i in range(0, 100)],
multi=True,
placeholder="Testing keyboard navigation",
),
],
)
dash_duo.start_server(app)
dropdown = dash_duo.find_element("#dropdown")
dropdown.send_keys(Keys.ENTER) # Open with Enter key
dash_duo.wait_for_element(".dash-dropdown-options")
send_keys(
Keys.ESCAPE
) # Expecting focus to remain on the dropdown after escaping out
with pytest.raises(TimeoutException):
dash_duo.wait_for_element(".dash-dropdown-options", timeout=0.25)
send_keys(Keys.ARROW_DOWN) # Expecting the dropdown to open up
dash_duo.wait_for_element(".dash-dropdown-search")
num_elements = len(dash_duo.find_elements(".dash-dropdown-option"))
assert num_elements == 100
send_keys(1) # Expecting to be typing into the searh bar
sleep(0.1) # Wait for search filtering to complete
num_elements = len(dash_duo.find_elements(".dash-dropdown-option"))
assert num_elements == 19
send_keys(Keys.ARROW_DOWN) # Expecting to be navigating through the options
send_keys(Keys.SPACE) # Expecting to be selecting
value_items = dash_duo.find_elements(".dash-dropdown-value-item")
assert len(value_items) == 1
assert value_items[0].text == "1"
send_keys(Keys.ARROW_DOWN) # Expecting to be navigating through the options
send_keys(Keys.SPACE) # Expecting to be selecting
value_items = dash_duo.find_elements(".dash-dropdown-value-item")
assert len(value_items) == 2
assert [item.text for item in value_items] == ["1", "10"]
send_keys(Keys.SPACE) # Expecting to be de-selecting
value_items = dash_duo.find_elements(".dash-dropdown-value-item")
assert len(value_items) == 1
assert value_items[0].text == "1"
send_keys(Keys.ARROW_UP)
send_keys(Keys.ARROW_UP)
send_keys(Keys.ARROW_UP) # Expecting to wrap over to the last item
send_keys(Keys.SPACE)
value_items = dash_duo.find_elements(".dash-dropdown-value-item")
assert len(value_items) == 2
assert [item.text for item in value_items] == ["1", "91"]
send_keys(
Keys.ESCAPE
) # Expecting focus to remain on the dropdown after escaping out
sleep(0.25)
value_items = dash_duo.find_elements(".dash-dropdown-value-item")
assert len(value_items) == 2
assert [item.text for item in value_items] == ["1", "91"]
assert dash_duo.get_logs() == []
def test_a11y004_selection_visibility_single(dash_duo):
app = Dash(__name__)
app.layout = (
Dropdown(
id="dropdown",
options=[f"Option {i}" for i in range(0, 100)],
value="Option 71",
multi=False,
placeholder="Testing selected item is visible on open",
),
)
dash_duo.start_server(app)
dash_duo.wait_for_element("#dropdown")
dash_duo.find_element("#dropdown").click()
dash_duo.wait_for_element(".dash-dropdown-options")
# Assert that the selected option is visible in the dropdown
selected_option = dash_duo.find_element(".dash-dropdown-option.selected")
assert selected_option.text == "Option 71"
assert selected_option.is_displayed()
assert elements_are_visible(
dash_duo, selected_option
), "Selected option should be visible when the dropdown opens"
assert dash_duo.get_logs() == []
def test_a11y005_selection_visibility_multi(dash_duo):
app = Dash(__name__)
app.layout = (
Dropdown(
id="dropdown",
options=[f"Option {i}" for i in range(0, 100)],
value=[
"Option 71",
"Option 23",
"Option 42",
],
multi=True,
placeholder="Testing selected item is visible on open",
),
)
dash_duo.start_server(app)
dash_duo.wait_for_element("#dropdown")
dash_duo.find_element("#dropdown").click()
dash_duo.wait_for_element(".dash-dropdown-options")
# Assert that the selected option is visible in the dropdown
selected_options = dash_duo.find_elements(".dash-dropdown-option.selected")
assert elements_are_visible(
dash_duo, selected_options
), "Selected options should be visible when the dropdown opens"
assert dash_duo.get_logs() == []
def test_a11y006_multi_select_keyboard_focus_retention(dash_duo):
def send_keys(key):
actions = ActionChains(dash_duo.driver)
actions.send_keys(key)
actions.perform()
app = Dash(__name__)
app.layout = Div(
[
Dropdown(
id="dropdown",
options=[f"Option {i}" for i in range(0, 10)],
value=[],
multi=True,
searchable=True,
),
Div(id="output"),
]
)
@app.callback(
Output("output", "children"),
Input("dropdown", "value"),
)
def update_output(value):
return f"Selected: {value}"
dash_duo.start_server(app)
dropdown = dash_duo.find_element("#dropdown")
dropdown.click()
dash_duo.wait_for_element(".dash-dropdown-options")
# Select 3 items by alternating ArrowDown and Spacebar
send_keys(Keys.ARROW_DOWN) # Move to first option
sleep(0.05)
send_keys(Keys.SPACE) # Select Option 0
dash_duo.wait_for_text_to_equal("#output", "Selected: ['Option 0']")
send_keys(Keys.ARROW_DOWN) # Move to second option
sleep(0.05)
send_keys(Keys.SPACE) # Select Option 1
dash_duo.wait_for_text_to_equal("#output", "Selected: ['Option 0', 'Option 1']")
send_keys(Keys.ARROW_DOWN) # Move to third option
sleep(0.05)
send_keys(Keys.SPACE) # Select Option 2
dash_duo.wait_for_text_to_equal(
"#output", "Selected: ['Option 0', 'Option 1', 'Option 2']"
)
assert dash_duo.get_logs() == []
def test_a11y007_opens_and_closes_without_races(dash_duo):
def send_keys(key):
actions = ActionChains(dash_duo.driver)
actions.send_keys(key)
actions.perform()
app = Dash(__name__)
app.layout = Div(
[
Dropdown(
id="dropdown",
options=[f"Option {i}" for i in range(0, 10)],
value="Option 5",
multi=False,
),
Div(id="output"),
]
)
def assert_focus_in_dropdown():
# Verify focus is inside the dropdown
assert dash_duo.driver.execute_script(
"""
const activeElement = document.activeElement;
const dropdownContent = document.querySelector('.dash-dropdown-content');
return dropdownContent && dropdownContent.contains(activeElement);
"""
), "Focus must be inside the dropdown when it opens"
@app.callback(
Output("output", "children"),
Input("dropdown", "value"),
)
def update_output(value):
return f"Selected: {value}"
dash_duo.start_server(app)
# Verify initial value is set
dash_duo.wait_for_text_to_equal("#output", "Selected: Option 5")
dropdown = dash_duo.find_element("#dropdown")
# Test repeated open/close to confirm no race conditions or side effects
for i in range(3):
# Open with Enter
dropdown.send_keys(Keys.ENTER)
dash_duo.wait_for_element(".dash-dropdown-options")
assert_focus_in_dropdown()
# Verify the value is still "Option 5" (not cleared)
dash_duo.wait_for_text_to_equal("#output", "Selected: Option 5")
# Close with Escape
send_keys(Keys.ESCAPE)
sleep(0.1)
# Verify the value is still "Option 5"
dash_duo.wait_for_text_to_equal("#output", "Selected: Option 5")
for i in range(3):
# Open with mouse
dropdown.click()
dash_duo.wait_for_element(".dash-dropdown-options")
assert_focus_in_dropdown()
# Verify the value is still "Option 5" (not cleared)
dash_duo.wait_for_text_to_equal("#output", "Selected: Option 5")
# Close with Escape
dropdown.click()
sleep(0.1)
# Verify the value is still "Option 5"
dash_duo.wait_for_text_to_equal("#output", "Selected: Option 5")
assert dash_duo.get_logs() == []
def test_a11y008_home_end_pageup_pagedown_navigation(dash_duo_fresh_browser):
dash_duo = dash_duo_fresh_browser
def send_keys(key):
actions = ActionChains(dash_duo.driver)
actions.send_keys(key)
actions.perform()
def get_focused_option_text():
return dash_duo.driver.execute_script(
"""
const focused = document.activeElement;
if (focused && focused.closest('.dash-options-list-option')) {
return focused.closest('.dash-options-list-option').textContent.trim();
}
return null;
"""
)
app = Dash(__name__)
app.layout = Div(
[
Dropdown(
id="dropdown",
options=[f"Option {i}" for i in range(0, 50)],
multi=True,
),
]
)
dash_duo.start_server(app)
dropdown = dash_duo.find_element("#dropdown")
dropdown.send_keys(Keys.ENTER) # Open with Enter key
dash_duo.wait_for_element(".dash-dropdown-options")
# Navigate from search input to options
send_keys(Keys.ARROW_DOWN) # Move from search to first option
sleep(0.05)
send_keys(Keys.ARROW_DOWN) # Move to second option
sleep(0.05)
send_keys(Keys.ARROW_DOWN) # Move to third option
sleep(0.05)
send_keys(Keys.ARROW_DOWN) # Move to fourth option
sleep(0.05)
assert get_focused_option_text() == "Option 3"
send_keys(Keys.HOME) # Should go back to search input (index 0)
# Verify we're back at search input
assert dash_duo.driver.execute_script(
"return document.activeElement.type === 'search';"
)
# Now arrow down to first option
send_keys(Keys.ARROW_DOWN)
assert get_focused_option_text() == "Option 0"
# Test End key - should go to last option
send_keys(Keys.END)
assert get_focused_option_text() == "Option 49"
# Test PageUp - should jump up by 10
send_keys(Keys.PAGE_UP)
assert get_focused_option_text() == "Option 39"
# Test PageDown - should jump down by 10
send_keys(Keys.PAGE_DOWN)
assert get_focused_option_text() == "Option 49"
# Test PageUp from middle
send_keys(Keys.HOME) # Back to search input (index 0)
send_keys(Keys.PAGE_DOWN) # Jump to index 10 (Option 9)
send_keys(Keys.PAGE_DOWN) # Jump to index 20 (Option 19)
assert get_focused_option_text() == "Option 19"
send_keys(Keys.PAGE_UP) # Jump to index 10 (Option 9)
assert get_focused_option_text() == "Option 9"
assert dash_duo.get_logs() == []
def elements_are_visible(dash_duo, elements):
# Check if the given elements are within the visible viewport of the dropdown
elements = elements if isinstance(elements, list) else [elements]
dropdown_content = dash_duo.find_element(".dash-dropdown-content")
def is_visible(el):
return dash_duo.driver.execute_script(
"""
const option = arguments[0];
const container = arguments[1];
const optionRect = option.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
return optionRect.top >= containerRect.top &&
optionRect.bottom <= containerRect.bottom;
""",
el,
dropdown_content,
)
return all([is_visible(el) for el in elements])