Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso - check which Activity is opened using intent on button press?

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);

}
like image 375
metalink Avatar asked Aug 31 '16 13:08

metalink


People also ask

How do you test espresso intent?

Validate intentsUsing the intended() method, which is similar to Mockito. verify() , you can assert that a given intent has been seen. However, Espresso-Intents doesn't stub out responses to intents unless you explicitly configure it to do so. // User action that results in an external "phone" activity being launched.

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.


2 Answers

To actually match a started activity with Espresso intents you need to check for the component of the new intent:

intended(hasComponent(NewActivity.class.getName()));

Make sure to call Intents.init() in the setup and Intents.release() in teardown to be able to record intents with Espresso.

like image 73
Be_Negative Avatar answered Oct 22 '22 05:10

Be_Negative


Whether it is possible to trace which the Activity opened after pressing the button?

Check espresso-intents library:

Configuration

Add to your app/build.gradle these lines:

androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

NOTICE: espresso-intents won't run without espresso-core, runner or rules libs.

You may also need to change ActivityTestRule<> to IntentsTestRule as it is described here:

IntentsTestRule

Use IntentsTestRule instead of ActivityTestRule when using Espresso-Intents. IntentsTestRule makes it easy to use Espresso-Intents APIs in functional UI tests. This class is an extension of ActivityTestRule, which initializes Espresso-Intents before each test annotated with @Test and releases Espresso-Intents after each test run. The activity will be terminated after each test and this rule can be used in the same way as ActivityTestRule.

From: https://google.github.io/android-testing-support-library/docs/espresso/intents/

Example code (click on button to launch new activity)

Here's a solution using espresso-intents for similar problem:

An example test with intent stubbing:

@Test
 public void testActivityResultIsHandledProperly() {
   // Build a result to return when a particular activity is launched.
   Intent resultData = new Intent();
   String phoneNumber = "123-345-6789";
   resultData.putExtra("phone", phoneNumber);
   ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);

   // Set up result stubbing when an intent sent to "contacts" is seen.
   intending(toPackage("com.android.contacts")).respondWith(result));

   // User action that results in "contacts" activity being launched.
   // Launching activity expects phoneNumber to be returned and displays it on the screen.
   user.clickOnView(system.getView(R.id.pickButton));

   // Assert that data we set up above is shown.
   assertTrue(user.waitForText(phoneNumber));
 }

From: https://developer.android.com/reference/android/support/test/espresso/intent/Intents.html

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

like image 38
piotrek1543 Avatar answered Oct 22 '22 05:10

piotrek1543