-->

Automating deep linking using android espresso

2019-01-26 19:41发布

问题:

I want to write espresso scripts to test deep linking and have no idea how to begin with. Looking for solutions that'll help me get more idea, possibly step by step procedure on how to get started.

For ex : I am looking for a scenario like you get a link in gmail tapping on which user should be directed towards the mobile app. How do I get started to test something like this using espresso ?

Thanks in advance.

回答1:

Start with an activity rule

 @Rule
 public ActivityTestRule<YourAppMainActivity> mActivityRule =
            new ActivityTestRule<>(YourAppMainActivity.class, true, false);

Then you want something to parse the uri from the link and return the intent

String uri = "http://your_deep_link_from_gmail"; 
private Intent getDeepLinkIntent(String uri){
        Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(uri))
                .setPackage(getTargetContext().getPackageName());


        return intent;
    }

Then you want the activity rule to launch the intent

Intent intent = getDeepLinkIntent(deepLinkUri);
mActivityRule.launchActivity(intent);


回答2:

Well IntentTestRule doesn't work properly. So I will try like this with an ActivityTestRule :

public ActivityTestRule<MyActivity> activityTestRule = new ActivityTestRule<MyActivity>(MyActivity.class, false, false);

And then I will write the proper UI Unit Test to be something like this:

@Test
public void testDeeplinkingFilledValue(){
        Intent intent = new Intent(InstrumentationRegistry.getInstrumentation()
                .getTargetContext(), MyActivity.class );

        Uri data = new Uri.Builder().appendQueryParameter("clientName", "Client123").build();
        intent.setData(data);

        Intents.init();
        activityTestRule.launchActivity(intent);


        intended(allOf(
                hasComponent(new ComponentName(getTargetContext(), MyActivity.class)),
                hasExtras(allOf(
                        hasEntry(equalTo("clientName"), equalTo("Client123"))
                ))));
        Intents.release();
}

With this you are going to test that the deeplink with a given query parameter is actually being retrieved correctly by your activity that is handling the Intent for the deeplinking.