カスタムルール
Not for use with personal data
カスタムルール機能を使用すると、新しいルールを作成してテストに利用し、既存のルールの主要な動作を変更することができます。
ビューの名前プロパティをチェックするカスタムルールを作成しましょう。結果基準は以下の通りです:
- 失敗:名前が空である。
- 成功:名前にある程度の長さがある。
ルールの作成
- から継承する新しいクラスオブジェクトを設定します。
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
) {
}- 可視テキストがヌルでないことを確認して、ルールがビューに適用されるかどうかを判断します。
override fun isApplicable(axeView: AxeView): Boolean {
val visibleText = axeView.text
return visibleText != null && super.isApplicable(axeView)
}- 最後に、
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")
}
}