Repository navigation
Expand file tree
/
Copy pathtest_axe.py
More file actions
executable file
·92 lines (67 loc) · 2.43 KB
/
Copy pathtest_axe.py
File metadata and controls
executable file
·92 lines (67 loc) · 2.43 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import json
from os import getcwd, getenv, path
import pytest
from selenium import webdriver
from ..axe import Axe
_DEFAULT_TEST_FILE = path.join(path.dirname(__file__), "test_page.html")
@pytest.fixture
def firefox_driver():
driver = webdriver.Firefox()
yield driver
driver.close()
@pytest.fixture
def chrome_driver():
opts = webdriver.ChromeOptions()
opts.headless = True
opts.add_argument("--no-sandbox")
driver_path = getenv("CHROMEDRIVER_PATH")
driver = (
webdriver.Chrome(options=opts, executable_path=driver_path)
if driver_path
else webdriver.Chrome(options=opts)
)
yield driver
driver.close()
@pytest.mark.nondestructive
def test_run_axe_sample_page_firefox(firefox_driver):
"""Run axe against sample page and verify JSON output is as expected."""
data = _perform_axe_run(firefox_driver)
assert len(data["inapplicable"]) == 60
assert len(data["incomplete"]) == 0
assert len(data["passes"]) == 7
assert len(data["violations"]) == 8
@pytest.mark.nondestructive
def test_run_axe_sample_page_chrome(chrome_driver):
"""Run axe against sample page and verify JSON output is as expected."""
data = _perform_axe_run(chrome_driver)
assert len(data["inapplicable"]) == 60
assert len(data["incomplete"]) == 0
assert len(data["passes"]) == 7
assert len(data["violations"]) == 8
def _perform_axe_run(driver):
driver.get("file://" + _DEFAULT_TEST_FILE)
axe = Axe(driver)
axe.inject()
data = axe.run()
return data
def test_write_results_to_file(tmpdir, mocker):
axe = Axe(mocker.MagicMock())
data = {"testKey": "testValue"}
filename = path.join(str(tmpdir), "results.json")
axe.write_results(data, filename)
with open(filename) as f:
actual_file_contents = json.loads(f.read())
assert data == actual_file_contents
def test_write_results_without_filepath(mocker):
axe = Axe(mocker.MagicMock())
data = {"testKey": "testValue"}
cwd = getcwd()
filename = path.join(cwd, "results.json")
axe.write_results(data, filename)
with open(filename) as f:
actual_file_contents = json.loads(f.read())
assert data == actual_file_contents
assert path.dirname(filename) == cwd