カスタムルール

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
Not for use with personal data

カスタムルール機能を使用すると、新しいルールを作成してテストに利用し、既存のルールの主要な動作を変更することができます。

ビューの名前プロパティをチェックするカスタムルールを作成しましょう。結果基準は以下の通りです:

  • 失敗:名前が空である。
  • 成功:名前にある程度の長さがある。

ルールの作成

  1. AxeRuleを継承する新しいクラスオブジェクトを設定します。 初期化子の中でルールの設定を行い、以下を指定できます。
  • どの標準に対してテストするか
  • ルールのカテゴリ
  • 成功基準
  • 影響
  • ルールの概要
class CustomVisibleTextRule : AxeRule(
    AxeWcagStandard.WCAG_2A.text,
    AxeCategory.STRUCTURE.text,
    AxeSuccessCriteria.WCAG_132.text,
    AxeImpact.MINOR.value,
    "Ensure AT and non-AT users have the same experience of understanding labels",
    false
) {
    
}
  1. 可視テキストがヌルでないことを確認して、ルールがビューに適用されるかどうかを判断します。
override fun isApplicable(axeView: AxeView): Boolean {
    val visibleText = axeView.text
    return visibleText != null && super.isApplicable(axeView)
}
  1. 最後に、runRule関数で、ルールが成功したか失敗したかを判断するために何に対してテストを行うかを指定します。分析された値のプロパティを収集するには、以下の例でpropCollectorのヘルパーメソッドを使用します。
override fun runRule(axeView: AxeView): RunRuleResult {
    val runRuleResult = super.runRule(axeView)
    if (runRuleResult.status.isNotEmpty()) {
        return runRuleResult
    }

    val visibleText = propCollector(Visible Text, axeView.test)

    visibleText?.let {
        return when (it.isNotEmpty()) {
            true -> RunRuleResult(AxeStatus.PASS, "All visible texts are present")
            false -> RunRuleResult(AxeStatus.FAIL, "All visible texts are not present")
        }
    }

    return RunRuleResult(AxeStatus.INCOMPLETE, "Need review")
}

Axe DevToolsへの追加

カスタムルールが準備できたら、それを初期化されたAxe DevToolsオブジェクトのルールリストに追加し、テストが実行される前に設定を行います。

val axe = AxeDevTools()

init {
    axe.addCustomRule(CustomVisibleTextRule::class.java)
} 

完全なカスタムルールの例

class CustomVisibleTextRule: AxeRule(
    AxeWcagStandard.WCAG_2A.text,
    AxeCategory.STRUCTURE.text,
    AxeSuccessCriteria.WCAG_132.text,
    AxeImpact.MINOR.value,
    "Ensure AT and non-AT users have the same experience of understanding labels",
    false
) {
    override fun isApplicable(axeView: AxeView): Boolean {
        val visibleText = axeView.text
        return visibleText != null && super.isApplicable(axeView)
    }

    override fun runRule(axeView: AxeView): RunRuleResult {
        val runRuleResult = super.runRule(axeView)
        if (runRuleResult.status.isNotEmpty()) {
            return runRuleResult
        }

        val visibleText = propCollector(Visible Text, axeView.text)

        visibleText?.let {
            return when (it.isNotEmpty()) {
                true -> RunRuleResult(AxeStatus.PASS, "All visible texts are present")
                false -> RunRuleResult(AxeStatus.FAIL, "All visible texts are not present")
            }
        }

        return RunRuleResult(AxeStatus.INCOMPLETE, "Need review")
    }
}