Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso testing disable animation

enter image description here

@Test     public void test3_PaySuccessful(){         init();      ViewInteraction amountEditText = onView(             allOf(withId(R.id.et_amount), isDisplayed()));     amountEditText.perform(replaceText("SGD 0.010"), closeSoftKeyboard());      //, withText("Proceed")     ViewInteraction appCompatButton = onView(             allOf(withId(R.id.btn_confirm), isDisplayed()));     appCompatButton.perform(click());      //, withText("Pay")     ViewInteraction appCompatButton2 = onView(             allOf(withId(R.id.btn_confirm), isDisplayed()));     appCompatButton2.perform(click());      //dialog     ViewInteraction appCompatButton3 = onView(             allOf(withId(R.id.confirm_button), withText("Confirm"), isDisplayed()));     appCompatButton3.perform(click());      //have to disable animation in order to pass this.     intended(CoreMatchers.allOf(hasComponent(PaymentSelectionActivity2.class.getName())));  } 

I encountered an issue on doing Espresso testing with a view involving animation, I know Espresso cannot deal with animation, so i did below. - disable my test device Window animation, transition animation and animator duration scale all set to OFF (this does not work) - then i tried to add a flag in my code eg. espresso_testing = true. if true, my code will skip calling all startAnimation() function call. ---> this is working. However, there is a requirement that I cannot change code on my app while writing espresso test case. Included a test case above.

Is there any other way to do this? Thanks in advance.

like image 460
kggoh Avatar asked May 03 '17 04:05

kggoh


People also ask

How do I turn off screen animation?

Open Settings . Scroll down and select Accessibility. Scroll down to Display and tap Text and display. Tap the toggle switch for Remove animations to turn it on.

How do I turn off animation remover?

To access the Accessibility features on your Android device open the Settings app . In the Settings app, select Accessibility from the list. Now scroll down to the Display section and select Remove Animations to set the toggle switch to On.

What is espresso testing used for?

Espresso is a testing framework that helps developers write automation test cases for user interface (UI) testing. It has been developed by Google and aims to provide a simple yet powerful framework. It allows both black-box testing as well as testing of individual components during development cycles.


2 Answers

Make sure to keep your plugin updated:

buildscript {   repositories {     google()     gradlePluginPortal()   }    dependencies {     classpath 'com.android.tools.build:gradle:3.3.0'   } } 

Use the new flag in testOptions called animationsDisabled:

android {    ...    testOptions {     animationsDisabled = true   } } 

Source: https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.TestOptions.html#com.android.build.gradle.internal.dsl.TestOptions:animationsDisabled

You can try turning off animations on your device/emulator manually:

To avoid flakiness, we highly recommend that you turn off system animations on the virtual or physical devices used for testing. On your device, under Settings > Developer options, disable the following 3 settings:

Window animation scale Transition animation scale Animator duration scale

Source: https://developer.android.com/training/testing/espresso/setup#set-up-environment

You can try using adb via command line:

# Turn off animations adb shell settings put global window_animation_scale 0 & adb shell settings put global transition_animation_scale 0 & adb shell settings put global animator_duration_scale 0 & 

Source: https://github.com/jaredsburrows/android-gif-example/blob/master/.travis.yml#L34

You can try LinkedIn's TestButler:

TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext()); 

Source: https://github.com/linkedin/test-butler/blob/master/test-butler-demo/src/androidTest/java/com/linkedin/android/testbutler/demo/AnimationDisablerTest.java#L26

You can try creating a TestRule and Gradle task for your espresso tests:

Source: https://product.reverb.com/disabling-animations-in-espresso-for-android-testing-de17f7cf236f

like image 149
Jared Burrows Avatar answered Sep 24 '22 00:09

Jared Burrows


True, you shouldn't add test code in production code. The problem here lies in the animation. If you are performing animations using Handlers and Runnables then you can't turn them off using the Developer Options. A common place where we use this to animate is in Custom views.

But even in custom views, ensure you use either ValueAnimator, ObjectAnimator or AnimatorSet to perform your animation. Only then you can turn off animations by turning off Animator duration scale in Developer options.

A good reference is the ProgressBar.

like image 40
Henry Avatar answered Sep 23 '22 00:09

Henry