Webperf-se / webperf_core

webperf-core is an open-source testing suite tailored to help you improve your digital presence in areas like web performance, security and accessibility to email best practice using many small improvements.
https://webperf.se/articles/webperf-core/
MIT License
17 stars 25 forks source link

Add support for Pa11y #103

Closed marcusosterberg closed 2 years ago

marcusosterberg commented 2 years ago

Add Pa11y as one of the tests for accessibility. The code below is using Pa11y/Pa11y-ci and worked with Webperf Core 2.x

# -*- coding: utf-8 -*-
import subprocess
import json
from models import Rating

def run_test(langCode, url):
    """
    """

    # pa11y --reporter json https://webperf.se
    # ~/node_modules/pa11y/bin/pa11y.js https://vgregion.se
    print(langCode, url)

    result = subprocess.run(['pa11y', '--reporter', 'json', url[0][1]], stdout=subprocess.PIPE)

    mod_results = result.stdout.decode("utf-8")
    result_list = json.loads(mod_results)
    num_errors = len(result_list)
    return_dict = {}

    points = 0
    review = ''

    if  num_errors == 0:
        points = 5
        review = '* Webbplatsen har inga uppenbara fel kring tillgänglighet!\n'
    elif num_errors == 1:
        points = 4
        review = '* Webbplatsen kan bli mer tillgänglig, men är helt ok.\n'
    elif num_errors > 8:
        points = 1
        review = '* Väldigt dålig tillgänglighet!\n'
    elif num_errors >= 4:
        points = 2
        review = '* Dålig tillgänglighet.\n'
    elif num_errors >= 2:
        points = 3
        review = '* Genomsnittlig tillgänglighet men kan bli bättre.\n'

    review += '* Antal tillgänglighetsproblem: {} st\n'.format(num_errors)
    return_dict['antal_problem'] = num_errors

    if num_errors > 0:
        review += '\nProblem:\n'

    i = 1
    old_error = ''

    for error in result_list:
        err_mess = error.get('message').replace('This', 'A')
        if err_mess != old_error:
            old_error = err_mess
            review += '* {0}\n'.format(err_mess)
            key = error.get('code') #'{0}-{1}'.format(error.get('code'), i)
            return_dict.update( { key : err_mess } )

            i += 1

        if i > 10:
            review += '* Info: För många unika problem för att lista alla\n'
            break

    rating = Rating(_)
    rating.set_overall(points, review)

    print(points, review, return_dict)
    return (rating, return_dict)

"""
If file is executed on itself then call a definition, mostly for testing purposes
"""
if __name__ == '__main__':
    print(run_test('sv', 'https://webperf.se'))
7h3Rabbit commented 2 years ago

Solved and closed :)