Accessibility is an essential aspect of a website. To make a website accessible, you need to properly design and code it so that people with disabilities can use it conveniently. Accessibility also means ensuring that the website can load fast enough to be usable by the broadest range of people.
Puppeteer is a Node library commonly used in browser automation testing. In this shot, we will see how to use Puppeteer to perform some standard accessibility tests.
An accessible website should not rely on the mouse alone – all functionality must be available from a keyboard as well.
The basics of keyboard testing are simple: The tab key must navigate through interactive elements (links, buttons, fields for inputting text, etc.), and enter and spacebar must select or toggle elements.
To test keyboard focus and navigation order, we can run a test to ensure that pressing on the tab or arrow-down keys will move the keyboard focus to the next interactive element in the view.
In the example code below, we first fetch all row
elements, and then we check that each row
has tabIndex=0
, role=button
, and that the row
is focused. We achieve this using Puppeteer’s page.evaluate
and getAttribute
methods.
After validating those properties, we move to the next element by “pressing” the tab key or arrow-down key with page.keyboard.press(<keyName>)
.
// get all rowsconst rows = await page.$$(`.rows`);for (let row of rows) {// validate role, tab index and focus.expect(await page.evaluate(elem => elem.getAttribute(`role`), row)).toEqual(`button`);expect(await page.evaluate(elem => elem.getAttribute(`tabindex`), row)).toEqual(`0`);expect(await page.evaluate(elem => window.document.activeElement === elem, row)).toEqual(true);// move to the next element (click on tab or arrow down button)await page.keyboard.press(i % 2 == 0 ? `Tab` : `ArrowDown`);}
Similar to the tab and spacebar keys navigation test, we can test if the spacebar key toggles the view state.
We do this by navigating to a category. Then, we test the accessibility properties (e.g., tabIndex
, role
, aria-expanded
). Finally, we press the spacebar key; it should toggle the property aria-expanded
from true to false, and vice versa.
const category = await page.$(`.category`);// verify tabIndex, role and focusexpect(await page.evaluate(elem => elem.getAttribute(`role`), category)).toEqual(`button`);expect(await page.evaluate(elem => elem.getAttribute(`tabindex`), category)).toEqual(`0`);expect(await page.evaluate(elem => window.document.activeElement === elem, category)).toEqual(true);// verify aria-expanded = falseexpect(await page.evaluate(elem => elem.getAttribute(`aria-expanded`), category)).toEqual(`false`);// toggle category by press spaceawait page.keyboard.press('Space');// verify aria-expanded = trueexpect(await page.evaluate(elem => elem.getAttribute(`aria-expanded`), category)).toEqual(`true`);
Puppeteer is a browser automation tool and may not be suitable for extensive accessibility testing. As an alternative, a widely recommended accessibility testing tool is axe. You can check it out here.
Unlock your potential: Puppeteer fundamentals series, all in one place!
If you've missed any part of the series, you can always go back and check out the previous Answers:
What is Puppeteer?
Learn about Puppeteer, a Node.js library that provides a high-level API for browser automation using headless Chrome or Chromium.
How to check for the browser version in Puppeteer
Discover how to retrieve the browser version using Puppeteer's browser.version()
method.
How to open the browser in headful mode with Puppeteer
Explore how to launch a visible browser instance by disabling the headless mode in Puppeteer.
How to get web page HTML with Puppeteer
Learn how to extract and manipulate a webpage’s HTML content using Puppeteer’s evaluate()
method.
What is the use of the setViewport method in Puppeteer?
Understand how setViewport()
customizes the browser’s viewport size for responsive testing and screenshots.
What is code coverage in Puppeteer?
Learn how to analyze unused JavaScript and CSS in web pages to optimize performance using Puppeteer’s coverage tool.
What is visual regression testing in Puppeteer?
Discover how Puppeteer can capture and compare screenshots to detect visual changes in web applications.
What is an accessibility test in Puppeteer?
Explore how Puppeteer, combined with accessibility tools like axe-core
, helps evaluate web accessibility compliance.
Free Resources