【发布时间】:2017-04-01 04:07:16
【问题描述】:
我刚刚开始使用 Espresso 来测试 Android 应用,但遇到了一些问题。我有一个带有按钮的 Activity,它以通常的方式替换片段:
public void onClick(View v) {
final FragmentTransaction t = getFragmentManager().beginTransaction();
t.setCustomAnimations(R.animator.fragment_slide_in_up, 0);
t.replace(R.id.fragment_container,
LogInFragment.newInstance(), LogInFragment.TAG)
.addToBackStack(LogInFragment.TAG);
t.commit();
}
在我的测试中,我单击发送新片段的按钮,然后检查新片段是否可见。
onView(withId(R.id.login_btn))
.perform(click());
Thread.sleep(500);
onView(withId(R.id.email_input))
.check(matches(isDisplayed()));
如果我从测试中删除Thread.sleep(),即使我从活动代码中删除setCustomAnimations(),测试也会失败。
按照说明,我手机上的所有动画都已关闭。我的理解是 Espresso 知道 UI 线程状态,并会等到一切准备就绪。但是如果我做一个onView(withId(R.id.widgetOnNewFragment)),它每次都会爆炸。每次显示新片段时,我都需要添加Thread.sleep(500)。
我错过了什么吗?我想我不应该在我的测试代码中添加几十个 Thread.sleep() 。
【问题讨论】:
-
使用 IdlingResource
-
根据stackoverflow.com/questions/34503616/…,
Fragment和Activity transitions发生在主线程,所以我们不需要实现任何IdlingResources。