Accessibility Validation Results in C#

Use ValidationResult to review accessibility checks in C#. After AccessibilityValidator.Validate(document) returns the result, inspect Success, Details, RuleValidationResult, technique results, errors, and warnings.

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. Create a WebAccessibility container.
  2. Create an AccessibilityValidator with the required rules or builder settings.
  3. Load an HTML document with HTMLDocument.
  4. Call Validate(document) and receive a ValidationResult.
  5. Check Success and inspect Details to review rule-level results.
 1// Validate HTML against WCAG rules using C#
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Create an accessibillity validator with static instance for all rules from repository that match the builder settings
 7AccessibilityValidator validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 8
 9// Prepare a path to a source HTML file
10string documentPath = Path.Combine(DataDir, "input.html");
11
12// Initialize an object of the HTMLDocument class
13using (HTMLDocument document = new HTMLDocument(documentPath))
14{
15    // Check the document
16    ValidationResult validationResult = validator.Validate(document);
17
18    // Checking for success
19    if (!validationResult.Success)
20    {
21        // Get a list of RuleValidationResult Details
22        foreach (RuleValidationResult detail in validationResult.Details)
23        {
24            Console.WriteLine("{0}:{1} = {2}", detail.Rule.Code, detail.Rule.Description, detail.Success);
25        }
26    }
27}

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        Console.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// Validate HTML accessibility using C# and print all failed WCAG rule with their descriptions
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Create an accessibillity validator with static instance for all rules from repository that match the builder settings
 7AccessibilityValidator validator = webAccessibility.CreateValidator(ValidationBuilder.All);
 8
 9string documentPath = Path.Combine(DataDir, "input.html");
10
11// Initialize an object of the HTMLDocument class
12using (HTMLDocument document = new HTMLDocument(documentPath))
13{
14    // Check the document
15    ValidationResult validationResult = validator.Validate(document);
16
17    // Take a list of rules results
18    foreach (RuleValidationResult ruleResult in validationResult.Details)
19    {
20        // List only unsuccessful rule
21        if (!ruleResult.Success)
22        {
23            // Print the code and description of the rule
24            Console.WriteLine("{0}:{1}", ruleResult.Rule.Code, ruleResult.Rule.Description);
25
26            // Print the results of all methods
27            foreach (ITechniqueResult ruleDetail in ruleResult.Results)
28            {
29                // Print the code and description of the criterions
30                StringBuilder str = new StringBuilder(string.Format("\n{0}: {1} - {2}",
31                    ruleDetail.Rule.Code, ruleDetail.Success,
32                    ruleDetail.Rule.Description));
33                Console.WriteLine(str.ToString());
34            }
35        }
36    }
37}

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.

FAQ

How do I know whether accessibility validation passed?

Check the ValidationResult.Success property. If it is false, inspect Details, Errors, and Warnings to understand which rules failed.

What does RuleValidationResult contain?

RuleValidationResult contains the checked rule, success status, all technique results, and filtered collections of errors and warnings.

When should I inspect technique results?

Inspect technique results when a rule fails and you need to understand which specific technique reported an error or warning.

Related Articles

You can also use the online Web Accessibility Checker to scan pages before analyzing results in code.