简体   繁体   中英

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()); , then 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. 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. 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.

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 :

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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