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
 2    var webAccessibility = new WebAccessibility();
 3
 4    // Create an accessibility validator with a static instance for all rules from repository that match the builder settings
 5    var validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 6
 7    // Initialize an object of the HTMLDocument class
 8    using (var document = new Aspose.Html.HTMLDocument("index.html"))
 9    {
10        // Web accessibility check
11        ValidationResult validationResult = validator.Validate(document);
12
13        // Checking for success
14        if (!validationResult.Success)
15        {
16            // Get a list of Details
17            foreach (var detail in validationResult.Details)
18            {
19                Console.WriteLine("{0}:{1} = {2}", detail.Rule.Code, detail.Rule.Description, detail.Success);
20            }
21        }
22    }

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>.
 1  // Get a list of RuleValidationResult Details
 2  foreach (var result in validationResult.Details)
 3  {
 4    Console.WriteLine("{0}:{1} = {2}", result.Rule.Code, result.Rule.Description, result.Success);
 5
 6    // List of the results
 7       foreach (var ruleDetail in result.Results)
 8        {
 9          // test code
10        }
11  }

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    // Take a list of criteria results
 2    foreach (RuleValidationResult criterionResult in validationResult.Details)
 3    {
 4        // List only unsuccessful criteria
 5        if (!criterionResult.Success)
 6        {
 7            // Print the code and description of the criterion
 8            Сonsole.WriteLine("{0}:{1}", criterionResult.Rule.Code, criterionResult.Rule.Description);
 9
10            // Print the results of all methods
11            foreach (ITechniqueResult ruleDetail in criterionResult.Results)
12            {
13                // Print the code and description of the method
14                StringBuilder str = new StringBuilder(string.Format("\n{0}: {1} - {2}",
15                      ruleDetail.Rule.Code, ruleDetail.Success,
16                      ruleDetail.Rule.Description));
17                Console.WriteLine(str);
18            }
19        }
20    }

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.