Disabling animations when running Android instrumented tests

Photo by Isaac Li Shung Tan on Unsplash

Overview

  1. Replace a standard AndroidJUnitRunner with a custom test runner that extends from it.
  2. Add a test runner delegate that will be triggered at the beginning/end of the test run.
  3. Use Espresso's UiAutomation.executeShellCommand() in a test runner delegate to read and modify device animation settings.

Step 1: Custom Test Runner

testInstrumentationRunner androidx.test.runner.AndroidJUnitRunner
CustomAndroidTestRunner template
testInstrumentationRunner full.package.name.of.your.CustomTestRunner

Step 2: Test Runner Delegates

CustomAndroidTestRunner implementation

Step 3: AnimationsTestRunnerDelegate

InstrumentationRegistry.getInstrumentation().uiAutomation
.executeShellCommand("%some command%")
"settings get global animator_duration_scale""settings put global animator_duration_scale $value""settings put global transition_animation_scale $value""settings put global window_animation_scale $value"
AnimationsTestRunnerDelegate that enables/disables device animations

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store