axe-cypress-a11y 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +135 -0
- package/dist/index.d.mts +117 -0
- package/dist/index.d.ts +117 -0
- package/dist/index.js +225 -0
- package/dist/index.mjs +195 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Malith Karunaratne
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# axe-cypress-a11y
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
Provides a chainable axe API for CypressIO.
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Automated accessibility testing builder for Cypress powered by [axe-core®](https://github.com/dequelabs/axe-core), following the same patterns as other `@axe-core/xyz` packages developed by [Deque Systems, Inc](https://www.deque.com/). axe®, and axe-core® are [trademarks of Deque Systems, Inc](https://www.deque.com/legal/trademarks/). Click to find out more detaila about [Axe tools](https://www.deque.com/axe/).
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
Install [Node.js](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) if you haven't already.
|
|
12
|
+
|
|
13
|
+
Install Cypress: `npm install cypress `
|
|
14
|
+
|
|
15
|
+
Install @axe-core/cypress: `npm install axe-cypress-a11y`
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
This module uses a chainable API to assist in injecting, configuring, and analyzing axe with [Cypress](https://www.cypress.io/).
|
|
20
|
+
|
|
21
|
+
Here is an example of a script that will drive Cypress to a page, perform an analysis, and then log results.
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import AxeBuilder from 'axe-cypress-a11y';
|
|
25
|
+
|
|
26
|
+
describe('Accessibility Tests', () => {
|
|
27
|
+
it('should have no accessibility violations', () => {
|
|
28
|
+
cy.visit('/');
|
|
29
|
+
|
|
30
|
+
new AxeBuilder()
|
|
31
|
+
.include('.main-content')
|
|
32
|
+
.exclude('.ads')
|
|
33
|
+
.withTags(['wcag2a', 'wcag2aa'])
|
|
34
|
+
.analyze()
|
|
35
|
+
.then((results) => {
|
|
36
|
+
cy.log(`Violations: ${JSON.stringify(results.violations, null, 2)}`);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API
|
|
44
|
+
|
|
45
|
+
### `new AxeBuilder()`
|
|
46
|
+
|
|
47
|
+
Creates a new AxeBuilder instance.
|
|
48
|
+
|
|
49
|
+
### `.include(selector: string)`
|
|
50
|
+
|
|
51
|
+
Add a CSS selector to include in the analysis.
|
|
52
|
+
|
|
53
|
+
### `.exclude(selector: string)`
|
|
54
|
+
|
|
55
|
+
Add a CSS selector to exclude from the analysis.
|
|
56
|
+
|
|
57
|
+
### `.withTags(tags: string | string[])`
|
|
58
|
+
|
|
59
|
+
Limit analysis to rules with specific tags (e.g., 'wcag2a', 'wcag2aa', 'wcag21a').
|
|
60
|
+
|
|
61
|
+
### `.withRules(rules: string | string[])`
|
|
62
|
+
|
|
63
|
+
Limit analysis to specific rule IDs.
|
|
64
|
+
|
|
65
|
+
### `.disableRules(rules: string | string[])`
|
|
66
|
+
|
|
67
|
+
Disable specific rules from the analysis.
|
|
68
|
+
|
|
69
|
+
### `.options(options: RunOptions)`
|
|
70
|
+
|
|
71
|
+
Pass custom options to axe.run().
|
|
72
|
+
|
|
73
|
+
### `.configure(config: Spec)`
|
|
74
|
+
|
|
75
|
+
Configure axe with custom settings.
|
|
76
|
+
|
|
77
|
+
### `.analyze()`
|
|
78
|
+
|
|
79
|
+
Run the accessibility analysis and return results.
|
|
80
|
+
|
|
81
|
+
## Examples
|
|
82
|
+
|
|
83
|
+
### Basic Usage
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
new AxeBuilder()
|
|
87
|
+
.analyze()
|
|
88
|
+
.then((results) => {
|
|
89
|
+
cy.log(`Found ${results.violations.length} violations`);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### With Specific Tags
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
new AxeBuilder()
|
|
98
|
+
.withTags(['wcag2a', 'wcag2aa'])
|
|
99
|
+
.analyze()
|
|
100
|
+
.then((results) => {
|
|
101
|
+
expect(results.violations).to.have.length(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Exclude Elements
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
new AxeBuilder()
|
|
110
|
+
.exclude('.third-party-widget')
|
|
111
|
+
.exclude('#ads')
|
|
112
|
+
.analyze()
|
|
113
|
+
.then((results) => {
|
|
114
|
+
expect(results.violations).to.have.length(0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Custom Configuration
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
new AxeBuilder()
|
|
123
|
+
.configure({
|
|
124
|
+
rules: [{
|
|
125
|
+
id: 'color-contrast',
|
|
126
|
+
enabled: false
|
|
127
|
+
}]
|
|
128
|
+
})
|
|
129
|
+
.analyze();
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { RunOptions, Spec, AxeResults } from 'axe-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Additional options specific to the Cypress AxeBuilder
|
|
5
|
+
*/
|
|
6
|
+
interface AxeBuilderOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Time interval between retries in milliseconds
|
|
9
|
+
*/
|
|
10
|
+
interval?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Number of times to retry if violations are found
|
|
13
|
+
*/
|
|
14
|
+
retries?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Context object for specifying include/exclude selectors
|
|
18
|
+
*/
|
|
19
|
+
interface ContextObject {
|
|
20
|
+
include?: string[];
|
|
21
|
+
exclude?: string[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Combined options for axe.run including Cypress-specific options
|
|
25
|
+
*/
|
|
26
|
+
type AxeRunOptions = RunOptions & AxeBuilderOptions;
|
|
27
|
+
declare global {
|
|
28
|
+
namespace Cypress {
|
|
29
|
+
interface Chainable<Subject = any> {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* AxeBuilder class for Cypress
|
|
36
|
+
* Provides a chainable API to configure and run axe-core accessibility tests
|
|
37
|
+
*/
|
|
38
|
+
declare class AxeBuilder {
|
|
39
|
+
private _includes;
|
|
40
|
+
private _excludes;
|
|
41
|
+
private _options;
|
|
42
|
+
constructor();
|
|
43
|
+
/**
|
|
44
|
+
* Add a CSS selector to the list of elements to include in analysis
|
|
45
|
+
* @param selector - CSS selector to include
|
|
46
|
+
* @returns Returns this for method chaining
|
|
47
|
+
*/
|
|
48
|
+
include(selector: string): this;
|
|
49
|
+
/**
|
|
50
|
+
* Add a CSS selector to the list of elements to exclude from analysis
|
|
51
|
+
* @param selector - CSS selector to exclude
|
|
52
|
+
* @returns Returns this for method chaining
|
|
53
|
+
*/
|
|
54
|
+
exclude(selector: string): this;
|
|
55
|
+
/**
|
|
56
|
+
* Specifies options to be used by axe.run
|
|
57
|
+
* Will override any other configured options, including calls to withRules() and withTags()
|
|
58
|
+
* @param options - Options object to pass to axe.run
|
|
59
|
+
* @returns Returns this for method chaining
|
|
60
|
+
*/
|
|
61
|
+
options(options: AxeRunOptions): this;
|
|
62
|
+
/**
|
|
63
|
+
* Limits analysis to only those with the specified rule IDs
|
|
64
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
65
|
+
* @param rules - Rule ID(s) to run
|
|
66
|
+
* @returns Returns this for method chaining
|
|
67
|
+
*/
|
|
68
|
+
withRules(rules: string | string[]): this;
|
|
69
|
+
/**
|
|
70
|
+
* Limits analysis to only those with the specified tags
|
|
71
|
+
* Accepts a String of a single tag or an Array of multiple tags
|
|
72
|
+
* @param tags - Tag(s) to run
|
|
73
|
+
* @returns Returns this for method chaining
|
|
74
|
+
*/
|
|
75
|
+
withTags(tags: string | string[]): this;
|
|
76
|
+
/**
|
|
77
|
+
* Skips verification of the rules provided
|
|
78
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
79
|
+
* @param rules - Rule ID(s) to disable
|
|
80
|
+
* @returns Returns this for method chaining
|
|
81
|
+
*/
|
|
82
|
+
disableRules(rules: string | string[]): this;
|
|
83
|
+
/**
|
|
84
|
+
* Set the frame testing method to "legacy mode"
|
|
85
|
+
* In this mode, axe will not open a blank page to aggregate results
|
|
86
|
+
* Use as a last resort when opening a blank page causes issues
|
|
87
|
+
* @param _legacyMode - Whether to enable legacy mode (default: true)
|
|
88
|
+
* @returns Returns this for method chaining
|
|
89
|
+
*/
|
|
90
|
+
setLegacyMode(_legacyMode?: boolean): this;
|
|
91
|
+
/**
|
|
92
|
+
* Configures axe with custom configuration
|
|
93
|
+
* @param config - Configuration object to pass to axe.configure
|
|
94
|
+
* @returns Returns this for method chaining
|
|
95
|
+
*/
|
|
96
|
+
configure(config: Spec): Cypress.Chainable<this>;
|
|
97
|
+
/**
|
|
98
|
+
* Performs analysis and returns results
|
|
99
|
+
* @returns Cypress chainable with axe results
|
|
100
|
+
*/
|
|
101
|
+
analyze(): Cypress.Chainable<AxeResults>;
|
|
102
|
+
/**
|
|
103
|
+
* Internal method to ensure axe-core is injected into the page
|
|
104
|
+
* @private
|
|
105
|
+
* @param win - The window object
|
|
106
|
+
* @returns Promise that resolves when axe is ready
|
|
107
|
+
*/
|
|
108
|
+
private _ensureAxeInjected;
|
|
109
|
+
/**
|
|
110
|
+
* Internal method to build the context object for axe.run
|
|
111
|
+
* @private
|
|
112
|
+
* @returns Context object or undefined
|
|
113
|
+
*/
|
|
114
|
+
private _buildContext;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { AxeBuilder, type AxeBuilderOptions, type AxeRunOptions, type ContextObject, AxeBuilder as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { RunOptions, Spec, AxeResults } from 'axe-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Additional options specific to the Cypress AxeBuilder
|
|
5
|
+
*/
|
|
6
|
+
interface AxeBuilderOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Time interval between retries in milliseconds
|
|
9
|
+
*/
|
|
10
|
+
interval?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Number of times to retry if violations are found
|
|
13
|
+
*/
|
|
14
|
+
retries?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Context object for specifying include/exclude selectors
|
|
18
|
+
*/
|
|
19
|
+
interface ContextObject {
|
|
20
|
+
include?: string[];
|
|
21
|
+
exclude?: string[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Combined options for axe.run including Cypress-specific options
|
|
25
|
+
*/
|
|
26
|
+
type AxeRunOptions = RunOptions & AxeBuilderOptions;
|
|
27
|
+
declare global {
|
|
28
|
+
namespace Cypress {
|
|
29
|
+
interface Chainable<Subject = any> {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* AxeBuilder class for Cypress
|
|
36
|
+
* Provides a chainable API to configure and run axe-core accessibility tests
|
|
37
|
+
*/
|
|
38
|
+
declare class AxeBuilder {
|
|
39
|
+
private _includes;
|
|
40
|
+
private _excludes;
|
|
41
|
+
private _options;
|
|
42
|
+
constructor();
|
|
43
|
+
/**
|
|
44
|
+
* Add a CSS selector to the list of elements to include in analysis
|
|
45
|
+
* @param selector - CSS selector to include
|
|
46
|
+
* @returns Returns this for method chaining
|
|
47
|
+
*/
|
|
48
|
+
include(selector: string): this;
|
|
49
|
+
/**
|
|
50
|
+
* Add a CSS selector to the list of elements to exclude from analysis
|
|
51
|
+
* @param selector - CSS selector to exclude
|
|
52
|
+
* @returns Returns this for method chaining
|
|
53
|
+
*/
|
|
54
|
+
exclude(selector: string): this;
|
|
55
|
+
/**
|
|
56
|
+
* Specifies options to be used by axe.run
|
|
57
|
+
* Will override any other configured options, including calls to withRules() and withTags()
|
|
58
|
+
* @param options - Options object to pass to axe.run
|
|
59
|
+
* @returns Returns this for method chaining
|
|
60
|
+
*/
|
|
61
|
+
options(options: AxeRunOptions): this;
|
|
62
|
+
/**
|
|
63
|
+
* Limits analysis to only those with the specified rule IDs
|
|
64
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
65
|
+
* @param rules - Rule ID(s) to run
|
|
66
|
+
* @returns Returns this for method chaining
|
|
67
|
+
*/
|
|
68
|
+
withRules(rules: string | string[]): this;
|
|
69
|
+
/**
|
|
70
|
+
* Limits analysis to only those with the specified tags
|
|
71
|
+
* Accepts a String of a single tag or an Array of multiple tags
|
|
72
|
+
* @param tags - Tag(s) to run
|
|
73
|
+
* @returns Returns this for method chaining
|
|
74
|
+
*/
|
|
75
|
+
withTags(tags: string | string[]): this;
|
|
76
|
+
/**
|
|
77
|
+
* Skips verification of the rules provided
|
|
78
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
79
|
+
* @param rules - Rule ID(s) to disable
|
|
80
|
+
* @returns Returns this for method chaining
|
|
81
|
+
*/
|
|
82
|
+
disableRules(rules: string | string[]): this;
|
|
83
|
+
/**
|
|
84
|
+
* Set the frame testing method to "legacy mode"
|
|
85
|
+
* In this mode, axe will not open a blank page to aggregate results
|
|
86
|
+
* Use as a last resort when opening a blank page causes issues
|
|
87
|
+
* @param _legacyMode - Whether to enable legacy mode (default: true)
|
|
88
|
+
* @returns Returns this for method chaining
|
|
89
|
+
*/
|
|
90
|
+
setLegacyMode(_legacyMode?: boolean): this;
|
|
91
|
+
/**
|
|
92
|
+
* Configures axe with custom configuration
|
|
93
|
+
* @param config - Configuration object to pass to axe.configure
|
|
94
|
+
* @returns Returns this for method chaining
|
|
95
|
+
*/
|
|
96
|
+
configure(config: Spec): Cypress.Chainable<this>;
|
|
97
|
+
/**
|
|
98
|
+
* Performs analysis and returns results
|
|
99
|
+
* @returns Cypress chainable with axe results
|
|
100
|
+
*/
|
|
101
|
+
analyze(): Cypress.Chainable<AxeResults>;
|
|
102
|
+
/**
|
|
103
|
+
* Internal method to ensure axe-core is injected into the page
|
|
104
|
+
* @private
|
|
105
|
+
* @param win - The window object
|
|
106
|
+
* @returns Promise that resolves when axe is ready
|
|
107
|
+
*/
|
|
108
|
+
private _ensureAxeInjected;
|
|
109
|
+
/**
|
|
110
|
+
* Internal method to build the context object for axe.run
|
|
111
|
+
* @private
|
|
112
|
+
* @returns Context object or undefined
|
|
113
|
+
*/
|
|
114
|
+
private _buildContext;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { AxeBuilder, type AxeBuilderOptions, type AxeRunOptions, type ContextObject, AxeBuilder as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AxeBuilder: () => AxeBuilder,
|
|
34
|
+
default: () => index_default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/utils.ts
|
|
39
|
+
function getAxeCorePath() {
|
|
40
|
+
if (typeof require.resolve === "function") {
|
|
41
|
+
return require.resolve("axe-core/axe.min.js");
|
|
42
|
+
}
|
|
43
|
+
return "node_modules/axe-core/axe.min.js";
|
|
44
|
+
}
|
|
45
|
+
function isAxeInjected(win) {
|
|
46
|
+
return "axe" in win && typeof win.axe === "object" && typeof win.axe.run === "function";
|
|
47
|
+
}
|
|
48
|
+
function injectAxeCore(win, source) {
|
|
49
|
+
win.eval(source);
|
|
50
|
+
}
|
|
51
|
+
function normalizeSelector(selector) {
|
|
52
|
+
return Array.isArray(selector) ? selector : [selector];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/index.ts
|
|
56
|
+
var AxeBuilder = class {
|
|
57
|
+
constructor() {
|
|
58
|
+
this._includes = [];
|
|
59
|
+
this._excludes = [];
|
|
60
|
+
this._options = {};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Add a CSS selector to the list of elements to include in analysis
|
|
64
|
+
* @param selector - CSS selector to include
|
|
65
|
+
* @returns Returns this for method chaining
|
|
66
|
+
*/
|
|
67
|
+
include(selector) {
|
|
68
|
+
if (selector) {
|
|
69
|
+
this._includes.push(selector);
|
|
70
|
+
}
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Add a CSS selector to the list of elements to exclude from analysis
|
|
75
|
+
* @param selector - CSS selector to exclude
|
|
76
|
+
* @returns Returns this for method chaining
|
|
77
|
+
*/
|
|
78
|
+
exclude(selector) {
|
|
79
|
+
if (selector) {
|
|
80
|
+
this._excludes.push(selector);
|
|
81
|
+
}
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Specifies options to be used by axe.run
|
|
86
|
+
* Will override any other configured options, including calls to withRules() and withTags()
|
|
87
|
+
* @param options - Options object to pass to axe.run
|
|
88
|
+
* @returns Returns this for method chaining
|
|
89
|
+
*/
|
|
90
|
+
options(options) {
|
|
91
|
+
this._options = { ...this._options, ...options };
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Limits analysis to only those with the specified rule IDs
|
|
96
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
97
|
+
* @param rules - Rule ID(s) to run
|
|
98
|
+
* @returns Returns this for method chaining
|
|
99
|
+
*/
|
|
100
|
+
withRules(rules) {
|
|
101
|
+
const ruleArray = normalizeSelector(rules);
|
|
102
|
+
this._options.runOnly = {
|
|
103
|
+
type: "rule",
|
|
104
|
+
values: ruleArray
|
|
105
|
+
};
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Limits analysis to only those with the specified tags
|
|
110
|
+
* Accepts a String of a single tag or an Array of multiple tags
|
|
111
|
+
* @param tags - Tag(s) to run
|
|
112
|
+
* @returns Returns this for method chaining
|
|
113
|
+
*/
|
|
114
|
+
withTags(tags) {
|
|
115
|
+
const tagArray = normalizeSelector(tags);
|
|
116
|
+
this._options.runOnly = {
|
|
117
|
+
type: "tag",
|
|
118
|
+
values: tagArray
|
|
119
|
+
};
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Skips verification of the rules provided
|
|
124
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
125
|
+
* @param rules - Rule ID(s) to disable
|
|
126
|
+
* @returns Returns this for method chaining
|
|
127
|
+
*/
|
|
128
|
+
disableRules(rules) {
|
|
129
|
+
const ruleArray = normalizeSelector(rules);
|
|
130
|
+
this._options.rules = this._options.rules || {};
|
|
131
|
+
ruleArray.forEach((rule) => {
|
|
132
|
+
this._options.rules[rule] = { enabled: false };
|
|
133
|
+
});
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Set the frame testing method to "legacy mode"
|
|
138
|
+
* In this mode, axe will not open a blank page to aggregate results
|
|
139
|
+
* Use as a last resort when opening a blank page causes issues
|
|
140
|
+
* @param _legacyMode - Whether to enable legacy mode (default: true)
|
|
141
|
+
* @returns Returns this for method chaining
|
|
142
|
+
*/
|
|
143
|
+
setLegacyMode(_legacyMode = true) {
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Configures axe with custom configuration
|
|
148
|
+
* @param config - Configuration object to pass to axe.configure
|
|
149
|
+
* @returns Returns this for method chaining
|
|
150
|
+
*/
|
|
151
|
+
configure(config) {
|
|
152
|
+
return cy.window({ log: false }).then((win) => {
|
|
153
|
+
return this._ensureAxeInjected(win).then(() => {
|
|
154
|
+
win.axe.configure(config);
|
|
155
|
+
return this;
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Performs analysis and returns results
|
|
161
|
+
* @returns Cypress chainable with axe results
|
|
162
|
+
*/
|
|
163
|
+
analyze() {
|
|
164
|
+
return cy.window({ log: false }).then((win) => {
|
|
165
|
+
return this._ensureAxeInjected(win).then(() => {
|
|
166
|
+
const context = this._buildContext();
|
|
167
|
+
const { interval, retries, ...axeOptions } = this._options;
|
|
168
|
+
let remainingRetries = retries || 0;
|
|
169
|
+
const runAxeCheck = () => {
|
|
170
|
+
return win.axe.run(context || win.document, axeOptions).then((results) => {
|
|
171
|
+
if (results.violations.length > 0 && remainingRetries > 0) {
|
|
172
|
+
remainingRetries--;
|
|
173
|
+
return new Promise((resolve) => {
|
|
174
|
+
setTimeout(() => resolve(runAxeCheck()), interval || 1e3);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return results;
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
return runAxeCheck();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Internal method to ensure axe-core is injected into the page
|
|
186
|
+
* @private
|
|
187
|
+
* @param win - The window object
|
|
188
|
+
* @returns Promise that resolves when axe is ready
|
|
189
|
+
*/
|
|
190
|
+
_ensureAxeInjected(win) {
|
|
191
|
+
if (isAxeInjected(win)) {
|
|
192
|
+
return cy.then(() => {
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
const axePath = getAxeCorePath();
|
|
196
|
+
return cy.readFile(axePath, { log: false }).then((source) => {
|
|
197
|
+
injectAxeCore(win, source);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Internal method to build the context object for axe.run
|
|
202
|
+
* @private
|
|
203
|
+
* @returns Context object or undefined
|
|
204
|
+
*/
|
|
205
|
+
_buildContext() {
|
|
206
|
+
const hasIncludes = this._includes.length > 0;
|
|
207
|
+
const hasExcludes = this._excludes.length > 0;
|
|
208
|
+
if (!hasIncludes && !hasExcludes) {
|
|
209
|
+
return void 0;
|
|
210
|
+
}
|
|
211
|
+
const context = {};
|
|
212
|
+
if (hasIncludes) {
|
|
213
|
+
context.include = this._includes;
|
|
214
|
+
}
|
|
215
|
+
if (hasExcludes) {
|
|
216
|
+
context.exclude = this._excludes;
|
|
217
|
+
}
|
|
218
|
+
return context;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var index_default = AxeBuilder;
|
|
222
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
223
|
+
0 && (module.exports = {
|
|
224
|
+
AxeBuilder
|
|
225
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// src/utils.ts
|
|
9
|
+
function getAxeCorePath() {
|
|
10
|
+
if (typeof __require.resolve === "function") {
|
|
11
|
+
return __require.resolve("axe-core/axe.min.js");
|
|
12
|
+
}
|
|
13
|
+
return "node_modules/axe-core/axe.min.js";
|
|
14
|
+
}
|
|
15
|
+
function isAxeInjected(win) {
|
|
16
|
+
return "axe" in win && typeof win.axe === "object" && typeof win.axe.run === "function";
|
|
17
|
+
}
|
|
18
|
+
function injectAxeCore(win, source) {
|
|
19
|
+
win.eval(source);
|
|
20
|
+
}
|
|
21
|
+
function normalizeSelector(selector) {
|
|
22
|
+
return Array.isArray(selector) ? selector : [selector];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
var AxeBuilder = class {
|
|
27
|
+
constructor() {
|
|
28
|
+
this._includes = [];
|
|
29
|
+
this._excludes = [];
|
|
30
|
+
this._options = {};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Add a CSS selector to the list of elements to include in analysis
|
|
34
|
+
* @param selector - CSS selector to include
|
|
35
|
+
* @returns Returns this for method chaining
|
|
36
|
+
*/
|
|
37
|
+
include(selector) {
|
|
38
|
+
if (selector) {
|
|
39
|
+
this._includes.push(selector);
|
|
40
|
+
}
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Add a CSS selector to the list of elements to exclude from analysis
|
|
45
|
+
* @param selector - CSS selector to exclude
|
|
46
|
+
* @returns Returns this for method chaining
|
|
47
|
+
*/
|
|
48
|
+
exclude(selector) {
|
|
49
|
+
if (selector) {
|
|
50
|
+
this._excludes.push(selector);
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Specifies options to be used by axe.run
|
|
56
|
+
* Will override any other configured options, including calls to withRules() and withTags()
|
|
57
|
+
* @param options - Options object to pass to axe.run
|
|
58
|
+
* @returns Returns this for method chaining
|
|
59
|
+
*/
|
|
60
|
+
options(options) {
|
|
61
|
+
this._options = { ...this._options, ...options };
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Limits analysis to only those with the specified rule IDs
|
|
66
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
67
|
+
* @param rules - Rule ID(s) to run
|
|
68
|
+
* @returns Returns this for method chaining
|
|
69
|
+
*/
|
|
70
|
+
withRules(rules) {
|
|
71
|
+
const ruleArray = normalizeSelector(rules);
|
|
72
|
+
this._options.runOnly = {
|
|
73
|
+
type: "rule",
|
|
74
|
+
values: ruleArray
|
|
75
|
+
};
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Limits analysis to only those with the specified tags
|
|
80
|
+
* Accepts a String of a single tag or an Array of multiple tags
|
|
81
|
+
* @param tags - Tag(s) to run
|
|
82
|
+
* @returns Returns this for method chaining
|
|
83
|
+
*/
|
|
84
|
+
withTags(tags) {
|
|
85
|
+
const tagArray = normalizeSelector(tags);
|
|
86
|
+
this._options.runOnly = {
|
|
87
|
+
type: "tag",
|
|
88
|
+
values: tagArray
|
|
89
|
+
};
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Skips verification of the rules provided
|
|
94
|
+
* Accepts a String of a single rule ID or an Array of multiple rule IDs
|
|
95
|
+
* @param rules - Rule ID(s) to disable
|
|
96
|
+
* @returns Returns this for method chaining
|
|
97
|
+
*/
|
|
98
|
+
disableRules(rules) {
|
|
99
|
+
const ruleArray = normalizeSelector(rules);
|
|
100
|
+
this._options.rules = this._options.rules || {};
|
|
101
|
+
ruleArray.forEach((rule) => {
|
|
102
|
+
this._options.rules[rule] = { enabled: false };
|
|
103
|
+
});
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Set the frame testing method to "legacy mode"
|
|
108
|
+
* In this mode, axe will not open a blank page to aggregate results
|
|
109
|
+
* Use as a last resort when opening a blank page causes issues
|
|
110
|
+
* @param _legacyMode - Whether to enable legacy mode (default: true)
|
|
111
|
+
* @returns Returns this for method chaining
|
|
112
|
+
*/
|
|
113
|
+
setLegacyMode(_legacyMode = true) {
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Configures axe with custom configuration
|
|
118
|
+
* @param config - Configuration object to pass to axe.configure
|
|
119
|
+
* @returns Returns this for method chaining
|
|
120
|
+
*/
|
|
121
|
+
configure(config) {
|
|
122
|
+
return cy.window({ log: false }).then((win) => {
|
|
123
|
+
return this._ensureAxeInjected(win).then(() => {
|
|
124
|
+
win.axe.configure(config);
|
|
125
|
+
return this;
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Performs analysis and returns results
|
|
131
|
+
* @returns Cypress chainable with axe results
|
|
132
|
+
*/
|
|
133
|
+
analyze() {
|
|
134
|
+
return cy.window({ log: false }).then((win) => {
|
|
135
|
+
return this._ensureAxeInjected(win).then(() => {
|
|
136
|
+
const context = this._buildContext();
|
|
137
|
+
const { interval, retries, ...axeOptions } = this._options;
|
|
138
|
+
let remainingRetries = retries || 0;
|
|
139
|
+
const runAxeCheck = () => {
|
|
140
|
+
return win.axe.run(context || win.document, axeOptions).then((results) => {
|
|
141
|
+
if (results.violations.length > 0 && remainingRetries > 0) {
|
|
142
|
+
remainingRetries--;
|
|
143
|
+
return new Promise((resolve) => {
|
|
144
|
+
setTimeout(() => resolve(runAxeCheck()), interval || 1e3);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return results;
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
return runAxeCheck();
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Internal method to ensure axe-core is injected into the page
|
|
156
|
+
* @private
|
|
157
|
+
* @param win - The window object
|
|
158
|
+
* @returns Promise that resolves when axe is ready
|
|
159
|
+
*/
|
|
160
|
+
_ensureAxeInjected(win) {
|
|
161
|
+
if (isAxeInjected(win)) {
|
|
162
|
+
return cy.then(() => {
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const axePath = getAxeCorePath();
|
|
166
|
+
return cy.readFile(axePath, { log: false }).then((source) => {
|
|
167
|
+
injectAxeCore(win, source);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Internal method to build the context object for axe.run
|
|
172
|
+
* @private
|
|
173
|
+
* @returns Context object or undefined
|
|
174
|
+
*/
|
|
175
|
+
_buildContext() {
|
|
176
|
+
const hasIncludes = this._includes.length > 0;
|
|
177
|
+
const hasExcludes = this._excludes.length > 0;
|
|
178
|
+
if (!hasIncludes && !hasExcludes) {
|
|
179
|
+
return void 0;
|
|
180
|
+
}
|
|
181
|
+
const context = {};
|
|
182
|
+
if (hasIncludes) {
|
|
183
|
+
context.include = this._includes;
|
|
184
|
+
}
|
|
185
|
+
if (hasExcludes) {
|
|
186
|
+
context.exclude = this._excludes;
|
|
187
|
+
}
|
|
188
|
+
return context;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
var index_default = AxeBuilder;
|
|
192
|
+
export {
|
|
193
|
+
AxeBuilder,
|
|
194
|
+
index_default as default
|
|
195
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "axe-cypress-a11y",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "axe-core plugin for cypress",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/malithlk/axe-core-cypress#readme",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/malithlk/axe-core-cypress.git"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup src/index.ts --dts --format esm,cjs",
|
|
20
|
+
"lint": "eslint . --cache --fix",
|
|
21
|
+
"pretest": "npm run lint",
|
|
22
|
+
"test:e2e:ci": "npx cypress run --spec 'cypress/e2e/axe-cypress.cy.ts' --headless",
|
|
23
|
+
"test": "npm run test:e2e:ci"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"cypress": ">=12.0.0",
|
|
27
|
+
"axe-core": ">=4.2.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^22.14.1",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "^4.8.1",
|
|
32
|
+
"@typescript-eslint/parser": "^4.8.1",
|
|
33
|
+
"axe-core": "^4.11.0",
|
|
34
|
+
"cypress": "^15.8.0",
|
|
35
|
+
"eslint": "^7.12.0",
|
|
36
|
+
"eslint-config-tamia": "^7.2.6",
|
|
37
|
+
"http-server": "^0.12.3",
|
|
38
|
+
"globals": "^16.3.0",
|
|
39
|
+
"husky": "^9.0.10",
|
|
40
|
+
"lint-staged": "^16.1.2",
|
|
41
|
+
"prettier": "^3.0.3",
|
|
42
|
+
"start-server-and-test": "^1.11.5",
|
|
43
|
+
"ts-node": "^10.9.1",
|
|
44
|
+
"tsup": "^8.0.1",
|
|
45
|
+
"typescript": "^5.6.3",
|
|
46
|
+
"@types/mocha": "^10.0.0"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"a11y",
|
|
50
|
+
"cypress",
|
|
51
|
+
"testing",
|
|
52
|
+
"plugin",
|
|
53
|
+
"axe-core"
|
|
54
|
+
],
|
|
55
|
+
"authors": [
|
|
56
|
+
{
|
|
57
|
+
"name": "Malith Karunaratne",
|
|
58
|
+
"url": "https://github.com/malithlk"
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
"license": "MIT"
|
|
62
|
+
}
|