|
1 | | -import json |
2 | | - |
3 | 1 | from axe_playwright_python.base import AxeResults |
4 | | -from django.conf import settings |
5 | | -from playwright.sync_api import Page |
| 2 | +from axe_playwright_python.sync_playwright import Axe |
6 | 3 |
|
7 | 4 | AXE_VIOLATIONS_EXCLUDE_LIST = [ |
8 | 5 | "region", # 'Some page content is not contained by landmarks' https://github.com/alphagov/govuk-frontend/issues/1604 |
9 | 6 | "aria-allowed-attr", # 'ARIA attribute is not allowed: aria-expanded="false"' https://github.com/alphagov/govuk-frontend/issues/979 |
10 | 7 | ] |
11 | 8 |
|
12 | 9 |
|
13 | | -class AxeAdapter: |
14 | | - def __init__( |
15 | | - self, |
16 | | - page: Page, |
17 | | - script_path=settings.BASE_DIR.parent |
18 | | - / "node_modules" |
19 | | - / "axe-core" |
20 | | - / "axe.min.js", |
21 | | - options=None, |
22 | | - ): |
23 | | - self.script_path = script_path |
24 | | - self.options = options or { |
| 10 | +class AxeAdapter(Axe): |
| 11 | + def __init__(self): |
| 12 | + self.default_options = { |
25 | 13 | "rules": {id: {"enabled": False} for id in AXE_VIOLATIONS_EXCLUDE_LIST} |
26 | 14 | } |
27 | | - self.page = page |
28 | | - self._install(page) |
29 | | - |
30 | | - def _install(self, page: Page): |
31 | | - """ |
32 | | - Add the axe script to a playwright Page. |
33 | | - The script will be re-executed any time the page or it's frames are navigated. |
34 | | - """ |
35 | | - page.add_init_script(path=self.script_path) |
| 15 | + super().__init__() |
36 | 16 |
|
37 | | - def run(self) -> AxeResults: |
38 | | - """ |
39 | | - Run axe on the whole document |
40 | | - """ |
41 | | - options = json.dumps(self.options) |
42 | | - response = self.page.evaluate(rf"axe.run({options})") |
43 | | - return AxeResults(response) |
| 17 | + def run(self, page, context=None, options=None) -> AxeResults: |
| 18 | + return super().run( |
| 19 | + page=page, context=context, options=options or self.default_options |
| 20 | + ) |
0 commit comments