Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readTesting

Assert roles and names with toHaveRole

Use Playwright to check a button’s computed role, accessible name and description.

Your end-to-end test finds the close button by its test id, checks that it exists, and passes. A screen reader user opens the same dialog and hears "button" with no name.

A test id or a CSS selector locates an element. It says nothing about what assistive technology announces for it. Playwright’s locator assertions check the computed role, name or description on a locator you already have. Screen readers can announce those properties in different ways.

Assert the name

Save this as close.spec.ts in a Playwright Test project. The dialog’s close button contains only an icon.

import { test, expect } from '@playwright/test'

test('close button is announced as Close dialog', async ({ page }) => {
  await page.setContent(`
    <div role="dialog" aria-labelledby="title">
      <h2 id="title">Delete project</h2>
      <button data-testid="close"><svg width="16" height="16"></svg></button>
    </div>
  `)

  const close = page.getByTestId('close')
  await expect(close).toHaveRole('button')
  await expect(close).toHaveAccessibleName('Close dialog')
})

Run npx playwright test and the role line passes. The name line waits out its 5 second timeout and fails.

Expected: "Close dialog"
Received: ""

An empty received string means the button has no accessible name. Add aria-label="Close dialog" to the <button> and the test passes.

The assertion retries until its timeout, as the call log shows. A name that your script sets a second after render still passes, so you don't need a manual wait before it.

Assert the description

The name says what a control is. In this example, aria-describedby points to the text that supplies the accessible description.

// same file, same imports
test('delete button explains what it does', async ({ page }) => {
  await page.setContent(`
    <button data-testid="delete" aria-describedby="warning">Delete</button>
    <p id="warning">This removes the project and cannot be undone.</p>
  `)

  const del = page.getByTestId('delete')
  await expect(del).toHaveRole('button')
  await expect(del).toHaveAccessibleName('Delete')
  await expect(del).toHaveAccessibleDescription('This removes the project and cannot be undone.')
})

All three assertions pass. The text inside the button supplies its name, and the referenced paragraph supplies its description.

Two details

toHaveAccessibleName and toHaveAccessibleDescription take a string or a regular expression, plus an ignoreCase option. I use a string for labels I wrote, and a regular expression when part of the text comes from data.

toHaveRole compares the role as a string and ignores the ARIA role hierarchy. Expecting checkbox on an element with role="switch" fails, so assert the specific role the element has. Put the role assertion first.

If the element is a div where you expected a button, that failure points at the real problem before the name assertion times out. All three assertions arrived in Playwright 1.44.