以下是我的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)中的视图是否可见.如果可见,我将假设活动成功打开.但它似乎没有像我预期的那样工作.有没有更好的方法来检查是否成功启动了新活动而不是检查该活动中的视图是否可见?谢谢
如果测试跨越多个活动,是否有办法获得当前活动?
getActivtiy()方法只提供一个用于启动测试的活动.
我尝试过类似下面的内容,
public Activity getCurrentActivity() {
    Activity activity = null;
    ActivityManager am = (ActivityManager) this.getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    try {
        Class<?> myClass = taskInfo.get(0).topActivity.getClass();
        activity = (Activity) myClass.newInstance();
    }
    catch (Exception e) {
    }
    return activity;
}
但我得到了空对象.
它过2次周以来我学习咖啡,我无法把握intending和intended.什么时候使用intending和intended?提供的示例和在线教程没有帮助,研究网络对我造成的伤害大于好处.
在语义上,为了智力参考,为什么它intend-ing和另一个intended,这更加增加了混乱.这是另一个谷歌命名错误还是仅仅是我?这两种方法真的没有意义.
我误解了它的用法.我想测试我的活动是否A启动了活动B.而已.这是我的代码:
@Test
public void shouldLaunchTagListActivity()
{
    onView(withId(R.id.edittext_description_minimized))
            .perform(click());
    onView(withId(R.id.linearlayout_add_note_maximize))
            .check(matches(isDisplayed()));
    onView(withId(R.id.relativelayout_quick_action_button))
            .check(matches(isDisplayed()));
    onView(withId(R.id.imagebutton_tag))
            .perform(click());
    // should I intended or intending here?
    // ???
    intended(toPackage(HomeScreenActivity.class.getName()));
    onView(withId(R.id.coordinatorlayout_tag_list))
            .check(matches(isDisplayed()));
}
即使我用错误的目标取代意图,这个测试总是通过.
我可以通过检查我的目标视图是否存在并且是否被用户看到来检查是否已启动其他活动.但现在我将运行一个不同的用户故事,我真的需要检查活动是否发送了请求以启动另一个活动(我的活动,而不是外部).
非常感谢任何解释!