Skip to content

Instructions from Jackson

Jackson Maxfield Brown edited this page Oct 27, 2020 · 1 revision

Getting Setup on axe-selenium

These are real quick-and-dirty setup steps. A better long-term solution would be to make an actual Python package for your project -- I recommend using my cookiecutter python package.

Dependencies

Geckodriver

Go to geckodriver releases and pick the one that matches your OS and architecture. In my case, I would pick geckodriver-v0.27.0-linux64.tar.gz.asc but for Windows you would pick between:

  • geckodriver-v0.27.0-win32.zip
  • geckodriver-v0.27.0-win64.zip

(I would assume if your machine is built / purchases within the last 8 ish years you probably have a 64 bit machine and thus should choose geckodriver-v0.27.0-win64.zip)

  • Download the file of your choosing
  • Unzip the contents
  • On Mac / Linux: add the folder where you unzipped the contents to, to your PATH. If you unzipped the contents to /home/maxfield/Downloads/ then add Downloads to your PATH like so: export PATH=$PATH:/home/maxfield/Downloads/
  • On Windows: I believe you don't need to do this

Environment

Please use some environment manager. pyenv, conda, whatever you are comfortable with just use something to maintain versions. (I use miniconda)

These were the commands I used to create my environment

conda create --name BITS-access-2020 python=3.8 -y
conda activate BITS-access-2020

Dependencies Installation

Copy paste the following to a requirements.txt file:

axe-selenium-python
ipython

While in your activated Python environment run pip install -r requirements.txt

Running the Script

In your activated Python environment run: ipython to launch an IPython terminal. Then, copy paste your corresponding OS commands into the terminal.

Windows

Replace the geckodriver path in the following script with the location of where your geckodriver executable can be found.

from selenium import webdriver
from axe_selenium_python import Axe
 
def test_site(url: str):
    # Add the path the geckodriver you downloaded earlier
    # The following is an example
    driver = webdriver.Firefox("C:\\Users\\Maxfield\\Downloads\\geckodriver.exe")
    driver.get(url)
    axe = Axe(driver)
    # Inject axe-core javascript into page.
    axe.inject()
    # Run axe accessibility checks.
    results = axe.run()
    # Write results to file
    axe.write_results(results, 'a11y.json')
    driver.close()
    # Assert no violations are found
    assert len(results["violations"]) == 0, axe.report(results["violations"])

Mac / Linux

from selenium import webdriver
from axe_selenium_python import Axe
 
def test_site(url: str):
    driver = webdriver.Firefox()
    driver.get(url)
    axe = Axe(driver)
    # Inject axe-core javascript into page.
    axe.inject()
    # Run axe accessibility checks.
    results = axe.run()
    # Write results to file
    axe.write_results(results, 'a11y.json')
    driver.close()
    # Assert no violations are found
    assert len(results["violations"]) == 0, axe.report(results["violations"])

Testing Sites

Great. In the same terminal that you just pasted the above option into, you can now simply run yours tests for each site:

test_site("http://natalieisawesome.com/")

This will spit out the Accessibilty errors into the terminal, and, write them out to a file called a11y.json in whatever directory you ran your terminal from.

Clone this wiki locally