下面是我的一个Espresso测试用例。
    public void testLoginAttempt() {
        Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com"));
        Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));
        Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
        // AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP.
        // Clicking launches a new activity that shows the text entered above. You don't need to do
        // anything special to handle the activity transitions. Espresso takes care of waiting for the
        // new activity to be resumed and its view hierarchy to be laid out.
        Espresso.onView(ViewMatchers.withId(R.id.action_logout))
                .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
    }目前,我所做的是检查新活动(R.id.action_logout)中的视图是否可见。如果可见,我将假定活动已成功打开。但它似乎并没有像我预期的那样工作。有没有更好的方法来检查新活动是否成功启动,而不是检查活动中的视图是否可见?谢谢
发布于 2017-11-28 02:16:00
确保Espresso意向库位于gradle依赖项中
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.1"然后将这两个文件导入到测试文件中
import static android.support.test.espresso.intent.Intents.intended
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent然后在测试类中添加IntentsTestRule
@Rule
@JvmField
val mainActivityRule = IntentsTestRule(MainActivity::class.java)最后,检查活动是否已启动意图
@Test
fun launchActivityTest() {
    onView(ViewMatchers.withId(R.id.nav_wonderful_activity))
            .perform(click())
    intended(hasComponent(WonderfulActivity::class.java!!.getName()))
}https://stackoverflow.com/questions/25998659
复制相似问题