| 1 |
""" |
| 2 |
Using Axe-core, scan the Kitchen Sink pages for accessibility violations. |
| 3 |
Note that in contrast with the rest of our tests, the accessibility tests in this file |
| 4 |
are run against a build of our PST documentation, not purposedly-built test sites. |
| 5 |
""" |
| 6 |
|
| 7 |
from urllib.parse import urljoin |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
|
| 12 |
# Using importorskip to ensure these tests are only loaded if Playwright is installed. |
| 13 |
playwright = pytest.importorskip("playwright") |
| 14 |
from playwright.sync_api import Page, expect # noqa: E402 |
| 15 |
|
| 16 |
|
| 17 |
# Important note: automated accessibility scans can only find a fraction of |
| 18 |
# potential accessibility issues. |
| 19 |
# |
| 20 |
# This test file scans pages from the Kitchen Sink examples with a JavaScript |
| 21 |
# library called Axe-core, which checks the page for accessibility violations, |
| 22 |
# such as places on the page with poor color contrast that would be hard for |
| 23 |
# people with low vision to see. |
| 24 |
# |
| 25 |
# Just because a page passes the scan with no accessibility violations does |
| 26 |
# *not* mean that it will be generally usable by a broad range of disabled |
| 27 |
# people. It just means that page is free of common testable accessibility |
| 28 |
# pitfalls. |
| 29 |
|
| 30 |
|
| 31 |
def filter_ignored_violations(violations, url_pathname): |
| 32 |
"""Filter out ignored axe-core violations. |
| 33 |
|
| 34 |
In some tests, we wish to ignore certain accessibility violations that we |
| 35 |
won't ever fix or that we don't plan to fix soon. |
| 36 |
""" |
| 37 |
# we allow empty table headers |
| 38 |
# https://dequeuniversity.com/rules/axe/4.8/empty-table-header?application=RuleDescription |
| 39 |
if url_pathname == "/examples/pydata.html": |
| 40 |
return [v for v in violations if v["id"] != "empty-table-header"] |
| 41 |
elif url_pathname in [ |
| 42 |
"/examples/kitchen-sink/generic.html", |
| 43 |
"/user_guide/theme-elements.html", |
| 44 |
]: |
| 45 |
filtered = [] |
| 46 |
for violation in violations: |
| 47 |
# TODO: remove this exclusion once the following update to Axe is |
| 48 |
# released and we upgrade: |
| 49 |
# https://github.com/dequelabs/axe-core/pull/4469 |
| 50 |
if violation["id"] == "landmark-unique": |
| 51 |
# Ignore landmark-unique only for .sidebar targets. Don't ignore |
| 52 |
# it for other targets because then the test might fail to catch |
| 53 |
# a change that violates the rule in some other way. |
| 54 |
unexpected_nodes = [] |
| 55 |
for node in violation["nodes"]: |
| 56 |
# If some target is not .sidebar then we've found a rule |
| 57 |
# violation we weren't expecting |
| 58 |
if not all([".sidebar" in target for target in node["target"]]): |
| 59 |
unexpected_nodes.append(node) |
| 60 |
if unexpected_nodes: |
| 61 |
violation["nodes"] = unexpected_nodes |
| 62 |
filtered.append(violation) |
| 63 |
else: |
| 64 |
filtered.append(violation) |
| 65 |
return filtered |
| 66 |
else: |
| 67 |
return violations |
| 68 |
|
| 69 |
|
| 70 |
def format_violations(violations): |
| 71 |
"""Return a pretty string representation of Axe-core violations.""" |
| 72 |
result = f""" |
| 73 |
|
| 74 |
Found {len(violations)} accessibility violation(s): |
| 75 |
""" |
| 76 |
|
| 77 |
for violation in violations: |
| 78 |
result += f""" |
| 79 |
|
| 80 |
- Rule violated: |
| 81 |
{violation["id"]} - {violation["help"]} |
| 82 |
- URL: {violation["helpUrl"]} |
| 83 |
- Impact: {violation["impact"]} |
| 84 |
- Tags: {" ".join(violation["tags"])} |
| 85 |
- Targets:""" |
| 86 |
|
| 87 |
for node in violation["nodes"]: |
| 88 |
for target in node["target"]: |
| 89 |
result += f""" |
| 90 |
- {target}""" |
| 91 |
|
| 92 |
result += "\n\n" |
| 93 |
|
| 94 |
return result |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.a11y |
| 98 |
@pytest.mark.parametrize("theme", ["light", "dark"]) |
| 99 |
@pytest.mark.parametrize( |
| 100 |
"url_pathname,selector", |
| 101 |
[ |
| 102 |
( |
| 103 |
"/examples/kitchen-sink/admonitions.html", |
| 104 |
"#admonitions", |
| 105 |
), |
| 106 |
( |
| 107 |
"/examples/kitchen-sink/api.html", |
| 108 |
"#api-documentation", |
| 109 |
), |
| 110 |
("/examples/kitchen-sink/blocks.html", "#blocks"), |
| 111 |
( |
| 112 |
"/examples/kitchen-sink/generic.html", |
| 113 |
"#generic-items", |
| 114 |
), |
| 115 |
( |
| 116 |
"/examples/kitchen-sink/images.html", |
| 117 |
"#images-figures", |
| 118 |
), |
| 119 |
("/examples/kitchen-sink/lists.html", "#lists"), |
| 120 |
( |
| 121 |
"/examples/kitchen-sink/structure.html", |
| 122 |
"#structural-elements", |
| 123 |
), |
| 124 |
( |
| 125 |
"/examples/kitchen-sink/structure.html", |
| 126 |
"#structural-elements-2", |
| 127 |
), |
| 128 |
("/examples/kitchen-sink/tables.html", "#tables"), |
| 129 |
( |
| 130 |
"/examples/kitchen-sink/typography.html", |
| 131 |
"#typography", |
| 132 |
), |
| 133 |
("/examples/pydata.html", "#PyData-Library-Styles"), |
| 134 |
( |
| 135 |
"/user_guide/theme-elements.html", |
| 136 |
"#theme-specific-elements", |
| 137 |
), |
| 138 |
( |
| 139 |
"/user_guide/web-components.html", |
| 140 |
"#sphinx-design-components", |
| 141 |
), |
| 142 |
( |
| 143 |
"/user_guide/extending.html", |
| 144 |
"#extending-the-theme", |
| 145 |
), |
| 146 |
( |
| 147 |
"/user_guide/styling.html", |
| 148 |
"#theme-variables-and-css", |
| 149 |
), |
| 150 |
# Using one of the simplest pages on the site, select the whole page for |
| 151 |
# testing in order to effectively test repeated website elements like |
| 152 |
# nav, sidebars, breadcrumbs, footer |
| 153 |
( |
| 154 |
"/user_guide/page-toc.html", |
| 155 |
"", # select whole page |
| 156 |
), |
| 157 |
], |
| 158 |
) |
| 159 |
def test_axe_core( |
| 160 |
page: Page, |
| 161 |
url_base: str, |
| 162 |
theme: str, |
| 163 |
url_pathname: str, |
| 164 |
selector: str, |
| 165 |
): |
| 166 |
"""Should have no Axe-core violations at the provided theme and page section.""" |
| 167 |
# Load the page at the provided path |
| 168 |
url_full = urljoin(url_base, url_pathname) |
| 169 |
page.goto(url_full) |
| 170 |
|
| 171 |
# Run a line of JavaScript that sets the light/dark theme on the page |
| 172 |
page.evaluate(f"document.documentElement.dataset.theme = '{theme}'") |
| 173 |
|
| 174 |
# Wait for CSS transitions (Bootstrap's transitions are 300 ms) |
| 175 |
page.wait_for_timeout(301) |
| 176 |
|
| 177 |
# On the PyData Library Styles page, wait for ipywidget to load and for our |
| 178 |
# JavaScript to apply tabindex="0" before running Axe checker (to avoid |
| 179 |
# false positives for scrollable-region-focusable). |
| 180 |
if url_pathname == "/examples/pydata.html": |
| 181 |
ipywidgets_pandas_table = page.locator("css=.jp-RenderedHTMLCommon").first |
| 182 |
expect(ipywidgets_pandas_table).to_have_attribute("tabindex", "0") |
| 183 |
|
| 184 |
# Inject the Axe-core JavaScript library into the page |
| 185 |
page.add_script_tag(path="node_modules/axe-core/axe.min.js") |
| 186 |
|
| 187 |
# Run the Axe-core library against a section of the page (unless the |
| 188 |
# selector is empty, then run against the whole page) |
| 189 |
results = page.evaluate("axe.run()" if selector == "" else f"axe.run('{selector}')") |
| 190 |
|
| 191 |
# Check found violations against known violations that we do not plan to fix |
| 192 |
filtered_violations = filter_ignored_violations(results["violations"], url_pathname) |
| 193 |
|
| 194 |
# We expect notebook outputs on the PyData Library Styles page to have color |
| 195 |
# contrast failures. |
| 196 |
if url_pathname == "/examples/pydata.html": |
| 197 |
# All violations should be color contrast violations |
| 198 |
for violation in filtered_violations: |
| 199 |
assert ( |
| 200 |
violation["id"] == "color-contrast" |
| 201 |
), f"""Found {violation["id"]} violation (expected color-contrast): |
| 202 |
{format_violations([violation])}""" |
| 203 |
|
| 204 |
# Now check that when we exclude notebook outputs, the page has no violations |
| 205 |
|
| 206 |
results_sans_nbout = page.evaluate( |
| 207 |
f"axe.run({{ include: '{selector}', exclude: '.nboutput > .output_area' }})" |
| 208 |
) |
| 209 |
violations_sans_nbout = filter_ignored_violations( |
| 210 |
results_sans_nbout["violations"], url_pathname |
| 211 |
) |
| 212 |
|
| 213 |
# No violations on page when excluding notebook outputs |
| 214 |
assert len(violations_sans_nbout) == 0, format_violations(violations_sans_nbout) |
| 215 |
|
| 216 |
# TODO: for color contrast issues with common notebook outputs |
| 217 |
# (ipywidget tabbed panels, Xarray, etc.), should we override |
| 218 |
# third-party CSS with our own CSS or/and work with NbSphinx, MyST-NB, |
| 219 |
# ipywidgets, and other third parties to use higher contrast colors in |
| 220 |
# their CSS? |
| 221 |
pytest.xfail("notebook outputs have color contrast violations") |
| 222 |
|
| 223 |
assert len(filtered_violations) == 0, format_violations(filtered_violations) |
| 224 |
|
| 225 |
|
| 226 |
@pytest.mark.a11y |
| 227 |
def test_code_block_tab_stop(page: Page, url_base: str) -> None: |
| 228 |
"""Code blocks that have scrollable content should be tab stops.""" |
| 229 |
page.set_viewport_size({"width": 1440, "height": 720}) |
| 230 |
page.goto(urljoin(url_base, "/examples/kitchen-sink/blocks.html")) |
| 231 |
|
| 232 |
code_block = page.locator( |
| 233 |
"css=#code-block pre", has_text="from typing import Iterator" |
| 234 |
) |
| 235 |
|
| 236 |
# Viewport is wide, so code block content fits, no overflow, no tab stop |
| 237 |
assert code_block.evaluate("el => el.scrollWidth > el.clientWidth") is False |
| 238 |
assert code_block.evaluate("el => el.tabIndex") != 0 |
| 239 |
|
| 240 |
page.set_viewport_size({"width": 400, "height": 720}) |
| 241 |
|
| 242 |
# Narrow viewport, content overflows ... |
| 243 |
assert code_block.evaluate("el => el.scrollWidth > el.clientWidth") is True |
| 244 |
|
| 245 |
# ... and code block should be a tab stop. |
| 246 |
# |
| 247 |
# Note: expect() will wait until the expect condition is true (up to the |
| 248 |
# test timeout limit). This is important because the resize handler is |
| 249 |
# debounced. |
| 250 |
expect(code_block).to_have_attribute("tabindex", "0") |
| 251 |
|
| 252 |
|
| 253 |
@pytest.mark.a11y |
| 254 |
def test_notebook_output_tab_stop(page: Page, url_base: str) -> None: |
| 255 |
"""Notebook outputs that have scrollable content should be tab stops.""" |
| 256 |
page.goto(urljoin(url_base, "/examples/pydata.html")) |
| 257 |
|
| 258 |
# A "plain" notebook output |
| 259 |
nb_output = page.locator("css=#Pandas > .nboutput > .output_area") |
| 260 |
|
| 261 |
# At the default viewport size (1280 x 720) the Pandas data table has |
| 262 |
# overflow... |
| 263 |
assert nb_output.evaluate("el => el.scrollWidth > el.clientWidth") is True |
| 264 |
|
| 265 |
# ...and so our js code on the page should make it keyboard-focusable |
| 266 |
# (tabIndex = 0) |
| 267 |
assert nb_output.evaluate("el => el.tabIndex") == 0 |
| 268 |
|
| 269 |
|
| 270 |
@pytest.mark.a11y |
| 271 |
def test_notebook_ipywidget_output_tab_stop(page: Page, url_base: str) -> None: |
| 272 |
"""Notebook ipywidget outputs that have scrollable content should be tab stops.""" |
| 273 |
page.goto(urljoin(url_base, "/examples/pydata.html")) |
| 274 |
|
| 275 |
# An ipywidget notebook output |
| 276 |
ipywidget = page.locator("css=.jp-RenderedHTMLCommon").first |
| 277 |
|
| 278 |
# As soon as the ipywidget is attached to the page it should trigger the |
| 279 |
# mutation observer |
| 280 |
ipywidget.wait_for(state="attached") |
| 281 |
|
| 282 |
# At the default viewport size (1280 x 720) the data table inside the |
| 283 |
# ipywidget has overflow... |
| 284 |
assert ipywidget.evaluate("el => el.scrollWidth > el.clientWidth") is True |
| 285 |
|
| 286 |
# ... and so our js code on the page should make it keyboard-focusable |
| 287 |
# (tabIndex=0). |
| 288 |
# |
| 289 |
# Note: expect() will wait until the expect condition is true (up to the |
| 290 |
# test timeout limit). This is important because the mutation callback that |
| 291 |
# sets tabIndex=0 is debounced. |
| 292 |
expect(ipywidget).to_have_attribute("tabindex", "0") |
| 293 |
|
| 294 |
|
| 295 |
@pytest.mark.a11y |
| 296 |
def test_search_as_you_type(page: Page, url_base: str) -> None: |
| 297 |
"""Search-as-you-type feature should support keyboard navigation. |
| 298 |
|
| 299 |
When the search-as-you-type (inline search results) feature is enabled, |
| 300 |
pressing Tab after entering a search query should focus the first inline |
| 301 |
search result. |
| 302 |
""" |
| 303 |
page.set_viewport_size({"width": 1440, "height": 720}) |
| 304 |
page.goto(urljoin(url_base, "/examples/kitchen-sink/blocks.html")) |
| 305 |
# Click the search textbox. |
| 306 |
searchbox = page.locator("css=.navbar-header-items .search-button__default-text") |
| 307 |
searchbox.click() |
| 308 |
# Type a search query. |
| 309 |
query_input = page.locator("css=#pst-search-dialog input[type=search]") |
| 310 |
expect(query_input).to_be_visible() |
| 311 |
query_input.type("test") |
| 312 |
page.wait_for_timeout(301) # Search execution is debounced for 300 ms. |
| 313 |
search_results = page.locator("css=#search-results") |
| 314 |
expect(search_results).to_be_visible() |
| 315 |
# Navigate with the keyboard. |
| 316 |
query_input.press("Tab") |
| 317 |
# Make sure that the first inline search result is focused. |
| 318 |
actual_focused_content = page.evaluate("document.activeElement.textContent") |
| 319 |
first_result_selector = "#search-results .search li:first-child a" |
| 320 |
expected_focused_content = page.evaluate( |
| 321 |
f"document.querySelector('{first_result_selector}').textContent" |
| 322 |
) |
| 323 |
assert actual_focused_content == expected_focused_content |