Accessibility Validator – Website Accessibility Check

Create Validator

Aspose.HTML for Java provides the com.aspose.html.accessibility package, which is for all web accessibility related manipulations and checks. The AccessibilityValidator class initializes an object that is used to validate the document against WCAG success criteria. The class does not have an internal constructor, therefore users cannot directly create an object of the class. To create an object, you need to use the WebAccessibility container.

To create a validator, initialize the object of the WebAccessibility class like this:

1    // Initialize webAccessibility container
2    WebAccessibility webAccessibility = new WebAccessibility();

The webAccessibility object has several overloaded createValidator() methods for creating an AccessibilityValidator instance with varying levels of customization. These methods allow validators to be created using all default rules, a specific rule, or a list of rules, and can be customized using a ValidationBuilder object. This flexible approach allows for both simple and highly customized accessibility checks.

The following Java examples demonstrate how to initialize an AccessibilityValidator for either all available rules or a specific subset based on a single accessibility principle:

Example 1. Create a Validator to Check All Rules

This example shows how to initialize an AccessibilityValidator that includes all available accessibility rules using the default comprehensive settings provided by ValidationBuilder.getAll():

1// Initialize the webAccessibility container
2WebAccessibility webAccessibility = new WebAccessibility();
3
4// Create an accessibility validator with all rules using the default settings
5AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());

Example 2. Create a Validator for a Specific Principle

In this example, a specific accessibility principle is selected using its code (e.g., “1”), and a validator is created only for the rules under that principle:

1// Retrieve a specific principle by its code
2IRule rule = webAccessibility.getRules().getPrinciple("1");
3
4// Create a validator for that principle using the same default settings
5AccessibilityValidator validatorRule = webAccessibility.createValidator(rule, ValidationBuilder.getAll());

ValidationBuilder Class

The ValidationBuilder class defines a helper class that can be used to customize the list of rules to be checked by the validator. The ValidationBuilder defines methods and provides concrete implementations of the configuration steps.

The ValidationBuilder class offers three predefined sets of properties:

The class also provides methods for customizing the validation, such as selecting specific technologies ( useHTML(), useCSS(), useScript()), setting criteria levels (useLowestLevel(), useMiddleLevel(), useHighestLevel(), allLevels()), etc.

For example, the following Java code initializes AccessibilityValidator to perform accessibility checks based on user configuration. It starts by creating a WebAccessibility container, which serves as the main access point for the validation functions. Then, using ValidationBuilder.getNone(), it creates a validator with no predefined rules. From there, it explicitly enables checks for HTML and CSS technologies and limits validation to medium conformance level (usually WCAG Level AA). This setup allows for targeted, standards-based validation that targets only selected technologies and a specific conformance level.

 1// Initialize the WebAccessibility container
 2WebAccessibility webAccessibility = new WebAccessibility();
 3
 4// Create a validator with no predefined filters, enabling HTML and CSS technologies, and selecting the middle conformance levels
 5AccessibilityValidator validatorHTML = webAccessibility.createValidator(
 6    ValidationBuilder.getNone()
 7                     .useHTML()
 8                     .useCSS()
 9                     .useMiddleLevel()
10);

ValidationResult Class

The ValidationResult class is the central component of web accessibility validation. It encapsulates the results of accessibility checks, providing a structured way to analyze, manage, and act on the results of validation operations. To use it, instantiate a validator object and invoke the validate(document) method, passing in the document to be validated. The method returns a ValidationResult object containing the validation process results.

The ValidationResult class aggregates the results for all criteria defined in the AccessibilityRules object, serving as the main container for reporting validation outcomes. It includes two key properties:

To support results export and reporting, the class provides several methods: saveTo(TextWriter) and saveTo(TextWriter, ValidationResultSaveFormat) allow saving the results to the specified output stream, while saveToString() returns the validation results as a formatted String.

A more complete description can be found in the article Validation Results – Check Web Accessibility in Java.

Validate an HTML Document

To perform document validation, follow these steps:

  1. Use the WebAccessibility() constructor to create an instance of the WebAccessibility class.
  2. Call one of the createValidator() methods to create a validator object.
  3. Load an HTML document using one of the HTMLDocument() constructors.
  4. Use the validate(document) method to validate the document against web accessibility rules.
  5. The result of the validation is an instance of the ValidationResult class.
  6. Print the validation results to the console and use them as needed for further analysis.

Here is an example of calling a document validation. The following Java code will write to the console which of the rules passed the test and which did not:

 1// Initialize a webAccessibility container
 2WebAccessibility webAccessibility = new WebAccessibility();
 3
 4// Create an accessibility validator with static instance for all rules
 5// from repository that match the builder settings
 6AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());
 7
 8// Prepare a path to a source HTML file
 9String documentPath = "input.html";
10
11// Initialize an object of the HTMLDocument class
12final HTMLDocument document = new HTMLDocument(documentPath);
13ValidationResult validationResult = validator.validate(document);
14
15// Checking for success
16if (!validationResult.getSuccess()) {
17    // Get a list of RuleValidationResult Details
18    for (RuleValidationResult detail : validationResult.getDetails()) {
19        System.out.println(String.format("%s: %s = %s",
20                detail.getRule().getCode(),
21                detail.getRule().getDescription(),
22                detail.getSuccess()));
23    }
24}
Example_ValidationResult hosted with ❤ by GitHub

See Also

  • You will find helpful tips on evaluating and improving text visibility in the article Color Contrast Accessibility, which covers contrast checking based on WCAG using Aspose.HTML for Java.
  • For instructions on checking web content is compatible with screen readers, you will find in the article Screen Reader Accessibility. You will learn how to check alt text and other key elements.
  • If you want to learn how to view validation results and identify web accessibility issues, see the Validation Results article.
  • In the article Web Accessibility Check – Errors and Warnings, you will learn how to programmatically in Java collect error and warning information while checking a website’s accessibility.

Aspose.HTML offers free online Web Accessibility Checker. This tool scans web pages, validates them for WCAG compliance, identifies problems, and suggests improvements. Get instant insights into your website’s compliance, allowing you to determine the scope of necessary corrections and the gap between the current state of your website or HTML document and WCAG requirements.

Text “Web Accessibility Checker”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.