Skip to main content

Ensuring Inclusivity: A Practical Guide to Accessibility Testing with Playwright

 


In today's digital-first world, your web application isn't truly "done" unless it's accessible to everyone. Accessibility testing (often shortened to A11y testing) ensures that your software can be used by people with a wide range of abilities and disabilities, including visual impairments, hearing loss, motor difficulties, and cognitive disabilities. Beyond legal compliance (like WCAG guidelines), building accessible applications means reaching a broader audience, enhancing user experience for all, and demonstrating ethical design.

While manual accessibility testing (e.g., using screen readers, keyboard navigation) is crucial, automating parts of it can significantly accelerate your efforts and catch common issues early. This is where Playwright, a modern and powerful web automation framework, combined with dedicated accessibility tools, comes in.

This guide will provide a practical approach to integrating automated accessibility checks into your Playwright test suite.

Why Accessibility Testing Matters

  • Legal Compliance: Laws like the Americans with Disabilities Act (ADA) in the US, the European Accessibility Act, and WCAG (Web Content Accessibility Guidelines) set standards for digital accessibility. Non-compliance can lead to significant legal repercussions.

  • Wider User Base: Globally, over a billion people live with some form of disability. An inaccessible website excludes a substantial portion of potential users.

  • Improved User Experience: Features designed for accessibility (e.g., clear navigation, proper headings, keyboard support) often benefit all users, not just those with disabilities.

  • SEO Benefits: Many accessibility best practices (like proper semantic HTML, alt text for images) also contribute positively to Search Engine Optimization.

  • Ethical Responsibility: Building inclusive products is simply the right thing to do.

The Role of Automation vs. Manual Testing in A11y

It's important to understand that automated accessibility testing cannot catch all accessibility issues. Many problems, especially those related to cognitive load, user flow, or assistive technology compatibility, require manual accessibility testing and even testing by real users with disabilities.

However, automated tools are excellent at catching a significant percentage (often cited as 30-50%) of common, programmatic errors quickly and consistently. They are best for:

  • Missing alt text for images

  • Insufficient color contrast

  • Missing form labels

  • Invalid ARIA attributes

  • Structural issues (e.g., empty headings)

Automated tests allow you to shift-left testing for accessibility, finding issues early in the development cycle, when they are cheapest and easiest to fix.

Integrating Axe-core with Playwright for Automated A11y Checks

The most popular and effective tool for automated accessibility scanning is Axe-core by Deque Systems. It's an open-source library that powers accessibility checks in tools like Lighthouse and Accessibility Insights. Playwright integrates seamlessly with Axe-core via the @axe-core/playwright package.

Step 1: Set up your Playwright Project

If you don't have a Playwright project, set one up:

Bash
npm init playwright@latest
# Choose TypeScript, add examples, etc.

Step 2: Install Axe-core Playwright Package

Install the necessary package:

Bash
npm install @axe-core/playwright axe-html-reporter
  • @axe-core/playwright: The core library to run Axe-core with Playwright.

  • axe-html-reporter: (Optional but highly recommended) Generates beautiful, readable HTML reports for accessibility violations.

Step 3: Write Your First Accessibility Test

Let's create a simple test that navigates to a page and runs an Axe scan.

Create a new test file, e.g., tests/accessibility.spec.ts:

TypeScript
import { test, expect } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';
import { createHtmlReport } from 'axe-html-reporter';
import * as fs from 'fs';
import * as path from 'path';

test.describe('Accessibility Testing', () => {

  test('should not have any automatically detectable accessibility issues on the homepage', async ({ page }, testInfo) => {
    await page.goto('https://www.google.com'); // Replace with your application's URL

    // Run Axe-core scan
    const accessibilityScanResults = await new AxeBuilder({ page })
      .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'best-practice']) // Define WCAG standards and best practices
      .analyze();

    // Generate HTML report for detailed violations
    if (accessibilityScanResults.violations.length > 0) {
      const reportDir = 'test-results/a11y-reports';
      const reportFileName = `${testInfo.title.replace(/[^a-zA-Z0-9]/g, '_')}_${testInfo.workerIndex}.html`;
      const reportPath = path.join(reportDir, reportFileName);

      if (!fs.existsSync(reportDir)) {
        fs.mkdirSync(reportDir, { recursive: true });
      }

      createHtmlReport({
        results: accessibilityScanResults,
        options: {
          outputDir: reportDir,
          reportFileName: reportFileName,
        },
      });
      console.log(`Accessibility report generated: ${reportPath}`);
      testInfo.attachments.push({
        name: 'accessibility-report',
        contentType: 'text/html',
        path: reportPath
      });
    }

    // Assert that there are no accessibility violations
    expect(accessibilityScanResults.violations).toEqual([]);
  });

  test('should not have accessibility issues on a specific element (e.g., form)', async ({ page }) => {
    await page.goto('https://www.example.com/contact'); // Replace with a page with a form

    const accessibilityScanResults = await new AxeBuilder({ page })
      .include('form#contact-form') // Scan only a specific element
      .withTags(['wcag2a', 'wcag2aa'])
      .analyze();

    expect(accessibilityScanResults.violations).toEqual([]);
  });
});

Step 4: Run Your Tests

Bash
npx playwright test tests/accessibility.spec.ts

If violations are found, the test will fail, and an HTML report will be generated in test-results/a11y-reports showing the exact issues, their WCAG criteria, and suggested fixes.

Advanced Accessibility Testing Strategies with Playwright

  1. Scanning Specific Elements (.include() / .exclude()): Focus your scan on a particular component or exclude known inaccessible third-party widgets.

    TypeScript
    await new AxeBuilder({ page }).include('#my-component').analyze();
    await new AxeBuilder({ page }).exclude('.third-party-widget').analyze();
    
  2. Configuring Rules and Standards (.withTags() / .disableRules()): Specify which WCAG standards (e.g., wcag2aa for Level AA, wcag21a for WCAG 2.1 Level A) or best practices to include, or temporarily disable specific rules.

    TypeScript
    // Check for WCAG 2.1 Level AA and best practices
    .withTags(['wcag21aa', 'best-practice'])
    // Disable a specific rule (e.g., for known, accepted issues)
    .disableRules(['color-contrast'])
    
  3. Integrating into E2E Flows: Instead of separate tests, run accessibility scans at crucial points within your existing end-to-end functional tests (e.g., after navigating to a new page, after a modal opens).

    TypeScript
    test('User registration flow should be accessible', async ({ page }) => {
      await page.goto('/register');
      await expect(new AxeBuilder({ page }).analyze()).resolves.toHaveNoViolations(); // Initial page check
    
      await page.fill('#username', 'testuser');
      await page.fill('#password', 'password');
      await page.click('button[type="submit"]');
    
      await page.waitForURL('/dashboard');
      await expect(new AxeBuilder({ page }).analyze()).resolves.toHaveNoViolations(); // Dashboard check
    });
    
  4. CI/CD Integration: Automate these accessibility checks to run with every code commit or nightly build. This ensures continuous quality and helps catch regressions early in your DevOps pipeline. Playwright's integration with CI tools makes this straightforward.

Limitations of Automated A11y Testing

Remember, automation is a powerful first line of defense, but it doesn't replace human judgment:

  • Contextual Issues: Automated tools can't determine if the purpose of a link is clear to a user or if the reading order makes sense.

  • Complex Interactions: They struggle with scenarios requiring user intent, like complex form workflows or keyboard-only navigation for custom components.

  • Assistive Technology Compatibility: True compatibility with screen readers, braille displays, etc., requires manual testing with those devices.

Therefore, a truly robust accessibility testing strategy combines automated checks (for speed and coverage of common issues) with expert manual reviews and, ideally, user testing with individuals with disabilities.

Conclusion: Building a More Inclusive Web

Integrating automated accessibility testing with Playwright using tools like Axe-core is a crucial step towards building inclusive and compliant web applications. By making A11y a consistent part of your continuous testing efforts and shifting quality left, you can proactively identify and resolve issues, reduce your test maintenance burden, and ultimately deliver a better experience for every user. Start making accessibility a core part of your quality strategy today!

Comments

Popular posts from this blog

Principles of Software Testing

๐Ÿงช The 7 Principles of Software Testing – A Deep-Dive for Beginners & Experts Published by QA Cosmos | June 28, 2025 ๐Ÿ‘‹ Introduction Hello QA enthusiasts! Today we're diving into the seven timeless principles of software testing , which form the foundation of all QA practices—be it manual or automated. Understanding these principles helps you: Write smarter tests Find bugs effectively Communicate professionally with your team Build software that users love This guide is packed with simple explanations, relatable examples, and hands-on tips. Whether you’re fresh to QA or polishing your skills, these principles are essential. Let’s begin! 1. Testing Shows Presence of Defects ✅ Principle: Testing can prove the presence of defects, but cannot prove that there are no defects. ๐Ÿง  What It Means: No matter how many flawless tests you run, you can never guarantee a bug-free application. Testing helps find bugs—but not confirm total correctness. ๐Ÿ› ️ Example: Y...

Selenium vs. Playwright: A Deep Dive into Waiting Concepts

  In the world of web automation, "waiting" is not just a pause; it's a strategic synchronization mechanism. Web applications are dynamic: elements appear, disappear, change state, or load asynchronously. Without proper waiting strategies, your automation scripts will frequently fail with "element not found" or "element not interactable" errors, leading to flaky and unreliable tests. Let's explore how Selenium and Playwright approach this fundamental challenge. The Challenge: Why Do We Need Waits? Imagine a user interacting with a webpage. They don't click a button the exact instant it appears in the HTML. They wait for it to be visible, stable, and ready to receive clicks. Automation tools must mimic this human behavior. If a script tries to interact with an element before it's fully loaded or clickable, it will fail. Waits bridge the gap between your script's execution speed and the web application's loading time. Selenium'...

Top 50 Manual Testing Interview

  Top 50 Manual Testing Interview Questions and Answers (2025 Edition) Your ultimate guide to cracking QA interviews with confidence! Manual testing remains a critical skill in the software industry. Whether you're a fresher or an experienced tester, preparing for interviews with a strong set of  common and real-world questions  is essential. This blog gives you  50 hand-picked manual testing questions  with  simple, clear answers , based on real interview scenarios and ISTQB fundamentals. ๐Ÿ”ฅ  Core Manual Testing Interview Questions & Answers 1.  What is software testing? Answer:  Software testing is the process of verifying that the software works as intended and is free from defects. It ensures quality, performance, and reliability. 2.  What is the difference between verification and validation? Answer: Verification : Are we building the product right? (Reviews, walkthroughs) Validation : Are we building the right product? (Testing...