android - Espresso 测试禁用动画

标签 android animation android-testing android-espresso

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

}

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

还有其他方法可以做到这一点吗?提前致谢。

最佳答案

确保更新您的插件:

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

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

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

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

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

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

关于android - Espresso 测试禁用动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751079/

相关文章:

android - Espresso : Assert a Dialog is not shown

android - 在 Android 中效果最好的模拟库/框架?

java - Android 上导致 NPE 的 runOnUiThread 和 sendKeys 调用序列

Android:录制语音并将其转换为文本

android - 无法通过存储访问框架加载扩展名为 .gpx 的文件

css - 带有动画堆叠 View 的 ng-enter 和 ng-leave

javascript - 使用 Three.js 按顺序对立方体进行动画处理

android - 没有构建应用程序的 Phonegap : Fast way of testing Camera-Functions,

android - 是否可以在同一个 button.xml 存档中定义具有背景可绘制行为的多个按钮?

html - 使用 CSS 动画固定悬停时的图像宽度