Cypressでのレポート生成
@axe-devtools/reporterパッケージを@axe-devtools/cypressと統合してレポートを生成する方法
Not for use with personal data
@axe-devtools/reporterを@axe-devtools/cypressと一緒に活用する方法
前提条件
Axe DevToolsレポーターを使用するには、まずプロジェクトにインストールする必要があります。
npm install @axe-devtools/reporterこのガイドはCypressに特有のセットアップについて説明します。レポーターとそのレポート形式の一般的な概要についてはレポーターガイドを、レポート作成メソッドとそのパラメータの完全なリストについてはレポーターAPIリファレンスをご覧ください。レポーターは、ロガーが記録した結果からレポートを作成します。
note
これらの指示はCypress 10.0以降に適用されます。
セットアップ
次のセットアップが必要です。なぜなら、Cypressにはブラウザコンテキストとノードコンテキストがあり、@axe-devtools/reporterはノードモジュールであるため、ノードコンテキストで初期化する必要があります。これがのみ時に、cy.task内にラップされるときにのみ利用可能です。
cypress/support/e2e.jsに次のものを含めてください:
import '@axe-devtools/cypress';
after(() => {
cy.getAxeResults().then(async results => {
const resultsDir = './results/'
// Use Cypress.spec.name for a unique filename per spec file to avoid overwriting results
await cy.writeFile(`${resultsDir}${Cypress.spec.name}.json`, results)
// reportName labels the results and is used as the prefix in the generated report filenames
await cy.task('reportAsHTML', { resultsDir, reportName: 'axeDevToolsCypress' })
})
})cypress.config.jsファイル内で、レポータータスクをsetupNodeEvents内に追加してください:
const { defineConfig } = require('cypress');
const axeDevToolsPlugin = require('@axe-devtools/cypress/dist/plugin');
const { Reporter } = require('@axe-devtools/reporter');
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
axeDevToolsPlugin(on);
on('task', {
// Reads all .json result files in resultsDir and generates an HTML report;
// reportName labels the results and is used as the prefix in the generated HTML filenames
reportAsHTML: async ({ resultsDir, reportName = 'axeDevToolsCypress' }) => {
const reporter = new Reporter(reportName, resultsDir);
await reporter.buildHTML(resultsDir);
return null;
},
// Reads all .json result files in resultsDir and generates a CSV report
reportAsCSV: async ({ resultsDir, reportName = 'axeDevToolsCypress' }) => {
const reporter = new Reporter(reportName, resultsDir);
await reporter.buildCSV(resultsDir);
return null;
},
// Reads all .json result files in resultsDir and generates a JUnit XML report
reportAsJunit: async ({ resultsDir, reportName = 'axeDevToolsCypress' }) => {
const reporter = new Reporter(reportName, resultsDir);
await reporter.buildJUnitXML(resultsDir);
return null;
},
// Reads all .json result files in resultsDir and generates a W3C EARL JSON-LD report
reportAsEARL: async ({ resultsDir, reportName = 'axeDevToolsCypress' }) => {
const reporter = new Reporter(reportName, resultsDir);
await reporter.buildEARL(resultsDir);
return null;
}
});
return config;
}
}
});cypress/e2e/test.cy.jsに次のものを含めてください:
describe('Axe DevTools Cypress', () => {
it('Scans the page for accessibility issues', () => {
// Visit the page to test
cy.visit('https://broken-workshop.dequelabs.com')
// Run an accessibility scan; results are collected and written to a file by the after() hook in support/e2e.js
cy.axeAnalyze()
})
})関連情報
- JSONアクセシビリティ結果をAxe Reportsにアップロードするは、結果をAxe Reportsにアップロードする方法を説明します。
- Axe Reports APIキーの取得は、Axe Reportsの使用を開始するためのAPIキーを取得する方法を伝えます。
- レポートの作成とフィルタリングは、JSONのアクセシビリティ結果からCSV、XML、またはHTMLでアクセシビリティレポートを作成する方法を示しています。また、このツールを使用して、深刻度で出力をフィルターすることもできます。
- ディスク上のJSON結果の保存方法は、JSONアクセシビリティ結果のファイル命名規則について説明します。
- CLIを使用してアクセシビリティ結果をAxe Developer Hubに送信するは、
.jsonの結果ファイルをAxe Developer Hubにアップロードする方法を示しています。
