Wednesday 10 February 2016

How to Test Dialer Activity with Espresso Intent?



If you are testing an application which talks to other applications like camera or dialer then Espresso Intent will help you to do so. Espresso-Intents is an extension of Espresso which allows validation and stubbing of Intents sent out  by the application. As you are familiar with Espresso now then using Espresso Intent will be very easy for you instead of using ActivityTestRule use IntenntsTestRule as it allows you use intent APIs for functional UI testing.

Espresso Intents monitors all the intents which attempts to launch activities from the application under test and you can validate that intend is seen using intended API.You can also send response with the launched intent activity and you can use hamcrest matchers for your assertions.

Below is screen-shot of simple app on click it launches the dialer activity. I will explain how can write test case to test such activity with Espresso Intent.

Dialer Activity Testing

Test Dialer with Espresso

1. Setting up Intent is the first step but before that you should have Espresso Setup which I explain in this post. Open your build.gradle of your app and then add below line in dependencies and Sync.

androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.1')

 2. At the start of your test case use IntentTestRule as shown in the test case.

3. Declare variables for phone number and dialer package.

4. First line of code is used for intent stubbing to black external intents.

5. Write the steps to enter the phone number and click on call button.

6. The intended API is used to launch the activity ,send data with it and to validate that the intended activity is launched.





@RunWith(AndroidJUnit4.class)
@LargeTestpublic class DialerIntentTest {

    @Rule    public IntentsTestRule<DialerActivity> mActivityRule = new IntentsTestRule<>(
            DialerActivity.class);

    private static final String PHONE_NUMBER = "1234567890";
    private static final Uri INTENT_DATA_PHONE_NUMBER = Uri.parse("tel:" + PHONE_NUMBER);
    private static String PACKAGE_ANDROID_DIALER = "com.android.phone";

    static {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Starting with Android Lollipop the dialer package has changed. 

            PACKAGE_ANDROID_DIALER = "com.android.server.telecom";
        }
    }

    @Test    public void testDialerIntent()throws Exception
    {
        intending(not(isInternal())).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
        onView(withId(R.id.edit_text_caller_number)).perform(typeText(PHONE_NUMBER));
        onView(withId(R.id.button_call_number)).perform(click());
        intended(allOf(
                hasAction(Intent.ACTION_CALL),
                hasData(INTENT_DATA_PHONE_NUMBER),
                toPackage(PACKAGE_ANDROID_DIALER)));
    }


}
I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)

3 comments:


  1. java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference

    ReplyDelete

  2. java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference

    ReplyDelete
  3. Beverages that are gainful for our well being to keep our body and skin hydrated and sound constantly. Well being must be the primary need so dependably be cognizant first about it. I truly welcome the for assignment writing services reviews this intriguing article to us.

    ReplyDelete