浓咖啡测试禁用动画 [英] Espresso testing disable animation
问题描述
@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无法处理动画,所以我在下面做了。
-禁用我的测试设备窗口动画,过渡动画和动画师持续时间比例全部设置为OFF(这不起作用)
-然后我尝试在代码中添加一个标志。 espresso_testing = true。如果为true,则我的代码将跳过调用所有startAnimation()函数的调用。 --->正常。但是,有一个要求,我在编写espresso测试用例时不能在应用程序上更改代码。包括上面的测试用例。
I encountered an issue on doing Espresso testing with a view involving animation, I know Espresso cannot deal with animation, so i did below. - disable my test device Window animation, transition animation and animator duration scale all set to OFF (this does not work) - then i tried to add a flag in my code eg. espresso_testing = true. if true, my code will skip calling all startAnimation() function call. ---> this is working. However, there is a requirement that I cannot change code on my app while writing espresso test case. Included a test case above.
还有其他方法吗?
推荐答案
请确保保持插件更新:
buildscript {
repositories {
google()
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
}
}
在 testOptions
中使用名为 animationsDisabled
:
Use the new flag in testOptions
called animationsDisabled
:
android {
...
testOptions {
animationsDisabled = true
}
}
您可以尝试在设备/仿真器上手动关闭动画:
我们强烈建议您在用于测试的虚拟或物理设备上关闭系统
动画。在
设备上的设置>开发人员选项下,禁用以下
3设置:
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
:
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 &
来源: https://github.com/jaredsburrows/android-gif-example/blob/master/.travis.yml #L34
您可以尝试使用LinkedIn的 TestButler
:
You can try LinkedIn's TestButler
:
TestButler.verifyAnimationsDisabled(InstrumentationRegistry.getTargetContext());
您可以尝试为浓缩咖啡创建 TestRule
和 Gradle
任务测试:
You can try creating a TestRule
and Gradle
task for your espresso tests:
来源: https://product.reverb.com/disabling-animations-in-espresso-for-android-testing-de17f7cf236f
这篇关于浓咖啡测试禁用动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!