Is it possible to trace which Activity is opened after a certain button is pressed?
I have a test in which, when a button is clicked / pressed, it sends a request to the server. Till the time the request is sent, it opens an Activity. To verify the successful execution of the test, I need to check what is the open Activity.
Example of my test:
Check which Intent is opened in Espresso ---
private void startTest() {
recreateAuthData(InstrumentationRegistry.getTargetContext(), "d78269d9-9e00-4b8d-9242-815204b0a2f6", "3f32da21-914d-4adc-b6a1-891b842a2972");
InstrumentationRegistry.getTargetContext().getSharedPreferences(ActivitySplashScreen.class.getSimpleName(),
Context.MODE_PRIVATE).edit().putInt(ActivitySplashScreen.PROPERTY_APP_VERSION, ActivitySplashScreen.getAppVersion(InstrumentationRegistry.getTargetContext())).commit();
InstrumentationRegistry.getTargetContext().getSharedPreferences(ActivitySplashScreen.class.getSimpleName(),
Context.MODE_PRIVATE).edit().putString(ActivitySplashScreen.PROPERTY_REG_ID, "testKey").commit();
mActivityRule.launchActivity(setIntent());
// inputPinCode("2794");
}
@Test
public void testIdent() {
startTest();
onView(withText("ПРО")).perform(click());
putDelay(500);
onView(withId(R.id.get_pro)).perform(click());
onView(withText("Авторизация по паспортным данным")).perform(click());
putDelay(500);
closeSoftKeyboard();
onView(withId(R.id.btn_goto_passport)).perform(click());
onView(withHint("Серия и номер паспорта")).perform(replaceText("9894657891"));
onView(withHint("Дата выдачи паспорта")).perform(replaceText("17032014"));
onView(withHint("Дата рождения")).perform(replaceText("31091994"));
onView(withHint("СНИЛС")).perform(replaceText("54665285919"));
putDelay(500);
Log.d("TestWidget", hasComponent(hasShortClassName("ActivityMain")).toString());
onView(withId(R.id.btn_next)).perform(click());
// some code which check which activity is display now
putDelay(500);
}
Check
espresso-intents
library:Configuration
Add to your
app/build.gradle
these lines:You may also need to change
Example code (click on button to launch new activity)ActivityTestRule<>
toIntentsTestRule
as it is described here:Here's a solution using
espresso-intents
for similar problem:Additional resources:
[Android Developers] Espresso Intents Reference
[Github || Google Samples] Basic sample for Espresso Intents
[Github] Android Espresso Intent Sample
Testing for Android Intents using Espresso
[Gist] Example of how to use espresso-intents in Android tests - source code for link above
To actually match a started activity with espresso intents you need to check for the component of the new intent:
Make sure to call
Intents.init()
in the setup andIntents.release()
in teardown to be able to record intents with espresso.