Espresso测试禁用动画

kgg*_*goh 18 animation android android-testing android-espresso

在此输入图像描述

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

}
Run Code Online (Sandbox Code Playgroud)

我在使用涉及动画的视图进行Espresso测试时遇到了一个问题,我知道Espresso无法处理动画,所以我在下面做了. - 禁用我的测试设备窗口动画,过渡动画和动画设计持续时间缩放都设置为OFF(这不起作用) - 然后我尝试在我的代码中添加一个标志,例如.espresso_testing = true.如果为true,我的代码将跳过调用所有startAnimation()函数调用.--->这是有效的.但是,在编写espresso测试用例时,我无法更改应用程序中的代码.包括上面的测试用例.

有没有其他方法可以做到这一点?提前致谢.

Jar*_*ows 60

确保更新插件:

buildscript {
  repositories {
    google()
    gradlePluginPortal()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0'
  }
}
Run Code Online (Sandbox Code Playgroud)

使用被testOptions调用的新标志animationsDisabled:

android {

  ...

  testOptions {
    animationsDisabled = true
  }
}
Run Code Online (Sandbox Code Playgroud)

资料来源: 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个设置:

窗口动画比例过渡动画比例Animator持续时间比例

资料来源: 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 &
Run Code Online (Sandbox Code Playgroud)

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

你可以试试LinkedIn的TestButler:

TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());
Run Code Online (Sandbox Code Playgroud)

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

您可以尝试为espresso测试创建一个TestRuleGradle任务:

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

  • @JaredBurrows,您是否自己测试了"animationsDisabled"标志,或者您只是依靠文档?我个人无法利用那面旗帜, (4认同)
  • animationsDisabled不是未知属性。我只是向您指出了官方文档。您必须拥有最新的Android插件“ 2.3.1”。 (2认同)
  • 对于其他人:“animationsDisabled”相当于运行测试的“am Instrument --no-window-animation”,并且在运行测试之前“adb shell 设置将全局 window_animation_scale 0”。要禁用所有动画,请在运行测试之前运行上面答案中的所有三个“adb shell ...”命令。来源:https://issuetracker.google.com/issues/69247502 (2认同)

Ras*_*iri 7

您有三种不同的选项可供选择:

1.在Gradle中使用它

android {

  //...

  testOptions {
    animationsDisabled = true
  }

  // ...
}
Run Code Online (Sandbox Code Playgroud)

2. 在ADB for device中使用

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 &
Run Code Online (Sandbox Code Playgroud)

3. 使用规则

class DisableAnimationsRule : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
           override fun evaluate() {
                // disable animations for test run
                changeAnimationStatus(enable = false)
                try {
                    base.evaluate()
                } finally {
                    // enable after test run
                    changeAnimationStatus(enable = true)
                }
            }
        }
    }

    @Throws(IOException::class)
    private fun changeAnimationStatus(enable:Boolean = true) {
        with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
            executeShellCommand("settings put global transition_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global window_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global animator_duration_scale ${if(enable) 1 else 0}")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 您列出点的方式看起来像是一个人**必须**执行所有这些步骤来禁用动画,但实际上它们都是彼此替代的。这就是为什么添加描述很有帮助 (3认同)

Hen*_*nry 5

是的,您不应该在生产代码中添加测试代码。这里的问题在于动画。如果您正在执行动画HandlersRunnables则无法使用开发人员选项将其关闭。我们使用它来制作动画的一个常见地方是在自定义视图中。

但即使在自定义视图中,也要确保使用ValueAnimator,ObjectAnimatorAnimatorSet来执行动画。只有这样,您才能通过Animator duration scale在开发人员选项中关闭来关闭动画。

一个很好的参考是ProgressBar.