下面是我的一个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)中的视图是否可见。如果可见,我将假定活动已成功打开。但它似乎并没有像我预期的那样工作。有没有更好的方法来检查新活动是否成功启动,而不是检查活动中的视图是否可见?谢谢
发布于 2016-03-08 15:05:24
您可以使用:
intended(hasComponent(YourExpectedActivity.class.getName()));需要此gradle条目:
androidTestCompile ("com.android.support.test.espresso:espresso-intents:$espressoVersion")intended()和hasComponent()的导入
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;正如Shubam Gupta所提到的,请记住在调用intended()之前先调用Intents.init()。您最终可以在@Before方法中调用它。
发布于 2017-12-14 08:14:46
尝试:
intended(hasComponent(YourActivity.class.getName()));
此外,请牢记
如果在intended()之前未调用Intents.init(),则引发java.lang.NullPointerException
发布于 2017-07-14 15:14:19
你可以这样做:
    @Test
public void testLoginAttempt() {
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@example.com"));
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));
    Intents.init();
    Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
    Intents.release();
}如果未调用Intents.init(),则抛出java.lang.NullPointerException。
https://stackoverflow.com/questions/25998659
复制相似问题