Back to Test Automation
2026-01-045 min read

Get started with Cypress Accessibility ➜ (Test Automation)

Learn Get started with Cypress Accessibility ➜ (Test Automation) step by step with clear examples and exercises.

Why This Matters

Accessibility testing is crucial for ensuring that web applications cater to users of all abilities and disabilities, fostering an inclusive user experience. Automated accessibility testing with tools like Cypress Accessibility can save time and resources compared to manual testing, help catch issues early in the development lifecycle, and ultimately improve the overall usability of your application.

Prerequisites

To follow this lesson, you'll need:

  1. Basic understanding of JavaScript
  2. Familiarity with Cypress test setup and running tests
  3. Installation of Node.js ()
  4. Installation of Cypress (npm install cypress --save-dev)
  5. Installation of Cypress Accessibility (npm install --save-dev cypress-axe)
  6. A project using Cypress for end-to-end testing
  7. Familiarity with the Axe Core engine () and its accessibility rules
  8. Understanding of HTML, CSS, and ARIA attributes for accessible web development

Core Concept

Cypress Accessibility leverages the Axe Core engine to scan your application for accessibility issues. You can incorporate accessibility tests seamlessly into your existing Cypress test suite by calling cy.checkA11y() at any point in your test file. This command checks the DOM against a set of rules and provides a report on any violations found.

Here's an example of how to use cy.checkA11y() in a Cypress test:

describe('Accessibility Test', () => {
it('Checks for accessibility issues', () => {
cy.visit('/your-app-url');
cy.checkA11y();

// Assertions to handle failure
cy.get('.cypress-axe').should('have.length', 0);
});
});

In this example, we're visiting the application URL and checking for accessibility issues using cy.checkA11y(). The test then asserts that no accessibility violations are found by checking if the element with the class cypress-axe (added by Cypress Accessibility) has a length of 0.

Customizing Axe Core Engine Rules

You can customize the rules used by the Axe Core engine by configuring the axeConfig option in your Cypress configuration file (cypress.json). For example:

{
"env": {
"axeConfig": {
"rules": {
"color-contrast": {
"enabled": false
},
"heading-order": {
"levels": [1, 2, 3, 4]
}
}
}
}
}

In this example, we've disabled the color-contrast rule and limited the heading order to levels 1 through 4.

Worked Example

Let's walk through a worked example where we identify and fix an accessibility issue in our application:

  1. Install Cypress Accessibility: npm install --save-dev cypress-axe
  2. Create a new test file (cypress/integration/accessibility_test.js) and write the following test:
describe('Accessibility Test', () => {
it('Checks for accessibility issues', () => {
cy.visit('/your-app-url');
cy.checkA11y();

// Assertions to handle failure
cy.get('.cypress-axe').should('have.length', 0);
});
});
  1. Run the test (npm run cy:run) and check the console output for any accessibility violations.
  1. Fix the identified issue and repeat the test to verify that it's resolved.

Practice Questions

  1. How can you configure Cypress Accessibility to skip certain elements during scans?

You can use the exclude option in your axeConfig to specify selectors for elements that should be excluded from accessibility scans. For example:

{
"env": {
"axeConfig": {
"rules": {},
"exclude": [".my-excluded-element"]
}
}
}
  1. How can you customize the Axe Core engine rules for your specific project?

You can configure the axeConfig option in your Cypress configuration file (cypress.json) to customize the rules used by the Axe Core engine. For example:

{
"env": {
"axeConfig": {
"rules": {
"color-contrast": {
"enabled": false
},
"heading-order": {
"levels": [1, 2, 3, 4]
}
}
}
}
}

In this example, we've disabled the color-contrast rule and limited the heading order to levels 1 through 4.

Common Mistakes

  1. Not initializing Cypress Accessibility correctly in your plugin file.
  2. Forgetting to include cy.checkA11y() at the appropriate point in your tests.
  3. Ignoring accessibility violations or not fixing them promptly.
  4. Testing on only one browser, as different browsers may have varying levels of accessibility support.
  5. Not configuring the Axe Core engine rules to suit your specific project's needs.
  6. Failing to handle false positives or false negatives in accessibility test results.
  7. Neglecting to update Cypress Accessibility to the latest version when new rule updates are available.
  8. Overlooking ARIA attributes and proper HTML structure in your application.
  9. Inadequately testing dynamic content, such as loading states or content generated by JavaScript.
  10. Not considering keyboard navigation and other assistive technologies when designing and testing your application.

Handling False Positives and False Negatives

False positives occur when a violation is reported even though there's no actual issue, while false negatives happen when an accessibility issue goes undetected. To handle these situations:

  1. Review the Axe Core engine rules to understand their implications and potential false positives or false negatives.
  2. Use custom rules or filters to exclude specific elements from scans if necessary.
  3. Manually verify any suspicious violations to confirm whether they're actual issues or false positives.
  4. Update the Axe Core engine or custom rules as needed to improve accuracy.
  5. Investigate potential causes of false negatives, such as incomplete DOM loading or complex dynamic content.
  6. Educate developers on writing accessible code and following best practices for accessibility.

FAQ

  1. How can I exclude certain elements from Cypress Accessibility scans?

You can use the exclude option in your axeConfig to specify selectors for elements that should be excluded from accessibility scans. For example:

{
"env": {
"axeConfig": {
"rules": {},
"exclude": [".my-excluded-element"]
}
}
}
  1. How can I customize the Axe Core engine rules for my specific project?

You can configure the axeConfig option in your Cypress configuration file (cypress.json) to customize the rules used by the Axe Core engine. For example:

{
"env": {
"axeConfig": {
"rules": {
"color-contrast": {
"enabled": false
},
"heading-order": {
"levels": [1, 2, 3, 4]
}
}
}
}
}

In this example, we've disabled the color-contrast rule and limited the heading order to levels 1 through 4.

Get started with Cypress Accessibility ➜ (Test Automation) | Test Automation | XQA Learn