npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jsxtools/playwright-getbycomputedrole

v0.1.0

Published

A Playwright extension that adds a `getByComputedRole` method for locating elements by their computed ARIA roles, including roles from Shadow DOM and ElementInternals.

Downloads

14

Readme

@jsxtools/playwright-getbycomputedrole

A Playwright extension that adds a getByComputedRole method for locating elements by their computed ARIA roles, including roles from Shadow DOM and ElementInternals.

Features

  • Computed Role Detection: Finds elements by their actual computed ARIA role, not just explicit role attributes.
  • Shadow DOM Support: Works with elements inside Shadow DOM boundaries.
  • ElementInternals Support: Handles roles set via ElementInternals API.
  • Accessible Name Matching: Filter by accessible name, description, and other ARIA properties.
  • W3C Compliant: Follows W3C Accessible Name and Description Computation specification.
  • Type Safe: Full TypeScript support with enhanced locator types.

Installation

npm install @jsxtools/playwright-getbycomputedrole

Usage

Basic Setup

Import and use the extended test from this package instead of the default Playwright test:

import { test, expect } from '@jsxtools/playwright-getbycomputedrole';

test('find button by computed role', async ({ page }) => {
  await page.goto('https://example.com');

  // Find element by computed ARIA role
  await page.getByComputedRole('button').click();

  // Find with accessible name
  await page.getByComputedRole('button', { name: 'Submit' }).click();

  // Find with description
  await page.getByComputedRole('textbox', {
    name: 'Email',
    description: 'Enter your email address'
  }).fill('[email protected]');
});

Advanced Usage

import { test, expect } from '@jsxtools/playwright-getbycomputedrole';

test('complex role matching', async ({ page }) => {
  await page.goto('https://example.com');

  // Find elements with exact name matching
  const submitButton = page.getByComputedRole('button', {
    name: 'Submit Form',
    exact: true
  });

  // Chain with other locators
  const formSection = page.locator('form')
    .getByComputedRole('textbox', { name: 'Username' });

  // Use with expect assertions
  await expect(page.getByComputedRole('alert')).toBeVisible();

  // Find elements in Shadow DOM
  const shadowButton = page.getByComputedRole('button', {
    name: 'Shadow Button'
  });
});

Working with Custom Elements

This package is particularly useful for testing custom elements that use ElementInternals or Shadow DOM:

test('custom element with ElementInternals', async ({ page }) => {
  await page.goto('/custom-elements');

  // Custom element that sets role via ElementInternals
  await page.getByComputedRole('slider', { name: 'Volume' }).click();

  // Custom element in Shadow DOM
  await page.getByComputedRole('tab', { name: 'Settings' }).click();
});

API Reference

page.getByComputedRole(role, options?)

Locates elements by their computed ARIA role.

Parameters:

  • role: The ARIA role to search for (e.g., 'button', 'textbox', 'heading')
  • options: Optional configuration object

Options:

  • name?: string: Match by accessible name
  • description?: string: Match by accessible description
  • exact?: boolean: Whether to match name/description exactly (default: false)

Returns:

  • Locator: A Playwright locator for the matching element(s)

locator.getByComputedRole(role, options?)

Same as the page method, but scoped to the current locator.

How It Works

This package extends Playwright by:

  1. Registering a custom selector engine that computes ARIA roles according to W3C specifications.
  2. Tracking Shadow DOM and ElementInternals through monkey-patching during page initialization.
  3. Computing accessible names and descriptions following the Accessible Name and Description Computation spec.
  4. Providing enhanced locator methods that work seamlessly with Playwright's existing API.

Differences from getByRole

While Playwright's built-in getByRole method works well for explicit roles, getByComputedRole provides additional capabilities:

| Feature | getByRole | getByComputedRole | |---------|-------------|---------------------| | Explicit role attributes | ✅ | ✅ | | Implicit HTML roles | ✅ | ✅ | | ElementInternals roles | ❌ | ✅ | | Shadow DOM computation | ❌ | ✅ |

License

MIT-0