Introduction
We use cypress-axe to check if websites are easy for everyone to use, including people with disabilities. It helps find problems that make websites hard to read or navigate.
import 'cypress-axe'; cy.injectAxe(); cy.checkA11y();
cy.visit('https://example.com');
cy.injectAxe();
cy.checkA11y();cy.checkA11y('.main-content');cy.checkA11y(null, { runOnly: ['wcag2a', 'wcag2aa'] });
cy.checkA11y(null, null, (violations) => { // custom handling of violations expect(violations.length).to.equal(0); });
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Accessibility Test</title> </head> <body> <header> <h1>Welcome to Our Site</h1> </header> <main> <button id="clickMe">Click Me</button> </main> </body> </html>
cypress-axe in Cypress tests?cy.injectAxe().cy.runAxe(), cy.loadAxe(), and cy.startAxe() are not valid Cypress commands for this purpose.cy.checkA11y() do?cy.visit('/home')
cy.injectAxe()
cy.checkA11y()cy.checkA11y() without arguments.cy.checkA11y() runs accessibility checks on the whole page and reports violations.cy.visit('/dashboard')
cy.checkA11y()cy.injectAxe().cy.checkA11y() does not run properly and silently fails.main-content. Which code snippet correctly does this using cypress-axe?cy.injectAxe() without arguments to add axe to the page.cy.checkA11y('#main-content') to check accessibility only inside that element.Pass the test and this pattern moves unknown