From d9f961787359761f0c0875399c3e8eaa7185accf Mon Sep 17 00:00:00 2001 From: Xavier Fernandez Date: Wed, 6 Nov 2024 23:05:31 +0100 Subject: [PATCH] tests: detect issues with our HTML --- pyproject.toml | 1 + tests/utils/test.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index f64382c1fc..cff98dc01b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,7 @@ python_files = ["tests*.py", "test_*.py"] filterwarnings = [ "error", "ignore:.*Use timezone-aware objects to represent datetimes in UTC.*:DeprecationWarning:(botocore|django_datadog_logger)", + "ignore:.*The 'strip_cdata' option of HTMLParser\\(\\) has never done anything and will eventually be removed.", ] addopts = [ "--reuse-db", diff --git a/tests/utils/test.py b/tests/utils/test.py index 373f492727..d18917e790 100644 --- a/tests/utils/test.py +++ b/tests/utils/test.py @@ -136,6 +136,26 @@ def request(self, **request): # Detect rendering issues in templates assert "{{" not in content assert "{%" not in content + + if request.get("HTTP_HX_REQUEST") != "true": # Do not check htmx requests that contain partial documents + # Check HTML consistency (use lxml parser for speed) + soup = BeautifulSoup(content, "lxml") + + tag_with_ids = soup.find_all(id=True) + ids = {tag.attrs["id"] for tag in tag_with_ids} + # Check for for duplicate ids + assert len(ids) == len(tag_with_ids) + + for tag in soup.find_all(attrs={"aria-labelledby": True}): + assert tag.attrs["aria-labelledby"] in ids + + for tag in soup.find_all(attrs={"aria-controls": True}): + assert tag.attrs["aria-controls"] in ids + + for tag in soup.find_all(attrs={"data-bs-target": True}): + if tag.attrs["data-bs-target"][0] == "#": + assert tag.attrs["data-bs-target"][1:] in ids + return response