Accessibility Testing Using Playwright
Tags: agilewebandappdevelo
Published: 2023-10-25 10:46:34

---
title: Accessibility Testing Using Playwright
published: true
date: 2023-10-09 09:39:10 UTC
tags: Agilewebandappdevelo
canonical_url: https://apiumhub.com/tech-blog-barcelona/accessibility-testing-playwright/
---

Today I’d like to talk about accessibility testing using Playwright.

Accessibility testing refers to evaluating web applications or websites for compliance with accessibility standards, ensuring that they can be used by people with disabilities. Existing guidelines and other standards related to web accessibility Here are the links to them:

- https://w3.org/WAI/standards-guidelines/
- https://w3c.github.io/silver/guidelines/

The Accessibility Tree is used by assistive technology to augment or replace the existing UI for applications, as well as translate operations performed by the user into something that the application understands.

[![image0](https://apiumhub.com/wp-content/uploads/2023/10/image0-1024x517.png "Accessibility Testing Using Playwright 1")](https://blogs.intuit.com/2021/01/12/why-the-accessibility-tree-is-key-to-understanding-accessible-applications/)
_Resource: Intuit Blog_

Playwright provides built-in support for accessibility testing by exposing methods and properties that allow you to interact with and inspect the accessibility features of web elements. You can use Playwright’s API to retrieve information about an element’s accessibility properties, such as its role, name, state, and description. This allows you to programmatically verify if the web application or website meets the required accessibility criteria. Additionally, Playwright has support for generating accessibility reports. You can use the page.accessibility.snapshot() method to capture a snapshot of the accessibility tree of a page. The Playwright site already has detailed documentation on using Accessibility for tests, so I won’t duplicate everything here, but I’d like to focus on the bottlenecks and possible solutions to some problems that I had on one of my projects.

[![CTA Software](https://apiumhub.com/wp-content/uploads/2019/09/CTA_Software-1024x293.jpg "Accessibility Testing Using Playwright 2")](https://apiumhub.com/contact-software-developers-barcelona/?utm_source=blog&utm_medium=banner&utm_campaign=softwaredevelopment)

## Basics

To test Accessibility using Playwright, you need to additionally install the @axe-core/playwright library, with the help of which scanning of the webpage will take place.  
[Library of axe-core](https://github.com/dequelabs/axe-core)  
Axe is an accessibility testing engine for websites and other HTML-based user interfaces. It’s fast, secure, lightweight, and was built to seamlessly integrate with any existing test environment so you can automate accessibility testing alongside your regular functional testing.

```
npm i @axe-core/playwright 
```

![1image](https://apiumhub.com/wp-content/uploads/2023/10/1image-1024x402.png "Accessibility Testing Using Playwright 3")

As you can see, everything happens in 3 steps in the test:  
1) open the desired page  
2) scanning using AxBuilder()  
3) comparison of obtained and expected results  
It is important to understand that the scanning is based on the principle of photography: if the page has modal windows or drop-down lists, they must first be opened to test. In addition, it is worth considering that the scan results contain not only violations, so for a correct comparison, you need to pass violations to `expect()`. If there is a violation, after the test we will receive a report with detailed information about the violated rules and places where it happened, as well as information about possible solutions.

## Configuration of the analyzer

To get the optimal effect from testing, the analysis can be configured quite flexibly. This effect is achieved by using methods that allow you to set the level of recommendations or a set of rules, correcting them by excluding irrelevant rules from the check.

It is also convenient to exclude a certain part of the page from the test and test only the part that is necessary. For myself, I chose the following configuration, wherein `include()` depending on the specific test, the required selector or body will be passed by default. Playwright offers a number of sophisticated tools that let you run more complex [a11y](https://github.com/a11yproject/a11yproject.com) tests. For instance, the AxeBuilder class offers a wide range of axe setup configs. These settings can be specified by utilizing a Builder pattern.

![2image](https://apiumhub.com/wp-content/uploads/2023/10/2image-1024x374.png "Accessibility Testing Using Playwright 4")

## Lighthouse for Accessibility Testing

[Lighthouse](https://chrome.google.com/webstore/detail/lighthouse/blipmdconlkpinefehnmjammfjpmpbjk?hl=es) is an open-source from Google, automated tool for improving the performance, quality, and correctness of your web apps. When auditing a page, Lighthouse runs a barrage of tests against the page and then generates a report on how well the page did. From here you can use the failing tests as indicators of what you can do to improve your app.  
To be able to use Lighthouse in Playwright tests, you can use the playwright-lighthouse package.

![3image](https://apiumhub.com/wp-content/uploads/2023/10/3image-1024x463.png "Accessibility Testing Using Playwright 5")

In my project, I have created a TS spec file, for the purpose of running directly against Lighthouse.

![4image](https://apiumhub.com/wp-content/uploads/2023/10/4image-969x1024.png "Accessibility Testing Using Playwright 6")

## Reports

Same approach as for normal test cases . Playwright supports configuration of different reports. In the case of accessibility tests, we need just pass some additional info to the test itself.

![5image](https://apiumhub.com/wp-content/uploads/2023/10/5image-1024x532.png "Accessibility Testing Using Playwright 7")

As I said before, for my current project I have a lighthouse TS spec file, which I run from the command set in package.json.

```
npm run clean && npx playwright test tests/lighthouse.spec.ts --project=chromium
```

## Conclusion

Playwright in combination with the @axe-core library is a fairly flexible and convenient tool for automated accessibility testing, although it does not cover all of its needs. As always – depends on your project needs. Playwright provides methods to access the accessibility tree of a page, allowing you to programmatically analyze and validate the properties of web elements. This makes it easier to identify and address accessibility issues.

However, it is true that the effectiveness of using any automation tool depends on who is using it, so always use it with common sense.

### References:

- Apiumhub – [Accesibility Testing](https://dev.to/apium_hub/accessibility-testing-182b)
- [W3C Accessibility Guidelines (WCAG) 3.0](https://w3c.github.io/silver/guidelines/)
- [W3C Accessibility Standards Overview](https://www.w3.org/WAI/standards-guidelines/)
- Chrome Web Store – [Lighthouse](https://chrome.google.com/webstore/detail/lighthouse/blipmdconlkpinefehnmjammfjpmpbjk?hl=es)
- Github – [A11Y Project](https://github.com/a11yproject/a11yproject.com)
- web.dev – [The Accessibility Tree](https://web.dev/the-accessibility-tree/)
- Intuit Developer – [Why the Accessibility Tree is key to understanding accessible applications](https://blogs.intuit.com/2021/01/12/why-the-accessibility-tree-is-key-to-understanding-accessible-applications/)