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?
Assuming you already have an activity ready with the required intents what you'll to do is check your activity's if(getIntent(). getAction() != null) which means it was launched via a deep-link. Normal intents used for navigation will return null .
A Deep Link is a URL link that is generated, when anyone clicks on that link our app will be open with a specific activity or a screen. Using this URL we can send a message to our app with parameters. In WhatsApp, we can generate a deep link to send a message to a phone number with some message in it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With