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// 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

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// 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
 8String documentPath = "input.html";
 9
10// Initialize an object of the HTMLDocument class
11final HTMLDocument document = new HTMLDocument(documentPath);
12ValidationResult validationResult = validator.validate(document);
13
14// Take a list of rules results
15for (RuleValidationResult ruleResult : validationResult.getDetails()) {
16    // List only unsuccessful rule
17    if (!ruleResult.getSuccess()) {
18        // Print the code and description of the rule
19        System.out.println(String.format("%s: %s",
20                ruleResult.getRule().getCode(),
21                ruleResult.getRule().getDescription()
22        ));
23
24        // Print the results of all methods
25        for (ITechniqueResult ruleDetail : ruleResult.getResults()) {
26            // Print the code and description of the criterions
27            StringBuilder str = new StringBuilder(String.format("\n{0}: {1} - {2}",
28                    ruleDetail.getRule().getCode(),
29                    ruleDetail.getSuccess(),
30                    ruleDetail.getRule().getDescription()
31            ));
32            System.out.println(str);
33        }
34    }
35}
Example_IRuleResult hosted with ❤ by GitHub

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:

 1String htmlPath = "input.html";
 2
 3final HTMLDocument document = new HTMLDocument(htmlPath);
 4AccessibilityValidator validator = new WebAccessibility().createValidator();
 5ValidationResult validationresult = validator.validate(document);
 6
 7// get rules errors in string format
 8String content = validationresult.saveToString();
 9
10// SaveToString - return only errors and warnings
11// if everything is ok, it will return "validationResult:true"
12System.out.println(content);
Example_SaveToString hosted with ❤ by GitHub

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

 1String htmlPath = "input.html";
 2
 3final HTMLDocument document = new HTMLDocument(htmlPath);
 4AccessibilityValidator validator = new WebAccessibility().createValidator();
 5ValidationResult validationresult = validator.validate(document);
 6
 7final java.io.StringWriter sw = new java.io.StringWriter();
 8validationresult.saveTo(sw, ValidationResultSaveFormat.XML);
 9
10String xml = sw.toString();
11System.out.println(xml);
12
13DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
14DocumentBuilder documentBuilder = documentBuildFactory.newDocumentBuilder();
15documentBuilder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));
Example_OutputToXML hosted with ❤ by GitHub

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.