Skip to content

Commit 76adb3a

Browse files
mchamwmcode
authored andcommitted
wip
1 parent 58154f2 commit 76adb3a

File tree

3 files changed

+335
-18
lines changed

3 files changed

+335
-18
lines changed

β€ŽREADME.bk.mdβ€Ž

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
> ❗️forked from [cypress-axe](https://github.com/component-driven/cypress-axe)
2+
>
3+
> πŸ‘€ watch [RFC 75](https://github.com/component-driven/cypress-axe/issues/75)
4+
>
5+
> TODO: update example
6+
>
7+
# cypress-axe
8+
9+
[![npm](https://img.shields.io/npm/v/cypress-axe.svg)](https://www.npmjs.com/package/cypress-axe) [![Node.js CI status](https://github.com/component-driven/cypress-axe/workflows/Node.js%20CI/badge.svg)](https://github.com/component-driven/cypress-axe/actions)
10+
11+
Test accessibility with [axe-core](https://github.com/dequelabs/axe-core) in [Cypress](https://cypress.io).
12+
13+
## Installation
14+
15+
1. **Install `cypress-axe` from npm:**
16+
17+
```sh
18+
npm install --save-dev cypress-axe
19+
```
20+
21+
2. **Install peer dependencies:**
22+
23+
```sh
24+
npm install --save-dev cypress axe-core
25+
```
26+
27+
3. **Include the commands.** Update `cypress/support/index.js` file to include the cypress-axe commands by adding:
28+
29+
```js
30+
import 'cypress-axe'
31+
```
32+
33+
4. **Add a task to log the messages to the terminal** when Cypress executes the spec files. [Example - configuring log task](https://docs.cypress.io/api/commands/task.html#Usage).
34+
35+
### TypeScript
36+
37+
If you’re using TypeScript, add cypress-axe types to your Cypress’ `tsconfig.json` file:
38+
39+
```json
40+
{
41+
"compilerOptions": {
42+
"baseUrl": "./",
43+
"target": "es5",
44+
"lib": ["esnext", "dom"],
45+
"types": ["cypress", "cypress-axe"]
46+
},
47+
"include": ["."]
48+
}
49+
```
50+
51+
## Commands
52+
53+
### cy.injectAxe
54+
55+
This will inject the `axe-core` runtime into the page under test. You must run this **after** a call to `cy.visit()` and before you run the `checkA11y` command.
56+
57+
You run this command with `cy.injectAxe()` either in your test, or in a `beforeEach`, as long as the `visit` comes first.
58+
59+
```js
60+
beforeEach(() => {
61+
cy.visit('http://localhost:9000')
62+
cy.injectAxe()
63+
})
64+
```
65+
66+
### cy.configureAxe
67+
68+
#### Purpose
69+
70+
To configure the format of the data used by aXe. This can be used to add new rules, which must be registered with the library to execute.
71+
72+
#### Description
73+
74+
User specifies the format of the JSON structure passed to the callback of axe.run
75+
76+
[Link - aXe Docs: axe.configure](https://www.deque.com/axe/documentation/api-documentation/#api-name-axeconfigure)
77+
78+
```js
79+
it('Has no detectable a11y violations on load (custom configuration)', () => {
80+
// Configure aXe and test the page at initial load
81+
cy.configureAxe({
82+
branding: {
83+
brand: String,
84+
application: String
85+
},
86+
reporter: 'option',
87+
checks: [Object],
88+
rules: [Object],
89+
locale: Object
90+
})
91+
cy.checkA11y()
92+
})
93+
```
94+
95+
### cy.checkA11y
96+
97+
This will run axe against the document at the point in which it is called. This means you can call this after interacting with your page and uncover accessibility issues introduced as a result of rendering in response to user actions.
98+
99+
#### Parameters on cy.checkA11y (axe.run)
100+
101+
##### context (optional)
102+
103+
Defines the scope of the analysis - the part of the DOM that you would like to analyze. This will typically be the document or a specific selector such as class name, ID, selector, etc.
104+
105+
##### options (optional)
106+
107+
Set of options passed into rules or checks, temporarily modifying them. This contrasts with axe.configure, which is more permanent.
108+
109+
The keys consist of [those accepted by `axe.run`'s options argument](https://www.deque.com/axe/documentation/api-documentation/#parameters-axerun) as well as a custom `includedImpacts` key.
110+
111+
The `includedImpacts` key is an array of strings that map to `impact` levels in violations. Specifying this array will only include violations where the impact matches one of the included values. Possible impact values are "minor", "moderate", "serious", or "critical".
112+
113+
Filtering based on impact in combination with the `skipFailures` argument allows you to introduce `cypress-axe` into tests for a legacy application without failing in CI before you have an opportunity to address accessibility issues. Ideally, you would steadily move towards stricter testing as you address issues.
114+
115+
##### violationCallback (optional)
116+
117+
Allows you to define a callback that receives the violations for custom side-effects, such as adding custom output to the terminal.
118+
119+
**NOTE:** _This respects the `includedImpacts` filter and will only execute with violations that are included._
120+
121+
##### skipFailures (optional, defaults to false)
122+
123+
Disables assertions based on violations and only logs violations to the console output. This enabled you to see violations while allowing your tests to pass. This should be used as a temporary measure while you address accessibility violations.
124+
125+
Reference : https://github.com/component-driven/cypress-axe/issues/17
126+
127+
### Examples
128+
129+
#### Basic usage
130+
131+
```js
132+
// Basic usage
133+
it('Has no detectable a11y violations on load', () => {
134+
// Test the page at initial load
135+
cy.checkA11y()
136+
})
137+
138+
// Applying a context and run parameters
139+
it('Has no detectable a11y violations on load (with custom parameters)', () => {
140+
// Test the page at initial load (with context and options)
141+
cy.checkA11y('.example-class', {
142+
runOnly: {
143+
type: 'tag',
144+
values: ['wcag2a']
145+
}
146+
})
147+
})
148+
149+
it('Has no detectable a11y violations on load (filtering to only include critical impact violations)', () => {
150+
// Test on initial load, only report and assert for critical impact items
151+
cy.checkA11y(null, {
152+
includedImpacts: ['critical']
153+
})
154+
})
155+
156+
// Basic usage after interacting with the page
157+
it('Has no a11y violations after button click', () => {
158+
// Interact with the page, then check for a11y issues
159+
cy.get('button').click()
160+
cy.checkA11y()
161+
})
162+
163+
it('Only logs a11y violations while allowing the test to pass', () => {
164+
// Do not fail the test when there are accessibility failures
165+
cy.checkA11y(null, null, null, {skipFailures: true});
166+
})
167+
168+
169+
```
170+
171+
#### Using the violationCallback argument
172+
173+
The violation callback parameter accepts a function and allows you to add custom behavior when violations are found.
174+
175+
This example adds custom logging to the terminal running Cypress, using `cy.task` and the `violationCallback` argument for `cy.checkA11y`
176+
177+
##### In Cypress plugins file
178+
179+
This registers a `log` task as seen in the [Cypress docs for cy.task](https://docs.cypress.io/api/commands/task.html#Usage) as well as a `table` task for sending tabular data to the terminal.
180+
181+
```js
182+
module.exports = (on, config) => {
183+
on('task', {
184+
log(message) {
185+
console.log(message)
186+
187+
return null
188+
},
189+
table(message) {
190+
console.table(message)
191+
192+
return null
193+
}
194+
})
195+
}
196+
```
197+
198+
#### In your spec file
199+
200+
Then we create a function that uses our tasks and pass it as the `validationCallback` argument to `cy.checkA11y`
201+
202+
```js
203+
// Define at the top of the spec file or just import it
204+
function terminalLog(violations) {
205+
cy.task(
206+
'log',
207+
`${violations.length} accessibility violation${
208+
violations.length === 1 ? '' : 's'
209+
} ${violations.length === 1 ? 'was' : 'were'} detected`
210+
)
211+
// pluck specific keys to keep the table readable
212+
const violationData = violations.map(
213+
({ id, impact, description, nodes }) => ({
214+
id,
215+
impact,
216+
description,
217+
nodes: nodes.length
218+
})
219+
)
220+
221+
cy.task('table', violationData)
222+
}
223+
224+
// Then in your test...
225+
it('Logs violations to the terminal', () => {
226+
cy.checkA11y(null, null, terminalLog)
227+
})
228+
```
229+
230+
This custom logging behavior results in terminal output like this:
231+
232+
![Custom terminal logging with cy.task and validationCallback](terminal_output_example.png)
233+
234+
## Standard Output
235+
236+
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 reveal more specifics about the error in the DevTools console.
237+
238+
![Cypress and DevTools output for passing and failing axe-core audits](cmd_log.png)
239+
240+
## Authors
241+
242+
The project was created by [Andy Van Slaars](https://vanslaars.io/), and maintained by [Artem Sapegin](https://sapegin.me).
243+
244+
## Contributors
245+
246+
Thanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)):
247+
248+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
249+
<!-- prettier-ignore-start -->
250+
<!-- markdownlint-disable -->
251+
<table>
252+
<tr>
253+
<td align="center"><a href="https://samcus.co"><img src="https://avatars2.githubusercontent.com/u/14907938?v=4" width="100px;" alt=""/><br /><sub><b>Samuel Custer</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=samcus" title="Code">πŸ’»</a> <a href="https://github.com/component-driven/cypress-axe/commits?author=samcus" title="Documentation">πŸ“–</a></td>
254+
<td align="center"><a href="https://github.com/miketoth"><img src="https://avatars2.githubusercontent.com/u/2512676?v=4" width="100px;" alt=""/><br /><sub><b>Michael Toth</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=miketoth" title="Code">πŸ’»</a></td>
255+
<td align="center"><a href="https://github.com/NicholasBoll"><img src="https://avatars2.githubusercontent.com/u/338257?v=4" width="100px;" alt=""/><br /><sub><b>Nicholas Boll</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=NicholasBoll" title="Code">πŸ’»</a></td>
256+
<td align="center"><a href="https://github.com/michaeljacobdavis"><img src="https://avatars2.githubusercontent.com/u/916905?v=4" width="100px;" alt=""/><br /><sub><b>Mike Davis</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=michaeljacobdavis" title="Code">πŸ’»</a></td>
257+
<td align="center"><a href="https://github.com/chit786"><img src="https://avatars2.githubusercontent.com/u/18376182?v=4" width="100px;" alt=""/><br /><sub><b>chit786</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=chit786" title="Code">πŸ’»</a> <a href="https://github.com/component-driven/cypress-axe/commits?author=chit786" title="Documentation">πŸ“–</a></td>
258+
<td align="center"><a href="https://github.com/acourdavault"><img src="https://avatars0.githubusercontent.com/u/27222128?v=4" width="100px;" alt=""/><br /><sub><b>Adrien courdavault</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=acourdavault" title="Code">πŸ’»</a></td>
259+
<td align="center"><a href="http://brett-zamir.me"><img src="https://avatars3.githubusercontent.com/u/20234?v=4" width="100px;" alt=""/><br /><sub><b>Brett Zamir</b></sub></a><br /><a href="https://github.com/component-driven/cypress-axe/commits?author=brettz9" title="Code">πŸ’»</a></td>
260+
</tr>
261+
</table>
262+
263+
<!-- markdownlint-enable -->
264+
<!-- prettier-ignore-end -->
265+
266+
<!-- ALL-CONTRIBUTORS-LIST:END -->
267+
268+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
269+
270+
## License
271+
272+
MIT License, see the included [License.md](License.md) file.

0 commit comments

Comments
Β (0)