相关疑难解决方法(0)

Espresso - 如何在执行某项操作后检查某项活动是否已启动?

以下是我的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())));

    }
Run Code Online (Sandbox Code Playgroud)

目前我所做的是检查新活动(R.id.action_logout)中的视图是否可见.如果可见,我将假设活动成功打开.但它似乎没有像我预期的那样工作.有没有更好的方法来检查是否成功启动了新活动而不是检查该活动中的视图是否可见?谢谢

android android-activity android-espresso

56
推荐指数
6
解决办法
4万
查看次数

在Espresso android中获取当前活动

如果测试跨越多个活动,是否有办法获得当前活动?

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;
}
Run Code Online (Sandbox Code Playgroud)

但我得到了空对象.

android ui-automation android-espresso

41
推荐指数
6
解决办法
3万
查看次数

什么意思与Espresso之间的意图有什么区别?

它过2次周以来我学习咖啡,我无法把握intendingintended.什么时候使用intendingintended?提供的示例和在线教程没有帮助,研究网络对我造成的伤害大于好处.

在语义上,为了智力参考,为什么它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()));
}
Run Code Online (Sandbox Code Playgroud)

即使我用错误的目标取代意图,这个测试总是通过.

我可以通过检查我的目标视图是否存在并且是否被用户看到来检查是否已启动其他活动.但现在我将运行一个不同的用户故事,我真的需要检查活动是否发送了请求以启动另一个活动(我的活动,而不是外部).

非常感谢任何解释!

android android-espresso

7
推荐指数
1
解决办法
4160
查看次数