Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Checking a website for accessibility means ensuring that everyone, including people with disabilities, can use your website or app. This involves testing your content against guidelines like WCAG, which outline how to make digital experiences more inclusive. Using Aspose.HTML for Java, you can automate this process by scanning HTML and CSS for accessibility issues – either against the full WCAG set of standards or by focusing on specific criteria.
In this article, you will learn how to assess website accessibility for WCAG compliance using the classes and methods from the com.aspose.html.accessibility and com.aspose.html.accessibility.results packages. We will explore various classes and interfaces that help collect error and warning information while checking a website’s accessibility. Special attention will be given to the failure criteria and methods for reporting errors, along with a Java example that demonstrates how to retrieve web accessibility errors after validating an HTML document.
Let’s see a code snippet related to web accessibility validation. The following code checks the accessibility of a remote HTML page using all WCAG validation rules and prints details of any rules that were not successfully met:
Url) constructor.document) method to check the web page for accessibility. The result is stored in the validationResult variable. 1// Check website for WCAG compliance in Java
2
3// Initialize webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Create an accessibility validator with static instance
7// for all rules from repository that match the builder settings
8AccessibilityValidator validator = webAccessibility.createValidator(ValidationBuilder.getAll());
9
10// Initialize an HTMLDocument object
11final HTMLDocument document = new HTMLDocument("https://products.aspose.com/html/net/generators/video/");
12ValidationResult validationResult = validator.validate(document);
13
14// Checking for success
15if (!validationResult.getSuccess()) {
16 // Get a list of Details
17 for (RuleValidationResult detail : validationResult.getDetails()) {
18 System.out.println(String.format("%s: %s = %s",
19 detail.getRule().getCode(),
20 detail.getRule().getDescription(),
21 detail.getSuccess()
22 ));
23 }
24}Detecting errors helps identify barriers that may prevent people with disabilities from using and interacting with web content effectively. Errors and warnings guide what must be addressed to improve web accessibility. They serve as a roadmap for developers and designers to make necessary changes.
The IError is a public interface containing information about the validation error. This means an erroneous check, i.e., the rule was not passed – and its result does not match Accessibility.
The
IError interface describes the details of a validation problem encountered during accessibility checks. It provides key information about the error, including a human-readable message via getErrorMessage(), the result status with getSuccess(), and the error type as a numeric value (getErrorType()) and a descriptive name (getErrorTypeName()), such as Error or Warning. Additionally, getTarget() returns the specific HTML or CSS element in which the problem was encountered, allowing accessibility issues to be accurately identified and corrected. This interface is essential for effectively interpreting and reporting accessibility check results.
IError object has such meanings:
* ErrorType = 1 and Success = false – this means that the error is critical, and the result of the check is not performed.
* ErrorType = 2 and Success = true – this means that the error is not critical but has an informational character and displays hints for possible improvement.
The type of error is determined by the type of technique:
ErrorType=1 and Success=false, and it is an Error.ErrorType=2 and Success=true, and it is a Warning.The
Target class represents a specific HTML or CSS element where an accessibility error or warning was detected during validation. It provides two key properties: Item, which returns the actual object (such as an HTML tag or CSS rule) that caused the problem, and TargetType, which determines whether the source of the problem is an HTML element or a CSS component.
The TargetTypes enumeration defines the possible types of objects in an HTML document where an accessibility error or warning might be found. It helps classify the source of the problem, making it easier to identify and resolve. The enumeration includes three values:
| Name | Value | Description |
|---|---|---|
| HTMLElement | 0 | The element containing the HTMLElement from a document: standard HTML elements such as <img> or <button>. |
| CSSStyleRule | 1 | The element containing the CSSStyleRule from a document: specific CSS rules in stylesheets. |
| CSSStyleSheet | 2 | The element containing the CSSStyleSheet from a document: entire CSS stylesheets. |
This enumeration works in conjunction with the Target class to indicate exactly what type of element caused the validation result.
Let’s look at the Java code for iterating through the web accessibility check results, paying particular attention to the failure criteria and the details of the methods that report errors. Example of getting errors details and elements of an HTML document:
ruleResult objects contained within validationResult.getDetails(). These represent different accessibility criteria.ruleDetail contained within ruleResult.getErrors(). The ITechniqueResult objects represent individual rule results for the criterion that reported errors.error object from the ruleDetail. The ruleDetail represents a specific accessibility issue, and the error object contains information about that issue.target object associated with the error. The target object typically represents the specific HTML element, CSS rule, or other content that triggered the accessibility error.error.getErrorTypeName() and the error message error.getErrorMessage(). 1// Check HTML for WCAG compliance and output failed rule codes and error messages
2
3// Initialize a webAccessibility container
4WebAccessibility webAccessibility = new WebAccessibility();
5
6// Create an accessibility validator with static instance
7// for all rules 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
16for (RuleValidationResult ruleResult : validationResult.getDetails()) {
17 // list only unsuccessful rule
18 if (!ruleResult.getSuccess()) {
19 // print the code and description of the rule
20 System.out.println(String.format("%s: %s = %s",
21 ruleResult.getRule().getCode(),
22 ruleResult.getRule().getDescription(),
23 ruleResult.getSuccess()
24 ));
25
26 // print the results of methods with errors
27 for (ITechniqueResult ruleDetail : ruleResult.getErrors()) {
28 // print the code and description of the method
29 StringBuilder str = new StringBuilder(String.format("\n{0}: {1} - {2}",
30 ruleDetail.getRule().getCode(),
31 ruleDetail.getSuccess(),
32 ruleDetail.getRule().getDescription()
33 ));
34 // get an error object
35 IError error = ruleDetail.getError();
36 // get a target object
37 Target target = error.getTarget();
38 // get error type and message
39 str.append(String.format("\n\n\t%s : %s",
40 error.getErrorTypeName(),
41 error.getErrorMessage()
42 ));
43
44 if (target != null) {
45 // Checking the type of the contained object for casting and working with it
46 if (target.getTargetType() == TargetTypes.CSSStyleRule) {
47 ICSSStyleRule cssRule = (ICSSStyleRule) target.getItem();
48 str.append(String.format("\n\n\t%s",
49 cssRule.getCSSText()
50 ));
51 }
52 if (ruleDetail.getError().getTarget().getTargetType() == TargetTypes.CSSStyleSheet) {
53 str.append(String.format("\n\n\t%s",
54 ((ICSSStyleSheet) target.getItem()).getTitle()
55 ));
56 }
57 if (ruleDetail.getError().getTarget().getTargetType() == TargetTypes.HTMLElement) {
58 str.append(String.format("\n\n\t%s",
59 ((HTMLElement) target.getItem()).getOuterHTML()
60 ));
61 }
62 }
63 System.out.println(str);
64 }
65 }
66}For each failed criterion, the code lists rule descriptions, failed methods, error messages, and highlights the exact HTML or CSS element where the issue was found. This approach is ideal for generating a complete and accurate accessibility report, including error context and technical diagnostics for developers.
See Also
Aspose.HTML offers a free online Color Contrast Checker to check the contrast ratios in your web designs. This tool provides feedback on whether your color choices meet accessibility standards.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.