首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

Stack Overflow用户

发布于 2017-11-28 02:16:00

确保Espresso意向库位于gradle依赖项中

代码语言:javascript
运行
复制
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.1"

然后将这两个文件导入到测试文件中

代码语言:javascript
运行
复制
import static android.support.test.espresso.intent.Intents.intended
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent

然后在测试类中添加IntentsTestRule

代码语言:javascript
运行
复制
@Rule
@JvmField
val mainActivityRule = IntentsTestRule(MainActivity::class.java)

最后,检查活动是否已启动意图

代码语言:javascript
运行
复制
@Test
fun launchActivityTest() {
    onView(ViewMatchers.withId(R.id.nav_wonderful_activity))
            .perform(click())

    intended(hasComponent(WonderfulActivity::class.java!!.getName()))
}
票数 10
EN
查看全部 8 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25998659

复制
相关文章

相似问题

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