Testing Deep Linking with Espresso and Burst

Singwai Chan
3 min readMay 24, 2017

--

The goal of this article is to

1 — Write an Espresso test that can test an intent with specific URI (deep link) and make sure the intent is resolvable by the specific activity in the app.

2 — Use Burst to set up a list of test cases so test cases are more readable and maintainable.

Burst Overview:

Burst (https://github.com/square/burst) is a testing tool made by Square. It’s a “A unit testing library for varying test data.” It helps write unit test that have a lot of different input. The idea is that it abstracts the input from the test case via enums. In addition, when there are 2 or more set of enums used in test, It runs test against all permutation of different enums as we will see in the example.

Get Started:

Add the intent filter to the activity that will handle the deep link

Add Intent Filter to the Main Activity

Since intent filter doesn’t support full regex, we will need to add multiples host and schema separtately in order to support all the url variations (http://, https://, http://www., https://www.). See https://developer.android.com/guide/topics/manifest/data-element.html

After adding the intent filter, we can deploy and test with below via adb command to make sure we have the deep link set up properly. Since the deep link is also resolvable by browser, we should see the browser and the app can both handle the intent on the device.

List of apps that can resolves that collection deep link

The command to test the deep link

adb shell am start -W -a android.intent.action.VIEW -d  "http://bloglovin.com/collections/123123/"

The output of the command

Starting: Intent { act=android.intent.action.VIEW dat=http://bloglovin.com/... }
Status: ok
Activity: android/com.android.internal.app.ResolverActivity
ThisTime: 252
TotalTime: 252
WaitTime: 288
Complete

After we played with the adb command, we can add burst dependency to our android test compile under our app’s gradle dependencies

androidTestCompile 'com.squareup.burst:burst-junit4:1.1.1'
androidTestCompile 'com.squareup.burst:burst:1.1.1'

Under the androidTest flavor, we can add the test by creating a new Java class name DeeplinkTest.java

Android test flavor

Here is the actual testing code

  1. enum for different type of schema with domain we want to test (line 25–37)
  2. enum for different type of test case, which contain the path and the expected result(line 40–52)
  3. In the test class, we have 2 variables for the enums, and annotate them with @Burst so when the test run, Burst will inject the proper enum in it. (line 54–57)
  4. In the test function, we assemble the domain and the path to generate the link, query against package manage to find all the activities that can handle the url. Loop though the resolve and make sure our exact package with the exact activity is in it and (line 60–77)

If we introduce new deep link, we can just add them in the DeeplinkTestCase enum.

We mentioned earlier about burst will run though all the permutation of the enums. In the given example, we will have total of 10 test cases. (5 domains x 2 paths) If we work with test with multiple dimensions, the effort of covering all the test cases will be exponential.

Bloglovin’s Use Case:

With this technique, the Bloglovin’ code base can test ~80 deep link format in 130 lines of code. That is less than 2 line per test! And adding a new test is as simple as creating another configuration.

Full source code is available at https://github.com/Singwai/Burst-Espresso

Foot note:

https://github.com/square/burst
http://blog.xebia.com/android-intent-extras-espresso-rules/

--

--