首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Espresso -如何在执行某个操作后检查活动是否启动?

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

Stack Overflow用户
提问于 2014-09-23 23:12:07
回答 8查看 50.7K关注 0票数 62

下面是我的一个Espresso测试用例。

代码语言:javascript
运行
复制
    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)中的视图是否可见。如果可见,我将假定活动已成功打开。但它似乎并没有像我预期的那样工作。有没有更好的方法来检查新活动是否成功启动,而不是检查活动中的视图是否可见?谢谢

EN

回答 8

Stack Overflow用户

发布于 2016-03-08 15:05:24

您可以使用:

代码语言:javascript
运行
复制
intended(hasComponent(YourExpectedActivity.class.getName()));

需要此gradle条目:

代码语言:javascript
运行
复制
androidTestCompile ("com.android.support.test.espresso:espresso-intents:$espressoVersion")

intended()hasComponent()的导入

代码语言:javascript
运行
复制
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方法中调用它。

票数 76
EN

Stack Overflow用户

发布于 2017-12-14 08:14:46

尝试:

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

此外,请牢记

如果在intended()之前未调用Intents.init(),则引发java.lang.NullPointerException

票数 24
EN

Stack Overflow用户

发布于 2017-07-14 15:14:19

你可以这样做:

代码语言:javascript
运行
复制
    @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

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25998659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档