Is it possible to write tests across several activities using the android espresso framework?
4 Answers
Yes, it is possible. In one of the samples they have demoed this here https://github.com/googlesamples/android-testing/blob/master/ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java
@Test
public void changeText_newActivity() {
// Type text and then press the button.
onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED),
closeSoftKeyboard());
onView(withId(R.id.activityChangeTextBtn)).perform(click());
// This view is in a different Activity, no need to tell Espresso.
onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)));
}
Read the inline comment.
Waiting for the new activity to load is taken care of implicitly by Espresso.

- 2,148
- 1
- 22
- 29
-
Is it still possible now? with Espresso 2. I have some kind of application like that when user presses on the file it moves to another activity and I need to test that UI. Any Ideas? – optimus prime Nov 18 '16 at 06:53
-
1@AQU I have not worked with `Espresso` for a long time now. I recommend going through the examples of Espresso 2. – Jigish Chawda Nov 28 '16 at 06:30
-
Yaa, it's working for me now. I am able to do all the things that I want. – optimus prime Nov 28 '16 at 09:50
-
Well, my test can't find the views of the second activity and crushed. – c-an Aug 09 '21 at 09:51
It is absolutely possible to write an Espresso (or any instrumentation based) test that crosses multiple Activities. You have to start out with one Activity, but can navigate through the UI of your application to other Activities. The only caveat - due to security restrictions, the test flow must stay within your application's process.

- 3,767
- 2
- 15
- 12
-
How do I assert that once I clicked an widget the correct activity got opened? – Bolhoso Dec 17 '13 at 15:56
-
6Like the app user would - assert that a particular view from that activity is displayed. – ValeraZakharov Dec 18 '13 at 22:21
I've tested this like:
onView(withId(R.id.hello_visitor)).perform(click());
pressBack();
onView(withId(R.id.hello_visitor)).check(matches(isDisplayed())); //fails here
The click action starts a new activity, obviously.

- 54,294
- 25
- 151
- 185

- 161
- 1
- 10
Let's say you have two activities: HomeActivity and SearchResultsActivity. For the test, you want to do some actions on HomeActivity, and verify the result on SearchResultsActivity. Then the test will be written like below:
public class SearchTest extends ActivityInstrumentationTestCase2<HomeActivity> {
public SearchTest() {
super(HomeActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
getActivity(); // launch HomeActivity
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSearch() {
onView(withId(R.id.edit_text_search_input)).perform(typeText("Hello World"));
onView(withId(R.id.button_search)).perform(click());
// at this point, another activity SearchResultsActivity is started
onView(withId(R.id.text_view_search_result)).check(matches(withText(containsString("Hello World"))));
}
}
So the only thing you need to care, is that you should extends the test class from ActivityInstrumentationTestCase2<FirstActivity>, and call super(FirstActivity.class) in your constructor.
Above example is fairly easy.
Advance example (when startActivityForResult happens):
Sometimes it's really confusing to write a test, where you still have two activities A and B, and the application-flow is different than above:
- user does nothing on activity A, but activity A calls startActivityForResult to launch activity B;
- then user makes some inputs and clicks on activity B (this part is the real test);
- finally activity B exits, it calls setResult and resumes activity A (you need to verify result here).
Even though the whole testing part happens on activity B, you may just need to verify one tiny piece on activity A, but your test should extend from ActivityInstrumentationTestCase2<ActivityWhoCallsStartActivityForResult> which is activity A, but not activity B. Otherwise, when test part is done, activity A won't be resumed, you have no chance to verify your result.

- 14,547
- 7
- 57
- 69