相关疑难解决方法(0)

Android Espresso不会等待片段加载

我刚开始使用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();
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我单击发送新片段的按钮,然后检查新片段是否可见.

onView(withId(R.id.login_btn))
    .perform(click());
Thread.sleep(500);
onView(withId(R.id.email_input))
    .check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

如果我Thread.sleep()从测试中删除,测试将失败,即使我setCustomAnimations()从Activity代码中删除了.

根据说明,我的手机上的所有动画都已关闭.我的理解是,Espresso知道UI线程状态,并会等到一切准备就绪.但如果我这样做onView(withId(R.id.widgetOnNewFragment)),每次都会爆炸.我Thread.sleep(500)每次显示一个新片段时都需要添加一个.

我错过了什么吗?我想我不应该在我的测试代码中添加几十个Thread.sleep().

android android-espresso

7
推荐指数
1
解决办法
7167
查看次数

标签 统计

android ×1

android-espresso ×1