咖啡测试禁用动画

问题描述 投票:15回答:3

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())));

}

我遇到一个问题,做咖啡的测试,以期涉及动画,我知道,咖啡不能处理的动画,所以我在下面做了。 - 禁用我的测试设备窗口动画,过渡动画和动画长比例都设置为OFF(不工作) - 然后我想在我的代码如增加一个标志。 espresso_testing = TRUE。如果是真的,我的代码将跳过调用所有startAnimation()函数调用。 --->这是工作。但是,我不能在写咖啡测试用例我的应用程序改变代码的要求。包括一个测试用例的上方。

是否有任何其他方式做到这一点?提前致谢。

android animation android-testing android-espresso
3个回答
57
投票

请一定要保持你的插件更新:

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}

使用新的标志testOptions称为animationsDisabled

android {

  ...

  testOptions {
    animationsDisabled = true
  }
}

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

您可以尝试手动在你的设备/仿真器关闭动画:

为了避免片状,我们强烈建议您关闭系统的动画用于测试的虚拟或物理设备上。在设备上,设置>开发者选项,禁用以下3个设置:

窗口动画缩放过渡动画缩放动画长比例

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

您可以尝试使用adb通过命令行:

# 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 &

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

你可以试试LinkedIn的TestButler

TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());

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

您可以尝试创建您的咖啡测试中TestRuleGradle任务:

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


1
投票

诚然,你不应该在生产代码添加测试代码。这里的问题在于动画。如果您正在使用HandlersRunnables进行动画,那么你可以使用开发人员选项不是将其关闭。在这里我们使用这个动画一个共同的地方是在自定义视图。

但即使是在自定义视图,确保您使用ValueAnimatorObjectAnimatorAnimatorSet进行动画。只有这样你才能在开发人员选项关闭Animator duration scale关闭动画。

一个很好的参考是ProgressBar


1
投票

你可以看看这个repo

生成项目并下载生成的.apk文件,并按照该项目禁用动画中提到的指示,你应该有一个一帆风顺之后。你也可以从很多其他来源下载同一个.apk文件。一旦你的.apk文件,然后发出以下命令:

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

这将需要禁用系统的动画为你的照顾。

© www.soinside.com 2019 - 2024. All rights reserved.