How To Perform Cypress Accessibility Testing

Enrique

Posted On: June 27, 2022

13 Min

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Cypress Tutorial.

Cypress accessibility testing ensures that applications are usable by everyone, including people with disabilities, often with the help of assistive technologies like screen readers and keyboard navigation. It is not just about inclusivity; many countries consider inaccessible applications as discriminatory, making accessibility a legal requirement.

Accessibility testing helps verify that applications work for all users, and automating these tests saves time while maintaining quality. Cypress simplifies this process by providing an all-in-one platform for frontend, backend, performance, and accessibility testing. In this tutorial, we will explore why accessibility testing matters and how to perform Cypress accessibility tests on a cloud grid.

Overview

Accessibility testing ensures your web applications are usable by everyone, including those with disabilities, using assistive technologies like screen readers. Automating accessibility tests with Cypress saves time while ensuring compliance.

Why is Accessibility Testing Important?

Accessibility testing not only ensures compliance but also improves user experience, expands your audience, and fulfills ethical responsibilities in product design.

  • Legal Compliance: Many countries have laws mandating accessibility for web applications, and failure to comply can lead to legal consequences.
  • Wider Audience Reach: Making your application accessible ensures that it can be used by everyone, including those with disabilities, improving user experience.
  • Ethical Responsibility: Ensuring accessibility is not just about compliance—it’s about making your product available to all users, regardless of their abilities.

Is it Possible to Automate Accessibility Testing?

Automation helps catch accessibility issues early in development and ensures consistent testing across multiple pages and scenarios.

  • Automated Tools: Tools like Axe-Core and Google Lighthouse can perform accessibility checks quickly and consistently, saving testers time and effort.
  • Integration with Testing Frameworks: These tools can be integrated into automation frameworks like Cypress, allowing for continuous accessibility testing during development.
  • Speed and Efficiency: Automation enables repeated testing throughout the development cycle, helping to catch accessibility issues early.

How to Perform Cypress Accessibility Testing?

Integrating tools like Pa11y and Axe with Cypress allows you to automatically check your web pages for accessibility violations in real-time.

  • Install Accessibility Tools: Tools like Pa11y and Axe can be integrated into Cypress for automated accessibility checks.
  • Set Up Cypress Commands: Use commands like cy.pa11y() in Cypress to run accessibility tests against your web pages.
  • Cloud Grid Testing: For large-scale testing, leverage cloud platforms like LambdaTest to run Cypress accessibility tests on multiple browsers and environments, ensuring broad test coverage.

How to Use Pa11y for Cypress Accessibility Testing?

Pa11y allows you to automate accessibility audits within Cypress, providing detailed reports on violations to streamline remediation.

  • Setup Pa11y: Install the Pa11y package in your project and configure Cypress to use Pa11y for accessibility audits.
  • Run Tests: Use cy.pa11y() within your Cypress test scripts to automatically check for accessibility issues.
  • Report Violations: The tests provide detailed reports of accessibility violations, allowing you to fix issues like insufficient color contrast or missing alt text.

How to Perform Cypress Accessibility Testing on the Cloud Grid?

Using cloud grids like LambdaTest enables parallel testing across multiple browsers and operating systems, increasing coverage and reliability.

  • Parallel Testing: Use cloud platforms like LambdaTest to run Cypress accessibility tests in parallel across multiple browsers and operating systems.
  • Improved Coverage: Cloud grid testing ensures that your application is accessible across different devices and platforms, increasing test coverage and reliability.
  • Efficient Execution: Using cloud grids allows you to execute tests at scale, speeding up the testing process and improving overall productivity.

The importance of Accessibility Testing

The success of an application depends on whether everyone can achieve their goals through their chosen technologies.

We must test an interface against a set of guidelines if we want to be considered for accessibility testing. Common issues that the WCAG doesn’t cover may be considered as well. If you have an accessibility policy, it is good to test your site against its criteria. There are a lot of broad use cases that are covered by the WCAG 2.1 criteria.

Accessibility testing should be focused on the application’s actual use. There are a lot of reasons why a product might not be accessible. It can be a problem with the software itself or how it was designed. Testing for accessibility is about the user and what they want to do. If you create a feature that does not help a user accomplish a task, you need to ask yourself: “Why not?”. Accessibility testing is about making sure a product works for everyone.

Is it possible to automate Accessibility Testing?

Automated accessibility testing uses special software to check your digital product for accessibility problems based on set accessibility standards. The advantages of these tests are that they can be done multiple times during the product’s development and are quick and straightforward to perform, giving you speedy results.

Accessibility testing is an essential part of quality assurance, and there are many different ways of conducting it. Unfortunately, exploratory testing for people with impairments is only one of many options, and sometimes it’s necessary to use other accessibility testing tools.

Test websites against criteria; many modern testing tools help companies like ours. For example, you can use a keyboard or screen reader that people with disabilities commonly use. Axe or Google Lighthouse are browser extensions that can run accessibility checks.

Axe-Core is the most common tool for automated accessibility testing in your software development process. Numerous projects are built on top of the Axe-Core; if you use Selenium, WebdriverIO, Cypress, or another automated testing framework, you can implement Axe in your tests.

Ensure Web Inclusivity With Accessibility DevTools

In addition to these tools, such as Axe and Google Lighthouse, you can leverage the Accessibility DevTools offered by LambdaTest. This Chrome extension provides a convenient way to test for accessibility issues directly within your browser. It offers features like Full Page Scan, Partial Page Scan, Multi-Page Scan, Workflow Scan, Quick Issue Discovery, and more. These features allow testers to efficiently test, manage, and report accessibility issues, ensuring the website is accessible to everyone, including users with disabilities and visual impairments.

We want to try new things in this article on Cypress accessibility testing, so I would like to share my new finding, pa11y, a powerful tool for accessibility testing.

Pally and Cypress Accessibility Testing

Firstly, let’s talk about Pally, a command-line interface that loads web pages and highlights any accessibility issues it finds. Second, Pa11y is an automated accessibility testing tool. It runs accessibility tests on your pages via the command line so that you can automate your testing process(Pa11y is licensed under the Lesser General Public License (LGPL-3.0-only).

Now, let’s focus on Cypress and Pally and follow the below-mentioned steps for performing Cypress accessibility testing:

  1. Install the dependency in our local project using the below command:
  2. The following configuration allows Lighthouse and Cypress to make their verifications inside the same browser (controlled by Cypress) instead of creating a new one. In the cypress/plugins/index.js file, let’s add the following:
  3. Filename – cypress/support/commands.js

  4. Once we add the following line in the cypress/support/commands.js file, you will be able to use cy.pa11y inside your Cypress tests:
  5. Now, we are ready to evaluate some web pages for accessibility; let’s use the following example:
  6. And then let’s run it using npx cypress open.

    run it using npx cypress open

    As we identified from the image above, it displays some errors; more specifically, 36 accessibility violations were found.

    it displays some errors

    As we identified from the image above, it displays some errors/violations; more specifically, 33 accessibility violations were found. We can see more details related to all the rules here.

    The only violation is color contrast; text elements must have sufficient color contrast against the background; as we can see, 33 elements are not following that rule.

Code Walkthrough

test-accessibility-cypress.cy.js

Let’s start defining our config.json file; here, we can include some data and URLs:

config.json

After that, we need to define our structure; as we can see on the following line of code -> import MainPage from ‘../../page-objects/components/MainPage’, we want to separate our Cypress locators from our tests, saying that this is the Page Object Model structure. We can find that under the “page-objects” folder:

page-objects folder

MainPage.js

As we mentioned above, our methods and locators can be found here; in our example, we try to do a simple search on Google.

For this case, we are using a before() hook that will open up our page before all of our tests, and after that, we can notice two tests, one related to a full-page home validation and the second one for the google search validation.

You can go through the following video from the LambdaTest YouTube channel to learn more about Cypress hooks:

You can subscribe to the channel and get the latest tutorials around Cypress testing, automated browser testing, CI/CD, and more!

And here is the test execution, which indicates that our Cypress accessibility testing approach is working:

Cypress accessibility testing approach is working

GitHub

One thing I forgot to mention during our test using “cy.origin” to use this experimental command, we must enable it from our cypress configuration file; our file must be as follows:

If we don’t enable it, Cypress will throw an error:

Cypress will throw an error

We must upgrade to Cypress 9.6.0, then we can try out the new functionality by setting the new experimentalSessionAndOrigin configuration option to true.

In the next section of this tutorial on Cypress accessibility testing, we will learn how to perform accessibility testing on the Cypress cloud grid.

How to perform Cypress Accessibility Testing on the cloud grid?

We can use a Cypress cloud grid like LambdaTest, which provides automated cross browser testing on 40+ browsers and operating systems, and Cypress parallel testing to expedite the test execution and help perform Cypress testing at scale. In addition, it will help improve our overall test coverage by resulting in better product quality as we can cover different combinations using the same test scripts.

To get started with Cypress e2e testing, follow the below-mentioned steps:

  1. Install LambdaTest Cypress CLI on your machine. Trigger the following command to install the same:
  2. After installation is completed, set up the configuration using the below command:
  3. Once the command is completed, lambdatest-config.json is created in the project folder. Next, enter the LambdaTest credentials from the LambdaTest Profile Section.
  4. Here is how you can configure the required browser & OS combinations in lambdatest-config.json:
  5. The run_settings section in the JSON file contains the desired Cypress test suite capabilities, including Cypress_version, build_name, number of parallel sessions, etc.
  6. Tunnel_settings in the JSON file lets you connect your local system with LambdaTest servers via an SSH-based integration tunnel. Once this tunnel is established, you can test locally hosted pages on all the browsers currently supported by Cypress on LambdaTest.
  7. Now that the setup is ready, it’s time to run the tests; remember that our run_settings file displays the parallels field as five once we trigger our execution in parallel without any extra parameter. We must consider that the code used in the earlier test remains unchanged when we use the Cloud Grid.
  8. Shown below is the test execution status from the LambdaTest Automation Dashboard.

LambdaTest Automation Dashboard

To view test performance metrics, navigate to the LambdaTest Analytics Dashboard. The Test Overview will provide a snapshot of tests consistent with stable behavior. Meanwhile, the Test Summary will display the total number of tests passed or failed and any completed and pending tests.

Test Summary

If you are a developer or a tester and have a basic understanding of Cypress and want to take your knowledge to the next level, then this Cypress 101 certification course is for you.

Here’s a short glimpse of the Cypress 101 certification from LambdaTest:

Final Thoughts

It’s important to ensure your website is accessible to all visitors, including those who have disabilities. In this Cypress tutorial, we learned the importance of accessibility testing and how to perform Cypress accessibility testing on a cloud grid.

Let’s include Accessibility Testing in our projects; Accessibility is easy to consider once you start caring about it. The importance here is to embrace accessibility as part of our Automation testing. Let’s keep applications accessible to as many people as possible.

“We must guarantee what we deliver works for everyone.” – Enrique A Decoss

My suggestion to all Testers is to consider accessibility in your test automation projects; we should work together and keep applications available to all. Accessibility can help us to generate a real impact.

To deepen your Cypress automation expertise, don’t miss our comprehensive guide on the top Cypress interview questions packed with insights to help you shine in your next interview.

Happy Bug Hunting!

Frequently Asked Questions (FAQs)

Is Cypress end to end testing?

Cypress is a great tool for e2e testing, especially for those who want to create useful tests with very little effort. The live preview makes it easy to see what you changed in the screenshots and snapshots. This enables you to debug issues with ease, so you can get bugs fixed quicker than ever before.

What is accessibility testing?

Accessibility testing is a practice of making websites and mobile apps usable to as many people as possible, including those with impaired vision or hearing. It is particularly useful for anyone in your audience who has a physical or cognitive condition that would otherwise prevent them from using typical interfaces.

Can we automate accessibility testing?

The automated accessibility testing tool identifies common accessibility errors for you to correct, but it does not guarantee that your site is accessible. Always combine automated testing with ongoing manual testing.

Author

Enrique DeCoss, Senior Quality Assurance Manager at FICO is an industry leader in quality strategy with 16+ of experience implementing automation tools. Enrique has a strong background in Testing Tools, API testing strategies, performance testing, Linux OS and testing techniques. Enrique loves to share his in-depth knowledge in competencies including, Selenium, JavaScript, Python, ML testing tools, cloud computing, agile methodologies, and people Management.

Blogs: 11

linkedintwitter