C#でレポートを生成する

This page is not available in the language you requested. You have been redirected to the English version of the page.
Link to this page copied to clipboard

Axe DevTools for Webを使用してC#でレポーター対応のJSON出力を生成する

Not for use with personal data

この統合は、Axe DevTools レポーターと互換性のある形式で結果を生成します。

レポーター対応のJSONを作成する

AxeSelenium.Analyze はAxe DevToolsレポーターで使用するための適切な形状のオブジェクトを返します。それはJSON.NET JSON変換ライブラリJsonConvert.SerializeObjectメソッドを使って簡単にJSON文字列に変換できます。

ここに、ページを分析し、結果をファイルに保存する例があります。

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

レポートを設定する

レポートのメタデータはReportConfigurationクラスを通じて設定されます。このリンクをチェックしてそのAPIを確認してください。

ReportConfigurationオブジェクトが望ましい状態になったら、次の2つの方法でスキャンに適用できます。

  1. それは単一のAxeSeleniumオブジェクトにコンストラクタ引数を通じて結びつけることにより、単一のスキャンに適用されます。

    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. ReportConfigurationのインスタンスをデフォルトとして設定することによって、個々に上書きされていないすべてのスキャンに適用されます。

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

現在デフォルトとして機能しているReportConfigurationインスタンスは、ReportConfiguration.GetDefault()を呼び出すことによって取得できます。

例の生成コード

Axe DevToolsは、ユーザーが閲覧したりエクスポートしたりできるレポートを自動的に作成するための組み込みツールを提供します。

次の例では、CreateResultsOutputメソッドを含むAxeReportingクラスを使用してグローバルなレポート設定を確立し、レポート結果を含むレンダリングされたJSONファイルを作成します。

CreateResultsOutputメソッドの実行が完了すると、LogResultsメソッドが続いて実行され、axe-TestCaseName.jsonを入力として使用して、対応するユーザーが読みやすいHTMLファイルを各JSONファイルに対して作成するためにAxe DevTools Reporterバイナリを起動します。

これらのレポートには3つの利用可能な形式があります。HTMLはローカルテスト環境でのユーザー閲覧に最適です。CI環境では、JUnit XMLオプションが利用可能です。最終的な形式はCSVで、これは他の多くのツールへのエクスポートを可能にします。

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/repoter-cli-win.exe", Arguments = "../../../reports ../../../reports/a11y-reports --format html", };
             Process proc = new Process() { StartInfo = startInfo, };
             proc.Start();
        }
  }
}

関連情報