Android Espresso waitFor.. 和 Thread.sleep() 解决方案

Ole*_*aev 5 multithreading android android-espresso

我刚开始使用 Espresso,在此之前我尝试过 Robotium。我需要测试 LoginActivity。逻辑是:

  1. 用户输入正确的凭据;
  2. 用户看到“登录...”字符串;
  3. 用户等待字符串消失;
  4. 用户在 MainActivity 中并看到“您已登录”

测试登录源:

    public void testLogin() throws Exception{
    onView(withId(R.id.login_email)).perform(typeText(LOGIN_EMAIL));
    onView(withId(R.id.login_password)).perform(typeText(LOGIN_PASSWORD));
    onView(withId(R.id.login_loginBtn)).perform(click());
    onView(withText(R.string.loading_logging_in)).check(matches(isDisplayed()));
    onView(withText("You're logged in")).check(matches(isDisplayed()));
}
Run Code Online (Sandbox Code Playgroud)

问题是浓缩咖啡没有等待“您已登录”字符串出现,而是在登录过程中尝试找到它。

日志猫:

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException:在层次结构中没有找到匹配的视图:文本:是“您已登录”

我试过使用 Thread.sleep(10000),但它终止了 Thread.sleep(10000) 上的运行并给我错误:

测试未能运行完成。原因:“由于“进程崩溃”,检测运行失败。”。检查设备日志以获取详细信息 测试运行失败:由于“进程崩溃”,仪器运行失败。

日志猫:

@@@ @@@ @@@ 被 HttpReRegistrationService 中的 cancelService 杀死

在此之前,我使用了 waitForActivity(MainActivity.class) 方法。是否有任何解决方法可以使这项工作?

And*_*son 2

如果您的登录过程在尝试登录用户时很忙,并且您没有使用 AsyncTask(以适当的方式),那么您唯一的选择是让繁忙的资源实现此处所述IdlingResource的接口。

这样 Espresso 就知道何时等待以及等待多长时间。