|
| 1 | +"""Using Axe-core, scan the Kitchen Sink pages for accessibility violations.""" |
| 2 | + |
| 3 | +import re |
| 4 | +import time |
| 5 | +from http.client import HTTPConnection |
| 6 | +from pathlib import Path |
| 7 | +from subprocess import PIPE, Popen |
| 8 | +from urllib.parse import urljoin |
| 9 | + |
| 10 | +import pytest |
| 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 | +# Important note: automated accessibility scans can only find a fraction of |
| 17 | +# potential accessibility issues. |
| 18 | +# |
| 19 | +# This test file scans pages from the Kitchen Sink examples with a JavaScript |
| 20 | +# library called Axe-core, which checks the page for accessibility violations, |
| 21 | +# such as places on the page with poor color contrast that would be hard for |
| 22 | +# people with low vision to see. |
| 23 | +# |
| 24 | +# Just because a page passes the scan with no accessibility violations does |
| 25 | +# *not* mean that it will be generally usable by a broad range of disabled |
| 26 | +# people. It just means that page is free of common testable accessibility |
| 27 | +# pitfalls. |
| 28 | + |
| 29 | +path_repo = Path(__file__).parent.parent |
| 30 | +path_docs_build = path_repo / "docs" / "_build" / "html" |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module") |
| 34 | +def url_base(path_repo=path_repo, path_docs_build=path_docs_build): |
| 35 | + """Start local server on built docs and return the localhost URL as the base URL.""" |
| 36 | + # Use a port that is not commonly used during development or else you will |
| 37 | + # force the developer to stop running their dev server in order to run the |
| 38 | + # tests. |
| 39 | + port = "8213" |
| 40 | + host = "localhost" |
| 41 | + url = f"http://{host}:{port}" |
| 42 | + |
| 43 | + # Try starting the server |
| 44 | + process = Popen( |
| 45 | + ["python", "-m", "http.server", port, "--directory", path_docs_build], |
| 46 | + stdout=PIPE, |
| 47 | + ) |
| 48 | + |
| 49 | + # Try connecting to the server |
| 50 | + retries = 5 |
| 51 | + while retries > 0: |
| 52 | + conn = HTTPConnection(host, port) |
| 53 | + try: |
| 54 | + conn.request("HEAD", "/") |
| 55 | + response = conn.getresponse() |
| 56 | + if response is not None: |
| 57 | + yield url |
| 58 | + break |
| 59 | + except ConnectionRefusedError: |
| 60 | + time.sleep(1) |
| 61 | + retries -= 1 |
| 62 | + |
| 63 | + # If the code above never yields a URL, then we were never able to connect |
| 64 | + # to the server and retries == 0. |
| 65 | + if not retries: |
| 66 | + raise RuntimeError("Failed to start http server in 5 seconds") |
| 67 | + else: |
| 68 | + # Otherwise the server started and this fixture is done now and we clean |
| 69 | + # up by stopping the server. |
| 70 | + process.terminate() |
| 71 | + process.wait() |
| 72 | + |
| 73 | + |
| 74 | +def test_breadcrumb_expansion(page: Page, url_base: str) -> None: |
| 75 | + page.set_viewport_size({"width": 1440, "height": 720}) |
| 76 | + page.goto(urljoin(url_base, "community/topics/config.html")) |
| 77 | + expect(page.get_by_label("Breadcrumb").get_by_role("list")).to_contain_text("Update Sphinx configuration during the build") |
| 78 | + el = page.get_by_text("Update Sphinx configuration during the build").nth(1) |
| 79 | + assert el.evaluate("e => e.clientWidth === e.scrollWidth", el) == True |
| 80 | + expect(el).to_have_css("overflow-x", "hidden") |
| 81 | + expect(el).to_have_css("text-overflow", "ellipsis") |
| 82 | + page.set_viewport_size({"width": 20, "height": 720}) |
| 83 | + |
| 84 | + # There is no good way to check if text-overflow has been applied other |
| 85 | + # than to directly compare the rendered clientWidth to the scrollWidth |
| 86 | + # required to display the text. |
| 87 | + assert el.evaluate("e => e.clientWidth < e.scrollWidth", el) == True |
| 88 | + |
0 commit comments