简体   繁体   English

浓咖啡,测试第二项活动的启动

[英]Espresso, test launch of second activity

I would like to know how to test the launch of an activity on the click of a button. 我想知道如何通过单击按钮来测试活动的启动。 I'm aware that this could possibly be made with this: onView(withText(R.string.button_next_activity)).perform(click()); 我知道这可以用以下方法实现: onView(withText(R.string.button_next_activity)).perform(click()); , then onView(withId(R.id.second_tv_welcome)).check(matches(withText(R.string.second_tv_welcome))); ,然后onView(withId(R.id.second_tv_welcome)).check(matches(withText(R.string.second_tv_welcome))); where second_tv_welcome is a textView from the second activity. 其中second_tv_welcome是第二个活动的textView。 But my current activity is starting the facebook web view, I don't know what are the ids in this activity, plus I don't find this method of checking (checking if a textview exist and has the expected text) very clean. 但是我当前的活动是启动Facebook Web视图,我不知道此活动中的ID是什么,而且我发现这种检查方法(检查是否存在textview并具有期望的文本)不是很干净。 Any workaround for this? 任何解决方法? Thank you. 谢谢。

You should avoid using external services in functional tests. 您应该避免在功能测试中使用外部服务。 This includes making API calls, using GPS and things that do that internally, eg Facebook or Google+ login button. 这包括进行API调用,使用GPS以及内部执行的操作,例如Facebook或Google+登录按钮。

The way I go with it, is by creating two flavors and putting login button layout in each: one real, for production and one mock, which doesn't do anything except for notifying that login action succeeded (or failed), so the calling code may go to another Activity. 我采用的方法是创建两种样式,然后在每种样式中放置登录按钮布局:一种用于生产,另一种用于模拟,一种模拟,除了通知登录操作成功(或失败)外,不执行任何操作,因此调用代码可能会转到另一个活动。 This might sound hard to do properly at first, so you may want to have a look at the relevant code of my pet app: 刚开始听起来很难做到这一点,所以您可能想看看我的宠物应用程序的相关代码:

Two flavors in this app define stub_login folder as src/res folders in build.gradle : 在此应用程序两种形式定义stub_login文件夹中的src / RES文件夹的build.gradle

sourceSets {
    mock {
        java { srcDir 'src/stub_login/java' }
        res { srcDir 'src/stub_login/res' }
    }
    apiary {
        java { srcDir 'src/stub_login/java' }
        res { srcDir 'src/stub_login/res' }
    }
}

And tests (not too many of them for now) are run with: 测试 (目前还不多)运行:

./gradlew connectedAndroidTestMockDebug

Note that functional tests are in src/androidTestMock and unit tests may stay in src/androidTest . 请注意,功能测试位于src/androidTestMock而单元测试则可能位于src/androidTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM