AxeRunContext Class

Link to AxeRunContext Class copied to clipboard

Configure the context for accessibility testing with axe Watcher by including or excluding DOM elements

Free Trial
Not for use with personal data

The AxeRunContext class allows you to define the scope of accessibility testing by specifying which DOM elements to include or exclude during analysis. This gives you greater control over the testing process by focusing the analysis on specific parts of the page or excluding irrelevant sections.

When you include elements, only those elements will be analyzed. When you exclude elements, those elements will be excluded from the analysis, but everything else on the page will be analyzed.

Constructor

AxeRunContext()

Creates a new instance of AxeRunContext with empty include and exclude lists.

AxeRunContext context = new AxeRunContext();

Methods

setInclude(List<String> include)

Specifies which elements to include in the accessibility analysis. When you set include elements, only those elements will be analyzed.

Parameters:

  • include - List of CSS selectors to include in the analysis

Returns:

  • AxeRunContext - The current instance for method chaining

Example:

AxeRunContext context = new AxeRunContext();
context.setInclude(Arrays.asList("#main-content", ".important-section"));

setExclude(List<String> exclude)

Specifies which elements to exclude from the accessibility analysis.

Parameters:

  • exclude - List of CSS selectors to exclude from the analysis

Returns:

  • AxeRunContext - The current instance for method chaining

Example:

AxeRunContext context = new AxeRunContext();
context.setExclude(Arrays.asList(".ad-section", "#header-navigation"));

getInclude()

Gets the list of CSS selectors that are included in the analysis.

Returns:

  • List<String> - The list of included CSS selectors

Example:

AxeRunContext context = new AxeRunContext();
context.setInclude(Arrays.asList("#main-content"));
List<String> includeSelectors = context.getInclude();

getExclude()

Gets the list of CSS selectors that are excluded from the analysis.

Returns:

  • List<String> - The list of excluded CSS selectors

Example:

AxeRunContext context = new AxeRunContext();
context.setExclude(Arrays.asList(".ad-section"));
List<String> excludeSelectors = context.getExclude();

toJson()

Serializes the AxeRunContext instance to a JSON string.

Returns:

  • String - A JSON string representation of the context

Example:

AxeRunContext context = new AxeRunContext()
    .setInclude(Arrays.asList("#main-content"))
    .setExclude(Arrays.asList(".ad-section"));
String json = context.toJson();
// Returns: {"include":["#main-content"],"exclude":[".ad-section"]}

See Also