Automating deep linking using android espresso

血红的双手。 提交于 2019-11-29 11:29:54

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!