Custom Rules

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

The custom rules feature lets you create new rules to test against and change key behaviors within existing rules.

Let's create a custom rule that checks for the name property of a view. The result criteria will be as follows:

  • Fail: the name is empty.
  • Success: the name has some length.

Creating the Rule

  1. Set up a new class object that inherits from AxeRule.

You can set up the rule configuration within the initializer to specify the following:

  • What standard you are testing against
  • The category of the rule
  • The success criteria
  • The impact
  • A summary of the rule
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. Determine if the rule applies to the view by ensuring the visible text is not null.
override fun isApplicable(axeView: AxeView): Boolean {
    val visibleText = axeView.text
    return visibleText != null && super.isApplicable(axeView)
}
  1. Lastly, we'll specify in the runRule function what we're testing against to determine whether the rule has succeeded or failed. To collect properties for the analyzed values, use the helper method: propCollector in the example below.
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")
}

Add to axe DevTools

Once the custom rule is ready, add it to the rule list of the initialized axe DevTools object, and set configurations before any tests run.

val axe = AxeDevTools()

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

Complete Custom Rule Example

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")
    }
}