我刚开始使用 Espresso,在此之前我已经尝试过 Robotium。我需要测试 LoginActivity。逻辑是:
- 用户输入正确的凭据;
- 用户看到“Logging in..”字符串;
- 用户等待字符串消失;
- 用户在 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/