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

}

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

还有其他方法吗?提前致谢。

原文由 kggoh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 255
1 个回答

确保保持你的插件更新:

 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:动画已禁用

您可以尝试手动关闭设备/模拟器上的动画:

为避免不稳定,我们强烈建议您关闭用于测试的虚拟或物理设备上的系统动画。在您的设备上,在“设置”>“开发人员选项”下,禁用以下 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 &

来源: 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

原文由 Jared Burrows 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题