dlm*_*lmt 2 android android-fragments android-espresso android-transitions
我有一个托管片段F1的活动.单击按钮后,F1将被另一个片段F2替换.当按下后退按钮时,应用程序通过退出过渡动画从F2返回到F1 .
我的Espresso测试用例大致如下:
@Test
public void pressingBackRestorePreviousFragment() {
// we are in F1 and about to switch to F2
onView(withId(R.id.the_button)).perform(click());
// we are now in F2, pressing back should "return" to F1
Espresso.pressBack();
onView(withText("A specific text in F1")).check(matches(isDisplayed());
}
Run Code Online (Sandbox Code Playgroud)
当测试用例以逐步调试模式运行时,上述测试通过.但在正常运行模式下,它会失败.只有当我Thread.sleep(___)在onView(withText(__))测试之前插入才会通过.
我相信更好的技术是替换Thread.sleep()Espresso IdlingResource,但我不确定如何将它与视图动画线程结合.理想情况下,我想重写上面的测试用例
@Test
public void pressingBackRestorePreviousFragment() {
// we are in F1 and about to switch to F2
onView(withId(R.id.the_button)).perform(click());
// we are now in F2, pressing back should "return" to F1
Espresso.pressBack();
onView(isRoot()).perform(waitForAnimCompletion());
onView(withText("A specific text in F1")).check(matches(isDisplayed());
}
Run Code Online (Sandbox Code Playgroud)
在执行操作之前,Espresso将等待主线程变为空闲.Fragment并且Activity转换发生在主线程上,因此您不需要实现任何IdlingResources.这里发生的事情是,过渡动画导致Espresso与主线程和测试线程的同步导致了瑕疵.如Espresso设置说明中所述,您应该在用于测试的设备上禁用系统动画.
一般来说,Thread.sleep语句是测试的不良实践,会导致测试不稳定和缓慢.Espresso的设计正是为了解决Android上的这个问题.在GTAC 2013上介绍Espresso时,结账Valera Zakharov对此问题的解释.
| 归档时间: |
|
| 查看次数: |
1801 次 |
| 最近记录: |