Espresso - 如何在执行特定操作后检查活动是否启动? [英] Espresso - How can I check if an activity is launched after performing a certain action?

查看:27
本文介绍了Espresso - 如何在执行特定操作后检查活动是否启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 Espresso 测试用例之一.

the following is one of my Espresso test cases.

    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)中的视图是否可见.如果可见,我将假设该活动已成功打开.但它似乎并没有像我预期的那样工作.有没有更好的方法来检查新活动是否成功启动,而不是检查该活动中的视图是否可见?谢谢

Currently what I did was to check if a view in the new activity (R.id.action_logout) is visibible or not. If visible, I will assume tha the activity opened successfully. But it doesn't seem to work as I expected. Is there a better way to check if a new activity is successfully launched instead of checking a view in that activity is visible? Thanks

推荐答案

您可以使用:

intended(hasComponent(YourExpectedActivity.class.getName()));

需要此 gradle 条目:

Requires this gradle entry:

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 方法中调用它.

as mentioned by Shubam Gupta please remember to call Intents.init() before calling intended(). You can eventually call it in the @Before method.

这篇关于Espresso - 如何在执行特定操作后检查活动是否启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆