From 2c6e7d9ad43f8f9d7d38e729e8f64ac9bca66e5d 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 f64382c1fcc..cff98dc01b8 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 f0aa42c9881..9024f3a7184 100644 --- a/tests/utils/test.py +++ b/tests/utils/test.py @@ -126,6 +126,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