generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_accessibility.py
65 lines (52 loc) · 2.1 KB
/
test_accessibility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pytest
from flask import url_for
import re
from playwright.sync_api import Page
from axe_core_python.sync_playwright import Axe
import json
import os
import shutil
ACCESSIBILITY_STANDARDS = ["wcag2a", "wcag2aa"]
def check_accessibility(page: Page):
"""
Inserts axe core python into a page at the yield step
to run accessibility based testing. Axe will run on
the page defined in the function.
"""
if page.title() != "localhost":
directory = "tests/functional_tests/accessibility_output"
if not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)
axe = Axe()
results = axe.run(page)
wcag_violations = []
for violation in results["violations"]:
if set(violation["tags"]) & set(ACCESSIBILITY_STANDARDS):
wcag_violations.append(violation)
if len(wcag_violations) == 0:
assert "No WCAG accessibility issues found"
else:
# Cleans the URL to remove any invalid characters and replace with _
invalid_filename_chars = r'[\/:*?"<>|]'
sanitized_title = re.sub(invalid_filename_chars, "_", page.title())
max_title_len = 30
file_name = f"axe_results_{sanitized_title[:max_title_len]}.json"
file_path = os.path.join(directory, file_name)
with open(file_path, "w") as file:
json.dump(wcag_violations, file, indent=4)
@pytest.mark.usefixtures("live_server")
def test_all_page_accessibility(app, page: Page):
ignored_routes = ["static", "/", "main.status", "main.set_locale"]
shutil.rmtree("tests/functional_tests/accessibility_output", ignore_errors=True)
routes = app.view_functions
for route in routes:
if route not in ignored_routes:
full_url = url_for(route, _external=True)
page.goto(full_url)
check_accessibility(page)
def test_accessibility_folder():
path = "tests/functional_tests/accessibility_output"
if not any(os.scandir(path)):
assert True
else:
assert not "WCAG accessibility issues found"