Espresso testing disable animation

后端 未结 3 1786
臣服心动
臣服心动 2020-12-30 21:42

@Test
    public void test3_PaySuccessful(){
        init();

    ViewInteraction amountEditText = onView(
            allOf(withId(R.id.et_amount), isDispl         


        
相关标签:
3条回答
  • 2020-12-30 22:02

    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.

    0 讨论(0)
  • 2020-12-30 22:10

    You could take a look at this repo

    Build the project and download the generated .apk file and follow the instructions mentioned in that project to disable the animations and you should have a smooth sailing afterwards. You could also download the same .apk file from a lot of other sources. Once you have the .apk file then issue the following commands:

    adb install -r android_emulator_hacks.apk
    adb shell pm grant no.finn.android_emulator_hacks android.permission.SET_ANIMATION_SCALE
    adb shell am start -n no.finn.android_emulator_hacks/no.finn.android_emulator_hacks.HackActivity
    

    This will take care of disabling the system animations for you.

    0 讨论(0)
  • 2020-12-30 22:15

    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

    0 讨论(0)
提交回复
热议问题