Writing Tests for axe DevTools HTML in C#

Link to Writing Tests for axe DevTools HTML in C# copied to clipboard

How to write effective tests for accessibility using axe DevTools HTML for C#

This guide provides example tests for both NUnit and MSTest. All tests use the AxeReporting class to create initial reporting results. For more information about the axe DevTools testing classes, visit the C# API Reference.

A basic default test

The following test calls axe.Analyze() to analyze the page, compares the results to the assertion, and creates initial report output using the AxeReporting class.

NUnit syntax

[Test]
public void AccessibilityAnalyzeAsync()
{
  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "basic analyze");

  Assert.That(results.Findings.Violations.Count, Is.EqualTo(0));
}

MSTest syntax

[TestMethod]
public void AccessibilityAnalyzeAsync()
{
  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "basic analyze");

  Assert.IsTrue(results.Findings.Violations.Count == 0);
}

Testing page states and flows

This test case calls axe.Analyze() to scan the page in different states, or different flows of the page, compares the results to the assertion, and creates initial report output:

NUnit syntax

[Test]
public void AccessibilityAnalyzeWithPageFlow()
{
  var homeResults = axe.Analyze();
  AxeReporting.CreateResultsOutput(homeResults, "home-page");

  driver.FindElementByName("q").SendKeys("csun 2019 accepting new papers for presentations");

  driver.FindElementById("go").Click();

  var searchResults = axe.Analyze();
  AxeReporting.CreateResultsOutput(searchResults,"search-results-page");

  Assert.That(searchResults.Findings.Violations.Count, Is.EqualTo(0));
}

MSTest syntax

[TestMethod]
public void AccessibilityAnalyzeWithPageFlow()
{
  var homeResults = axe.Analyze();
  AxeReporting.CreateResultsOutput(homeResults, "home-page");

  driver.FindElementByName("q").SendKeys("csun 2019 accepting new papers for presentations");

  driver.FindElementById("go").Click();

  var searchResults = axe.Analyze();

  AxeReporting.CreateResultsOutput(searchResults, "search-results-page");

  Assert.IsTrue(searchResults.Findings.Violations.Count == 0);
}

Testing with specific rules only

This test calls axe.Analyze() to scan the page with specified rules:

NUnit syntax

[Test]
public void AccessibilityAnalyzeWithSpecificRules()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("specifc rules")).WithRules("heading-order", "link-name");
  var results = axe.Analyze();

  System.Console.WriteLine(results);
  
  AxeReporting.CreateResultsOutput(results, "specific-rules");

  Assert.That(results.Findings.Violations.Count, Is.EqualTo(0));
}

Testing with specific rules disabled

This test calls axe.Analyze() to scan the with specific rules disabled:

NUnit syntax

[Test]
public void AccessibilityAnalyzeWithDisabledRules()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("disabled rules")).DisablingRules("heading-order", "link-name");

  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "disabled-rules");

  Assert.That(results.Findings.Violations.Count, Is.EqualTo(0));
}

MSTest syntax

[TestMethod]
public void AccessibilityAnalyzeWithDisabledRules()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("disabled rules")).DisablingRules("heading-order", "link-name");

  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "disabled-rules");

  Assert.IsTrue(results.Findings.Violations.Count == 0);
}

Testing with a specific ruleset

This test calls axe.Analyze() to scan the page with a specified ruleset, in this case 508:

NUnit syntax

[Test]
public void AccessibilityAnalyzeWith508Ruleset()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("specific ruleset")).WithRuleset("508");
  
  var results = axe.Analyze();
  
  AxeReporting.CreateResultsOutput(results, "508-rules");
  
  Assert.That(results.Findings.Violations.Count, Is.EqualTo(0));
}

MSTest syntax

[TestMethod]
public void AccessibilityAnalyzeWith508Ruleset()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("specific ruleset")).WithRuleset("508");

  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "508-rules");

  Assert.IsTrue(results.Findings.Violations.Count == 0);
}

Testing a specific portion of the page (scoped test)

The following test calls axe.Analyze(); to exclude the content associated with the #header ID from testing and ensures content with the #homecontent ID is tested:

NUnit syntax

[Test]
public void IncludingAndExcludingElements()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("excluding and including ruleset")).Excluding("#header").Including("#homecontent");

  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "Including-Excluding rules");

  Assert.That(results.Findings.Violations.Count, Is.EqualTo(0));
}

MSTest syntax

[TestMethod]
public void IncludingAndExcludingElements()
{
  var axe = new AxeSelenium(driver, ReportConfiguration.GetDefault().UserAgent("chrome").UIState("excluding and including ruleset")).Excluding("#header").Including("#homecontent");

  var results = axe.Analyze();

  AxeReporting.CreateResultsOutput(results, "Including-Excluding rules");

  Assert.IsTrue(results.Findings.Violations.Count == 0);
  }

Downloadable examples

Download and run both axe DevTools C# NUnit and MSTest examples:

Refer to the README.md files in the downloaded attestCSharpNUnit/attestCSharpExample and attestCSharpMSTest/attestMSTestExample package folders for prerequisites, integration information, included test cases, and reporting setup in each example package.