Generating Reports with C#

Link to Generating Reports with C# copied to clipboard

This integration produces results in a format compatible with the Axe DevTools reporter.

Creating reporter-compatible JSON

AxeSelenium.Analyze returns an object in the right shape for use with the axe DevTools reporter. It can be easily converted to a JSON string via the JsonConvert.SerializeObject method of the JSON.NET JSON conversion library.

Here is an example where we analyze a page and save the results to a file:

var axe = new AxeSelenium(driver);
var results = axe.Analyze();
var jsonResults = JsonConvert.SerializeObject(results, Formatting.Indented);
File.WriteAllText("myResults.json", jsonResults);

Configuring the report

Report metadata is configured via the ReportConfiguration class. Check the link to view its API.

Once a ReportConfiguration object is in the desired state, it can be applied to a scan in two ways:

  1. It can be applied to a single scan by tying it to a single AxeSelenium object via constructor arguments

    var reportConfig = ReportConfiguration.GetDefault().TestSuiteName("my suite").UIState("some state");
    var axe = new AxeSelenium(driver, reportConfig);
    var results = axe.Analyze();
    
    Debug.Assert(results.Name == "my suite");
    Debug.Assert(results.Id = "some state");
  2. It can be applied to all scans that are not individually overridden, by setting an instance of ReportConfiguration as the default

    ReportConfiguration.GetDefault().TestSuiteName("my suite").SetAsDefault()
    
    var axe = new AxeSelenium(driver);
    var results = axe.Analyze();
    
    Debug.Assert(results.Name == "my suite");

You can get the ReportConfiguration instance currently serving as the default by calling ReportConfiguration.GetDefault().

Example generation code

Axe DevTools offers a built-in tool to automatically create reports for user viewing or for export.

The following example sets up a global reporting configuration with an AxeReporting class containing a CreateResultsOutput method to create a rendered JSON file containing report results.

Upon completing the execution of the CreateResultsOutput method, the LogResults method executes after to launch the axe DevTools Reporter binary using the axe-TestCaseName.json as an input to create a corresponding user-readable HTML file for each JSON file.

There are three available formats for these reports. HTML works best for user viewing in local testing environments. For CI environments, a JUnit XML option is available. The final format is CSV, which enables export to numerous other tools.

using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;
using Deque.AxeDevtools.Selenium.Results;

namespace test
{
  public class AxeReporting
  {
    public static void CreateResultsOutput(ReportResults results, string TestCaseName)
    {
        var jsonResults = JsonConvert.SerializeObject(results, Formatting.Indented);
        File.WriteAllText("../../../reports/axe-" + TestCaseName + ".json", jsonResults);
    }

    public static void LogResults()
        {
             ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "../../../resources/axe-devtools-reporter-win.exe", Arguments = "../../../reports --dest ../../../reports/a11y-reports --format html", };
             Process proc = new Process() { StartInfo = startInfo, };
             proc.Start();
        }
  }
}

See Also