Validation Results – How to Check Web Accessibility and Save Results

Aspose.HTML for Java includes the com.aspose.html.accessibility package, intended for performing and managing web accessibility checks.

This article explains how to review validation results against WCAG standards, report the tested criteria, and detail specific issues encountered during the validation.

To learn how to programmatically check the accessibility of a website and get a step-by-step guide, read the article Accessibility Validator – Website Accessibility Check in Java.

ValidationResult Class

The com.aspose.html.accessibility.results package contains classes describing the web accessibility rule validation results. The ValidationResult class is the main class that contains results for checking all criteria from AccessibilityRules object.

Here is a Java code snippet for a web accessibility check. It sets up a validator with specific settings, validates an HTML document, and outputs the results to the console, allowing you to identify and address the specific problems in your HTML document:

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

RuleValidationResult Class

The RuleValidationResult class represents the validation result of a particular accessibility rule. It provides detailed information about how the rule was evaluated, including a collection of ITechniqueResult elements, each representing the method used to meet the success criteria.

The class reveals several key properties (methods):

The following code iterates through the list of RuleValidationResult objects in the Details property of a validationResult object. Each RuleValidationResult represents the outcome of a specific accessibility rule check during the validation process:

1    // Get a list of RuleValidationResult Details
2    for (RuleValidationResult result : validationResult.getDetails())
3    {
4        System.out.println(String.format("%s: %s = %s",
5            result.getRule().getCode(), result.getRule().getDescription(), result.getSuccess()));
6    }

Assume the Details contains results for two rules – H37 and H67. The output will be:

1H37:Check alt attributes for images = True
2H67:Check that all forms have labels = False

ITechniqueResult

The ITechniqueResult is a public interface that contains information about the result of checking a particular accessibility technique used to satisfy a rule’s success criterion. It includes a getRule() method to identify the associated rule, getSuccess() to indicate whether the technique passed, and getError(), which returns an IError object containing details of any problems encountered during the check. This interface helps break down the rule check into individual technique results for more detailed analysis.

Below is an example of the output of results that did not pass the test of success criteria. We print to the console code description of the criteria, as well as all the details of the execution of the methods of these criteria both successful and with errors:

 1// Validate HTML accessibility using Java and get detailed failed rule results
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Create an accessibility validator with static instance for all rules
 7// from repository that match the builder settings
 8AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());
 9
10String documentPath = "input.html";
11
12// Initialize an object of the HTMLDocument class
13final HTMLDocument document = new HTMLDocument(documentPath);
14ValidationResult validationResult = validator.validate(document);
15
16// Take a list of rules results
17for (RuleValidationResult ruleResult : validationResult.getDetails()) {
18    // List only unsuccessful rule
19    if (!ruleResult.getSuccess()) {
20        // Print the code and description of the rule
21        System.out.println(String.format("%s: %s",
22                ruleResult.getRule().getCode(),
23                ruleResult.getRule().getDescription()
24        ));
25
26        // Print the results of all methods
27        for (ITechniqueResult ruleDetail : ruleResult.getResults()) {
28            // Print the code and description of the criterions
29            StringBuilder str = new StringBuilder(String.format("\n{0}: {1} - {2}",
30                    ruleDetail.getRule().getCode(),
31                    ruleDetail.getSuccess(),
32                    ruleDetail.getRule().getDescription()
33            ));
34            System.out.println(str);
35        }
36    }
37}

Save Validation Results

Once the validation process is complete, you can save the results for further analysis, documentation, and reporting. Aspose.HTML for Java library allows you to save the validation results into a TextWriter object, where a ValidationResultSaveFormat type parameter specifies in what format the text will be saved.

Three main formats are available for saving web accessibility validation results:

Save Validation Results to a String

When saving validation results to a string, the saveToString() method is used:

 1// Validate HTML for accessibility in Java and export all errors and warnings as a string
 2
 3String htmlPath = "input.html";
 4
 5final HTMLDocument document = new HTMLDocument(htmlPath);
 6AccessibilityValidator validator = new WebAccessibility().createValidator();
 7ValidationResult validationresult = validator.validate(document);
 8
 9// get rules errors in string format
10String content = validationresult.saveToString();
11
12// SaveToString - return only errors and warnings
13// if everything is ok, it will return "validationResult:true"
14System.out.println(content);

The output is presented in a simple text format, clearly indicating the result of the check and providing detailed information about errors with comments:

 1validationResult:False;
 2%%
 3technique: H35;
 4criterion: 1.1.1;
 5type: Error;
 6description: Check that the applet element contains an alt attribute with a text alternative for the applet. ;
 7source: <applet code="tictactoe.class" width="250" height="250">tic-tac-toe game</applet>;
 8%%
 9technique: H37;
10criterion: 1.1.1;
11type: Error;
12description: Img element missing an alt attribute. The value of this attribute is referred to as "alt text".;
13source: <img src="image1.jpeg">;
14%%
15
16...

Where the result of the check is indicated validationResult and a list of errors and comments:

Save Validation Results in XML Format

For those who prefer a more structured and machine-readable format, storing validation results in XML is a suitable choice. Let’s look at how to save the results in XML format using the saveTo(writer, format) method. This method takes a TextWriter object and the desired ValidationResultSaveFormat (in this case, XML).

 1// Validate HTML for accessibility in Java and export all errors and warnings as an XML
 2
 3String htmlPath = "input.html";
 4
 5final HTMLDocument document = new HTMLDocument(htmlPath);
 6AccessibilityValidator validator = new WebAccessibility().createValidator();
 7ValidationResult validationresult = validator.validate(document);
 8
 9final java.io.StringWriter sw = new java.io.StringWriter();
10validationresult.saveTo(sw, ValidationResultSaveFormat.XML);
11
12String xml = sw.toString();
13System.out.println(xml);
14
15DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
16DocumentBuilder documentBuilder = documentBuildFactory.newDocumentBuilder();
17documentBuilder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));

The resulting XML representation is a well-organized format for easy analysis and further processing:

 1<validationResult>
 2<isValid>false</isValid>
 3<details>
 4  <techniqueResult>
 5    <technique>H35</technique>
 6    <criterion>1.1.1</criterion>
 7    <type>Error</type>
 8    <description>Check that the applet element contains an alt attribute with a text alternative for the applet. </description>
 9    <source><![CDATA[<applet code="tictactoe.class" width="250" height="250">tic-tac-toe game</applet>]]>
10    </source>
11  </techniqueResult>
12  <techniqueResult>
13    <technique>H37</technique>
14    <criterion>1.1.1</criterion>
15    <type>Error</type>
16    <description>Img element missing an alt attribute. The value of this attribute is referred to as "alt text".</description>
17    <source><![CDATA[<img src="image1.jpeg">]]>
18    </source>
19  </techniqueResult>
20  
21   ...
22
23 </details>
24</validationResult>

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.
  • 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.