An open API service indexing awesome lists of open source software.

https://github.com/guisalmeida/a11y-analyzer

A tool to help you make accessibility tests.
https://github.com/guisalmeida/a11y-analyzer

a11y-testing axe-core cypress cypress-axe e2e-testing wcag

Last synced: 11 months ago
JSON representation

A tool to help you make accessibility tests.

Awesome Lists containing this project

README

          

# A11Y Analyzer

[![npm](https://img.shields.io/npm/v/a11y-analyzer.svg)](https://www.npmjs.com/package/a11y-analyzer)
[![Cypress Tests](https://github.com/guisalmeida/a11y-analyzer/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/guisalmeida/a11y-analyzer/actions/workflows/main.yml)

Test accessibility with in [Cypress](https://cypress.io).

## Installation

1. **Install `a11y-analyzer` from npm:**

```sh
npm install --save-dev a11y-analyzer
```

2. **Install peer dependencies:**

```sh
npm install --save-dev axe-core cypress cypress-axe cypress-terminal-report http-server start-server-and-test
```
3. **Configure Cypress.**

- Run the command below:

```js
npx cypress open
```

Then cypress will opens a window dialogue like this one:
![Welcome cypress image](./public/assets/images/cypress-config1.png)
Selecting E2E Testing it will create a configuration environment for us.
![Files for configuration](./public/assets/images/cypress-config2.png)
Just hit the continue button and select a browser to start testing.
Afterwards you can create your first spec with Cypress in this interface or you can create your specs mannualy inside the folder `cypress/e2e`.
![Create spec](./public/assets/images/cypress-config3.png)
![Create spec](./public/assets/images/cypress-config4.png)
After hit create and run your test inside the interface of tests of the Cypress.

4. **Include the commands.**

- Update `cypress/support/e2e.js` file to include the cypress-axe commands by adding:

```js
import 'a11y-analyzer'
```

5. **Include the configuration.**

- Update `cypress.config.js` file to include the a11y-analyzer commands by adding:

```js
const { defineConfig } = require("cypress");

module.exports = defineConfig({
env: {
"hideElements": true // To hide elements like XHR requests
},
e2e: {
baseUrl: 'http://localhost:8080', // Choose you localhost
screenshotOnRunFailure: false, // Don't take screenshots
video: false, // Don't record videos
setupNodeEvents(on, config) {
require('cypress-terminal-report/src/installLogsPrinter')(on);
},
},
});
```

> NOTE: To use require import, make sure there ins't a `"type": "module",` inside your `package.json` and if using typescript and es6 imports ensure `esModuleInterop` is enabled.

## Commands

### cy.analyseA11y

This will inject the `axe-core` runtime into the page under test.

```js
it('Test', () => {
cy.analyseA11y('http://localhost:8080')
})
```

### Examples

#### Basic usage

```js
describe('Example tests', () => {
it('Should log any accessibility failures', () => {
cy.analyseA11y('https://example.cypress.io');
})

it('Should execute ONLY specific elements on the page', () => {
cy.analyseA11y('https://example.cypress.io', '.container', null);
})

it('Should exclude specific elements on the page', () => {
cy.analyseA11y('https://example.cypress.io', { exclude: ['.banner'] }, null);
})

it('Should ONLY include rules with serious and critical impacts', () => {
cy.analyseA11y('https://example.cypress.io', null, {
includedImpacts: ['critical', 'serious']
});
})

it('Should exclude specific accessibility rules', () => {
cy.analyseA11y('https://example.cypress.io', null, {
rules: {
'color-contrast': { enabled: false }
}
});
})

it('Should ONLY include rules with these levels of conformance', () => {
cy.analyseA11y('https://example.cypress.io', null, {
runOnly: {
type: 'tag',
values: ['wcag2a', 'wcag2aa']
}
}
);
})
})
```

## Standard Output

When accessibility violations are detected, your test will fail and an entry titled "A11Y ERROR!" will be added to the command log for each type of violation found (they will be above the failed assertion). Clicking on those will can see where they are on screen and more like impact and a link to a documentation from Axe-core.

![Cypress and DevTools output for passing and failing axe-core audits](./public/assets/images/cypress-config5.png)

A similar output is present on terminal and on CI logs.
![Terminal](./public/assets/images/terminal.png)

## Authors

The project was created by [Guilherme Almeida](https://guisalmeida.com/).

## Contributors

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## License

MIT License, see the included [License.md](License.md) file.