API Reference for Java Playwright

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard

API reference for the AxePlaywrightBuilder class in the Axe DevTools for Web Java Playwright integration

Not for use with personal data

This document provides an API reference to the AxePlaywrightBuilder Java class, which is provided by the Playwright Java integration.

note

Most methods in the AxePlaywrightBuilder class return an AxePlaywrightBuilder object, which allows you to easily chain several methods together. See the code samples below for examples of chaining methods.

Constructors

Constructor Description
AxePlaywrightBuilder(Page) Standard constructor that creates an AxePlaywrightBuilder object.
AxePlaywrightBuilder(Page, File) Create an AxePlaywrightBuilder object and specify a JSON file containing custom rules to be used during analysis.
AxePlaywrightBuilder(Page, String) Create an AxePlaywrightBuilder object while specifying a JSON String containing custom rules.

AxePlaywrightBuilder(Page)

Initializes a new instance of the AxePlaywrightBuilder class for the specified Page object.

public AxePlaywrightBuilder(Page page);

Parameters

Name Type Description
page com.microsoft.Playwright.Page The Page object to be used for accessibility analysis.

Example

The following code example demonstrates this constructor. It also shows how to create a Page object and an AxePlaywrightBuilder object with that Page object.

// Example Page
Page page = browser.newPage();

AxePlaywrightBuilder axePlaywrightBuilder = new AxePlaywrightBuilder(page);

AxePlaywrightBuilder(Page, File)

Initializes a new instance of AxePlaywrightBuilder class with the specified Page object and specified File object. The File contains custom rules in a JSON file.

public AxePlaywrightBuilder(Page page, File rules);

Parameters

Name Type Description
page com.microsoft.Playwright.Page The required Page object.
rules File A File object representing a JSON file of custom accessibility rules.

Example

The following example shows how to use this constructor.

// Example Page
Page page = browser.newPage();

File customRuleset = new File("somePath/custom-ruleset.json")
AxePlaywrightBuilder axePlaywrightBuilder = new AxePlaywrightBuilder(page, customRuleset);

AxePlaywrightBuilder(Page, String)

Initializes a new instance of AxePlaywrightBuilder class with the specified Page object and specified String object, which contains custom rules in a JSON string.

public AxePlaywrightBuilder(Page page, String rules);

Parameters

Name Type Description
page com.microsoft.Playwright.Page The required Page object
rules String A JSON string representing a set of custom accessibility rules.

Example

The following code example demonstrates this constructor.

// Example Page
Page page = browser.newPage();

String customRuleset = "{...}"
AxePlaywrightBuilder axePlaywrightBuilder = new AxePlaywrightBuilder(page, customRuleset);

Methods

Method Description
analyze Analyze the Playwright page and return an AxeResults object from the completed analysis.
configure(File) Provide a custom configuration on how axe-core is run via a JSON file.
configure(String) Provide a custom configuration on how axe-core is run via a JSON string.
disableRules Prevents the specified rules from being run during analysis.
exclude(String) Specifies a single CSS selector to exclude certain HTML content during analysis.
exclude(List<String>) Specifies a CSS iframe selector to be used to exclude certain HTML content during analysis.
exclude(FromFrames) Excludes an element inside one or more nested iframes.
exclude(FromShadowDom) Excludes an element inside one or more nested shadow DOM trees.
exclude(Object) Excludes a selector supplied as a single object.
exclude(Object...) Excludes a selector that combines CSS selectors, frame context, and shadow DOM context.
include(String) Specifies a single CSS selector that includes HTML content during analysis.
include(List<String>) Specifies a CSS iframe selector that includes HTML content during analysis.
include(FromFrames) Includes an element inside one or more nested iframes.
include(FromShadowDom) Includes an element inside one or more nested shadow DOM trees.
include(Object) Includes a selector supplied as a single object.
include(Object...) Includes a selector that combines CSS selectors, frame context, and shadow DOM context.
setLegacyMode Set legacy mode to exclude accessibility issues that may occur in cross-domain frames and iframes.
withAxeSource(File) Provide a custom version of axe-core using a File object.
withAxeSource(String) Provide a custom version of axe-core by using a String object.
withOnlyBestPracticeRules Checks accessibility using only the best-practice rules.
withOnlyExperimentalRules Checks accessibility using only the experimental rules.
withRules Limit the accessibility rules to the ones specified.
withTags Limit the accessibility rules checked to the specified list of tags.

analyze

Analyze the Playwright Page (specified when the AxePlaywrightBuilder object was created) and return an AxeResults object from the completed analysis.

AxeResults AxePlaywrightBuilder.analyze();

Returns

  • AxeResults

Example

The following code example demonstrates how to use the analyze method.


AxePlaywrightBuilder axePlaywrightBuilder = new AxePlaywrightBuilder(page);

AxeResults axeResults = axePlaywrightBuilder.analyze();

/* Usage may include:
axeResults.getViolations() - returns only violation results
axeResults.getPasses() - returns only pass results
axeResults.getIncomplete() - returns only incomplete results
*/

configure(File)

Provide a custom configuration to axe-core via a JSON file.

public AxePlaywrightBuilder configure(File axeConfigure);

For more information about the format of the configuration file, see axe-core configuration.

Parameters

Name Type Description
file File A File object representing an axe-core configuration in JSON.

Example

The following example shows how to access a configuration and use it with an AxePlaywrightBuilder object.

File myAxeConfigure = new File("./axe-configure.json");

AxePlaywrightBuilder axePlaywrightBuilder = new AxePlaywrightBuilder(page)
        .configure(myAxeConfigure);

configure(String)

Provide a custom configuration to axe-core (the underlying technology for analyzing accessibility) is run via a JSON string.

public AxePlaywrightBuilder configure(String axeConfigure);

For more information about the format of the configuration file, see axe-core configuration.

Parameters

Name Type Description
axeConfig String A String object representing an axe-core configuration in JSON.

Example

The following example shows how to access a configuration and use it with an AxePlaywrightBuilder object.

String myAxeConfigure = "{..}"

AxePlaywrightBuilder axePlaywrightBuilder = new AxePlaywrightBuilder(page)
        .configure(myAxeConfigure);

disableRules

Disable rules from being executed during analysis.

public AxePlaywrightBuilder disableRules(List<String> rules);

Parameters

Name Type Description
rules List<String> A list of strings representing rules to disable during analysis.

Returns

  • AxePlaywrightBuilder

Example

The following example shows how to disable accessibility checking for a single rule using the singletonList method of the Collections class and multiple rules using a List.

// Single Rule
AxePlaywrightBuilder axePlaywrightBuilder1 = new AxePlaywrightBuilder(page)
        .disableRules(Collections.singletonList("color-contrast"));

// Multiple Rules
AxePlaywrightBuilder axePlaywrightBuilder2 = new AxePlaywrightBuilder(page)
        .disableRules(Arrays.asList("color-contrast",