Tuesday, 1 August 2023

How to Write Accessibility Tests in Playwright?

We can identify many type of accessibility issues in the web application using Playwright. You need to add below dependency to find accessibility issues.

<dependency>
  <groupId>com.deque.html.axe-core</groupId>
  <artifactId>playwright</artifactId>
  <version>4.5.1</version>
</dependency>

To scan entire webpage

page.navigate("https://self-learning-java-tutorial.blogspot.com/");
AxeResults accessibilityScanResults = new AxeBuilder(page).analyze();
List<Rule> violations = accessibilityScanResults.getViolations();

Find the below working application.

 

ScanEntirePage.java

package com.sample.app.accessibility;

import java.util.List;

import com.deque.html.axecore.playwright.AxeBuilder;
import com.deque.html.axecore.results.AxeResults;
import com.deque.html.axecore.results.Rule;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class ScanEntirePage {

  public static void main(String[] args) {

    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium()
          .launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
      BrowserContext context = browser.newContext();
      Page page = context.newPage();

      page.navigate("https://self-learning-java-tutorial.blogspot.com/");
      AxeResults accessibilityScanResults = new AxeBuilder(page).analyze();

      List<Rule> violations = accessibilityScanResults.getViolations();
      for (Rule violation : violations) {
        System.out.println("impact : " + violation.getImpact());
        System.out.println("description : " + violation.getDescription());
      }

    }
  }
}

To scan specific part of a page

AxeResults accessibilityScanResults = new AxeBuilder(page)
  .include(Arrays.asList("#navbar-iframe-container"))
  .analyze();

ScanSpecificPartOfAPage.java

package com.sample.app.accessibility;

import java.util.Arrays;
import java.util.List;

import com.deque.html.axecore.playwright.AxeBuilder;
import com.deque.html.axecore.results.AxeResults;
import com.deque.html.axecore.results.Rule;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class ScanSpecificPartOfAPage {

  public static void main(String[] args) {

    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium()
          .launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
      BrowserContext context = browser.newContext();
      Page page = context.newPage();

      page.navigate("https://self-learning-java-tutorial.blogspot.com/2014/11/pretty-print-json-format.html");
      AxeResults accessibilityScanResults = new AxeBuilder(page)
          .include(Arrays.asList("#navbar-iframe-container")).analyze();

      List<Rule> violations = accessibilityScanResults.getViolations();
      for (Rule violation : violations) {
        System.out.println("impact : " + violation.getImpact());
        System.out.println("description : " + violation.getDescription());
      }

    }
  }
}

Scanning for WCAG violations

AxeResults accessibilityScanResults = new AxeBuilder(page)
  .withTags(Arrays.asList("wcag2a", "wcag2aa", "wcag21a", "wcag21aa"))
  .analyze();

ScanWcagViolations.java

package com.sample.app.accessibility;

import java.util.Arrays;
import java.util.List;

import com.deque.html.axecore.playwright.AxeBuilder;
import com.deque.html.axecore.results.AxeResults;
import com.deque.html.axecore.results.Rule;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class ScanWcagViolations {

  public static void main(String[] args) {

    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium()
          .launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
      BrowserContext context = browser.newContext();
      Page page = context.newPage();

      page.navigate("https://self-learning-java-tutorial.blogspot.com/2014/11/pretty-print-json-format.html");
      AxeResults accessibilityScanResults = new AxeBuilder(page)
          .withTags(Arrays.asList("wcag2a", "wcag2aa", "wcag21a", "wcag21aa")).analyze();

      List<Rule> violations = accessibilityScanResults.getViolations();
      for (Rule violation : violations) {
        System.out.println("impact : " + violation.getImpact());
        System.out.println("description : " + violation.getDescription());
        System.out.println("description : " + violation.getTags() + "\n");
      }

    }
  }
}

To exclude known elements from the scan

AxeResults accessibilityScanResults = new AxeBuilder(page)
  .exclude(Arrays.asList("#element-with-known-issue"))
  .analyze();

References

https://github.com/dequelabs/axe-core/blob/develop/doc/API.md#axe-core-tags

https://playwright.dev/java/docs/accessibility-testing#example-accessibility-tests


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment