Skip to content

Commit

Permalink
tests: detect issues with our HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Nov 27, 2024
1 parent d787dcc commit d9f9617
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions tests/utils/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit d9f9617

Please sign in to comment.