|
| 1 | +import json |
| 2 | + |
| 3 | +from axe_selenium_python import Axe |
| 4 | +from selenium import webdriver |
| 5 | +from selenium.webdriver.common.by import By |
| 6 | +from selenium.webdriver.firefox.options import Options |
| 7 | +from selenium.webdriver.firefox.service import Service |
| 8 | +from selenium.webdriver.support import expected_conditions as EC |
| 9 | +from selenium.webdriver.support.ui import WebDriverWait |
| 10 | + |
| 11 | +TAGS = ["wcag2a", "wcag2aa", "wcag21aa"] |
| 12 | + |
| 13 | +ANON_URLS_TO_SCAN = ("http://host.docker.internal:3000",) |
| 14 | + |
| 15 | +AUTHED_URLS_TO_SCAN = ( |
| 16 | + "http://host.docker.internal:3000/sites", |
| 17 | + "http://host.docker.internal:3000/sites/1/documents" |
| 18 | + "http://host.docker.internal:3000/sites/1/insights", |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def scan_urls(): |
| 23 | + # Automatically manage geckodriver |
| 24 | + options = Options() |
| 25 | + options.add_argument("--headless") |
| 26 | + options.add_argument("--no-sandbox") |
| 27 | + options.add_argument("--disable-dev-shm-usage") |
| 28 | + |
| 29 | + service = Service("/usr/local/bin/geckodriver") |
| 30 | + driver = webdriver.Firefox(service=service, options=options) |
| 31 | + |
| 32 | + all_results = { |
| 33 | + "total_violations": 0, |
| 34 | + "anon_urls": {}, |
| 35 | + "authed_urls": {}, |
| 36 | + } |
| 37 | + |
| 38 | + for url in ANON_URLS_TO_SCAN: |
| 39 | + driver.get(url) |
| 40 | + driver.implicitly_wait(5) |
| 41 | + results = get_axe_results(driver) |
| 42 | + all_results["total_violations"] += len(results["violations"]) |
| 43 | + all_results["anon_urls"][url] = results |
| 44 | + |
| 45 | + # Log into the app. |
| 46 | + wait = WebDriverWait(driver, 10) |
| 47 | + email_field = wait.until( |
| 48 | + EC.presence_of_element_located((By.CSS_SELECTOR, "#email_address")) |
| 49 | + ) |
| 50 | + password_field = driver.find_element(By.CSS_SELECTOR, "#password") |
| 51 | + email_field. send_keys( "[email protected]") |
| 52 | + password_field.send_keys("password") |
| 53 | + submit_button = driver.find_element(By.CSS_SELECTOR, "#submit-session-form") |
| 54 | + submit_button.click() |
| 55 | + wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#add-site-modal"))) |
| 56 | + |
| 57 | + for url in AUTHED_URLS_TO_SCAN: |
| 58 | + driver.get(url) |
| 59 | + driver.implicitly_wait(5) |
| 60 | + results = get_axe_results(driver) |
| 61 | + all_results["total_violations"] += len(results["violations"]) |
| 62 | + all_results["authed_urls"][url] = results |
| 63 | + |
| 64 | + driver.quit() |
| 65 | + |
| 66 | + print(json.dumps(all_results, indent=2)) |
| 67 | + |
| 68 | + |
| 69 | +def get_axe_results(driver): |
| 70 | + axe = Axe(driver) |
| 71 | + axe.inject() |
| 72 | + return axe.run({"runOnly": {"type": "tag", "values": TAGS}}) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + scan_urls() |
0 commit comments