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

标签 android multithreading android-espresso

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

  1. 用户输入正确的凭据;
  2. 用户看到“Logging in..”字符串;
  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()));
}

问题是 espresso 没有等待“您重新登录”字符串出现,它试图在日志记录仍在进行时找到它。

日志:

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "You're logged in"

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

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. Check device logcat for details Test running failed: Instrumentation run failed due to 'Process crashed.'

日志:

@@@ @@@ @@@was killed cancelService in HttpReRegistrationService

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

最佳答案

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

这样 Espresso 就知道什么时候要等,要等多久。

关于Android Espresso waitFor.. 和 Thread.sleep() 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23494912/

相关文章:

testing - 如何滚动 PreferenceScreen?

android - 使用 Espresso 测试 TextView 值不为空,如果 TextView 值为空则失败

java - 如何从 AutoCompleteTextView 中选取文本并将其显示在 ListView 中?

android - 使用 SyncAdapter 同步联系人,工作

java - 为什么我在 JavaFX 上得到 “Not on FX application thread”?

java - 当 JDialog 可见时执行 SQL 语句?

java - JNI - 捕获异常并检查其类型

java - Android - 恢复对话框?

c++ - 共享内存中的std::mutex无法正常工作

android - 如何在 espresso webview 中选择下拉列表选项