Automating deep linking using android espresso

前端 未结 2 885
情深已故
情深已故 2020-12-19 21:05

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 procedu

相关标签:
2条回答
  • 2020-12-19 21:27

    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);
    
    0 讨论(0)
  • 2020-12-19 21:42

    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.

    0 讨论(0)
提交回复
热议问题