Validation Results – Check Web Accessibility in C#

Aspose.HTML for .NET provides the Aspose.Html.Accessibility namespace, which is intended for all web accessibility related manipulations and checks. In this article, you will learn how to review the results of web accessibility checks against WCAG compliance, report the criteria tested, and detail specific issues encountered during the validation.

You can learn how to programmatically check website accessibility from the article Accessibility Validator – Website Accessibility Check in C#.

ValidationResult Class

The Aspose.Html.Accessibility.Results namespace contains classes describing the rule validation results. The ValidationResult class is the main class that contains results for all criteria. The ValidationResult object includes a description of the results of checking a document and has properties:

PropertyDescription
SuccessReturns a boolean that says whether the validation succeeded.
DetailsReturns IList< RuleValidationResult> containing objects of RuleValidationResult with information about validation results.
SaveToString()Save validation results to string
SaveTo(TextWriter writer)Saves the validation results in a System.IO.TextWriter object in text format.
SaveTo(TextWriter writer, ValidationResultSaveFormat format)Saves the validation results to the System.IO.TextWriter object, where the ValidationResultSaveFormat type parameter specifies in what format the text will be saved.

Here is a C# code snippet for a web accessibility check. It sets up a validator with specific settings, validates an HTML document, and outputs the validation results to the console:

 1// Initialize a webAccessibility container
 2var webAccessibility = new WebAccessibility();
 3
 4// Create an accessibillity validator with static instance for all rules from repository that match the builder settings
 5var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 6
 7// Prepare a path to a source HTML file
 8string documentPath = Path.Combine(DataDir, "input.html");
 9
10// Initialize an object of the HTMLDocument class
11using (var document = new HTMLDocument(documentPath))
12{
13    // Check the document
14    ValidationResult validationResult = validator.Validate(document);
15
16    // Checking for success
17    if (!validationResult.Success)
18    {
19        // Get a list of RuleValidationResult Details
20        foreach (var detail in validationResult.Details)
21        {
22            Console.WriteLine("{0}:{1} = {2}", detail.Rule.Code, detail.Rule.Description, detail.Success);
23        }
24    }
25}

The code outputs information about accessibility issues to the console, allowing you to identify and address the specific problems in your HTML document.

RuleValidationResult Class

The RuleValidationResult class – a result of the rule check, contains a list of methods results ITechniqueResult, which are ways to satisfy the success criteria.

PropertyDescription
SuccessReturns a boolean that says whether the validation succeeded.
RuleReturns the checked rule. The rule has a type that implements the IRule interface.
ResultsReturns a collection of all results, both successful and not. Returns type is IList< ITechniqueResult>.
ErrorsReturns a collection of results with Errors. Returns type is IList< ITechniqueResult>.
WarningsReturns a collection of results with Warnings. Returns type is IList< ITechniqueResult>.

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    foreach (RuleValidationResult result in validationResult.Details)
3    {
4        Output.WriteLine("{0}:{1} = {2}", result.Rule.Code, result.Rule.Description, result.Success);
5    }

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

PropertyDescription
SuccessReturns a boolean that says whether the validation succeeded.
RuleReturns the checked technique with type of IRule.
ErrorReturns the object with error. Returns type is IError.

Below is a C# 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. Each RuleValidationResult represents a specific accessibility criterion evaluated during validation. The code iterates through the RuleValidationResult objects contained in validationResult.Details.
  2. Check if the criterion was unsuccessful – !criterionResult.Success. If the criterion fails, the code reports information about the unsuccessful criterion.
  3. Use Console.WriteLine to print the code and description of the failing criterion.
  4. Then iterate through the ITechniqueResult objects. These ITechniqueResult objects represent the results of individual rules for the criterion that reported errors.
  5. For each rule result, create a StringBuilder object to format information about the method, including the method code, method success, and method description.
  6. Print information about the result of each rule, including the method code, success status, and description.
 1// Initialize a webAccessibility container
 2var webAccessibility = new WebAccessibility();
 3
 4// Create an accessibillity validator with static instance for all rules from repository that match the builder settings
 5var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 6
 7string documentPath = Path.Combine(DataDir, "input.html");
 8
 9// Initialize an object of the HTMLDocument class
10using (var document = new HTMLDocument(documentPath))
11{
12    // Check the document
13    ValidationResult validationResult = validator.Validate(document);
14
15    // Take a list of rules results
16    foreach (var ruleResult in validationResult.Details)
17    {
18        // List only unsuccessful rule
19        if (!ruleResult.Success)
20        {
21            // Print the code and description of the rule
22            Console.WriteLine("{0}:{1}", ruleResult.Rule.Code, ruleResult.Rule.Description);
23
24            // Print the results of all methods
25            foreach (ITechniqueResult ruleDetail in ruleResult.Results)
26            {
27                // Print the code and description of the criterions
28                StringBuilder str = new StringBuilder(string.Format("\n{0}: {1} - {2}",
29                    ruleDetail.Rule.Code, ruleDetail.Success,
30                    ruleDetail.Rule.Description));
31                Console.WriteLine(str.ToString());
32            }
33        }
34    }
35}

Using Aspose.HTML for .NET allows you to review the results of web accessibility checks systematically, report the criteria tested, and detail specific issues encountered during the validation process. This is useful for understanding the nature of your web content accessibility problems and taking appropriate action to resolve them.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.