How to write tests for deep links in Android?
Asked Answered
B

3

12

I would like to write tests for Android app with deep link cases using UI testing framework (Espresso) - launch app using only ACTION_VIEW intent and check all views on opened screen.

But looks like Espresso (even espresso-intents) doesn't have this functionality, and require to define Activity class.

I tried this way, but it doesn't work properly, because launched app twice - standard launch using AppLauncherActivity (required by Espresso) and launch via deep link.

@RunWith(AndroidJUnit4.class)
public class DeeplinkAppLauncherTest {

    @Rule
    public ActivityTestRule<AppLauncherActivity> activityRule = new ActivityTestRule<>(AppLauncherActivity.class);

    @Test
    public void testDeeplinkAfterScollDownAndBackUp() {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myapp://search/777"));
        activityRule.launchActivity(intent);

        onView(withId(R.id.search_panel)).check(matches(isDisplayed()));
    }

}

I would like to launch testing app using only deep link without standard launch. Do you know, how to do it?

Breadnut answered 22/3, 2017 at 12:8 Comment(1)
I already replied to same kind of question here #44074673. Hope it helps!Nicias
B
8

I found one option - just added deep link opening parameters for existed intent and use standard activity launch:

@Rule
public ActivityTestRule<AppLauncherActivity> activityRule = new ActivityTestRule<AppLauncherActivity>(AppLauncherActivity.class){
    @Override protected Intent getActivityIntent() {
        Intent intent = super.getActivityIntent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("myapp://search/777"));
        return intent;
    }
};
Breadnut answered 23/3, 2017 at 4:9 Comment(0)
S
3

Accepted answer is helpful, but nowadays the ActivityTestRule class has been deprecated.

We can use ActivityScenario instead.

Here is a Kotlin example:

class MyDeepLinkTest {

    private lateinit var scenario: ActivityScenario<LoadingActivity>

    @Before
    fun setUp() {
        Intents.init()
    }

    @After
    fun tearDown() {
        Intents.release()
        scenario.close()
    }

    @Test
    fun myTest() {
        val intent = Intent(ApplicationProvider.getApplicationContext(), LoadingActivity::class.java)
            .putExtra("example_extra1", "Value 1")
            .putExtra("example_extra2", 777)

        scenario = ActivityScenario.launch(intent)

        // Test code goes here (e.g. intent causes to start MainActivity)
        intended(hasComponent(MainActivity::class.java.name))
    }

}

I also found this blog post with additional examples.

Svelte answered 10/6, 2021 at 22:35 Comment(0)
G
2
@Rule
public ActivityTestRule<AppLauncherActivity> activityRule = new ActivityTestRule<>(AppLauncherActivity.class, false, false);

There are multiple constructors for creating an ActivityTestRule. The third one is launchActivity. Set it to false as shown above because you manually start that activity afterwards with activityRule.launchActivity(intent). This should prevent it from starting twice

Grazier answered 22/3, 2017 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.