Automated Accessibility Testing Using Accessibility Test Framework In Android

Shraddha Gupta
2 min readDec 27, 2021

Android provides an in-built framework to test accessibility in our app, called Accessibility-Test-Framework-for-Android(ATF).We can turn on accessibility checks in Espresso and Roboelectric using this framework.

You can follow below steps to enable ATF in your project:

  1. In Espresso , you can add following dependency in your app’s build.gradle :
dependencies {
androidTestImplementation “androidx.test.espresso:espresso-accessibility:3.2.0”
}

In Roboelectric, you can add following dependency in your app’s build.gradle:

dependencies {
testImplementation “org.robolectric:robolectric:4.3.1”
}

2. you need to add this line in your BeforeClass or in your test method(As per your project structure).

AccessibilityChecks.enable();

Once you add it, accessibility checks will run on any view action defined in ViewAction Class.So each check will include the view on which action is performed and its descendent views.

If you want to run the accessibility checks on entire view hierarchy of a screen, you can add the following line of code:

AccessibilityChecks.enable().setRunChecksFromRootView(true)

Suppressing Results:

The Accessibility Test Framework (ATF) provides option to suppress the failures if it’s not possible to address the issues immediately.It will stop the tests from continuously failing.

You can use setSuppressingResultMatcher to suppress specific check like TextContrastViewCheck , TouchTargetSizeViewCheck etc.

AccessibilityChecks.enable().apply {
setSuppressingResultMatcher(
allOf(
matchesCheckNames(`is`("TextContrastViewCheck")),
matchesViews(withId(R.id.element_id))
)
)
}
AccessibilityChecks.enable().apply {
setSuppressingResultMatcher(
allOf(
matchesCheckNames(`is`("TouchTargetSizeViewCheck")),
matchesViews(withId(R.id.element_id))
)
)
}

Accessibility rules coverage

The following rules are invoked when we enable tests for accessibility checks:

  • TouchTargetSizeViewCheck
  • Target height or target width less than 48 dp is flagged, unless there is a touchdelegate detected.
  • TextContrastViewCheck
  • Checks text color and background and factors in large text, and calculates the contrast ratio: — 4.5 for regular text, 3 for large text.
  • DuplicateSpeakableTextViewHierarchyCheck
  • SpeakableTextPresentViewCheck
  • EditableContentDescViewCheck
  • ClickableSpanViewCheck
  • RedundantContentDescViewCheck
  • DuplicateClickableBoundsViewCheck

References:

Accessibility Test Framework for Android

Hope this helps.

Happy Testing.

--

--