You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-36Lines changed: 33 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,19 +8,19 @@ Test accessibility with [axe-core](https://github.com/dequelabs/axe-core) in [Cy
8
8
>
9
9
> **Reasons**: to upgrade dependencies (i.e. `Cypress ^7` & `axe-core ^4`) and try out some of the suggesions in [RFC 75](https://github.com/component-driven/cypress-axe/issues/75) 👀
10
10
11
-
12
11
1.[Installation and Setup](#Installation-and-Setup)
13
-
-[Typescript](#TypeScript)
12
+
-[Typescript](#TypeScript)
14
13
2.[Examples](#Examples)
15
-
-[Simple](#Simple)
16
-
-[Customised](#Customised)
14
+
-[Simple](#Simple)
15
+
-[Customised](#Customised)
17
16
3.[Commands](#Commands)
18
-
-[cy.injectAxe](#cyinjectAxe)
19
-
-[cy.checkA11y](#cycheckA11y)
20
-
-[cy.configureCypressAxe](#cyconfigureCypressAxe)
21
-
-[cy.configureAxe](#cyconfigureAxe)
17
+
-[cy.injectAxe](#cyinjectAxe)
18
+
-[cy.checkA11y](#cycheckA11y)
19
+
-[cy.configureCypressAxe](#cyconfigureCypressAxe)
20
+
-[cy.configureAxe](#cyconfigureAxe)
22
21
23
22
# Installation and Setup
23
+
24
24
1.**Install** required packages. Assuming you have Cypress installed already, you will only need to:
25
25
26
26
```sh
@@ -34,35 +34,35 @@ import 'cypress-axe-core'
34
34
```
35
35
36
36
3.**Enable** results logging by defining cy.tasks in `cypress/plugins/index.js` file:
37
+
37
38
```js
38
39
module.exports= (on, config) => {
39
-
on('task', {
40
-
log(message) {
41
-
console.log(message);
42
-
returnnull;
43
-
},
44
-
45
-
table(message) {
46
-
console.table(message);
47
-
returnnull;
48
-
},
49
-
});
50
-
};
40
+
on('task', {
41
+
log(message) {
42
+
console.log(message)
43
+
returnnull
44
+
},
45
+
46
+
table(message) {
47
+
console.table(message)
48
+
returnnull
49
+
}
50
+
})
51
+
}
51
52
```
53
+
52
54
> **NOTE**: You can control how results are displayed via [the `violationsCallback` config option](#cyconfigureCypressAxe)
53
55
54
56
After following the steps above (_and defining cy.tasks_), violations will be displayed as follows:
If you’re using TypeScript, add `cypress-axe-core` types to your Cypress’s `tsconfig.json` file:
67
67
68
68
```json
@@ -94,20 +94,15 @@ it('passes axe', () => {
94
94
95
95
## Customised
96
96
97
-
Leveraging ([Cypress commands](https://docs.cypress.io/api/cypress-api/custom-commands)) You can either:
98
-
99
-
- Overwrite `cy.checkA11y`_or_
100
-
- Wrap it in your own custom command
101
-
102
-
then pass it a `shouldFailFn` function to decide which rules should fail. For example, only assert against _serious_ & _critical_ violations but ignore _color-contrast_ rule.
103
-
97
+
Leveraging [Cypress commands](https://docs.cypress.io/api/cypress-api/custom-commands), you can create your own custom command that calls `cy.checkA11y` with the config you want.
98
+
For example, if you only want to assert against _serious_ & _critical_ violations but ignore _color-contrast_ rule, you can do something like this:
104
99
```js
105
100
// cypress/support/commands.js
106
101
Cypress.Commands.add(
107
102
'checkAxeViolations',
108
103
{ prevSubject:'optional' },
109
-
(subject, options, label) => {
110
-
returncy.checkA11y(
104
+
(context, options, label) => {
105
+
cy.wrap(context).checkA11y(
111
106
{
112
107
shouldFailFn:violations=>
113
108
violations.filter(
@@ -151,14 +146,16 @@ beforeEach(() => {
151
146
When not chained to another element, it will run against the whole document. You can have it at the end of your test (after other interaction assertions) so it checks against all possible violations. It accepts the same (optional) config object that [`cy.configureCypressAxe`](#cyconfigureCypressAxe) accepts
152
147
153
148
**Note**: if you have a toggle-able element i.e. a side menu, make sure it's on (shown) by the time `cy.checkA11y` is called, otherwise you might end up with some false-positive cases. Or, you can target those elements directly to make sure they're tested
149
+
154
150
```js
155
-
cy.get('#menu-button').click();
151
+
cy.get('#menu-button').click()
156
152
cy.get('#side-menu-container').checkA11y()
157
153
```
158
154
159
155
## cy.configureCypressAxe
160
156
161
157
Instead of wrapping or overwriting `cy.checkA11y`, you can configure it. It accepts the following:
158
+
162
159
-`axeOptions` passed to axe-core.
163
160
-`shouldFailFn` function that returns array of violations to check for.
164
161
-`skipFailures` if true, it will log the violations but not assert against them.
0 commit comments