如何为 Android 中的深层链接编写测试? [英] How to write tests for deep links in Android?
问题描述
我想为 Android 应用编写具有深层链接案例的测试 使用 UI 测试框架 (Espresso) - 仅使用 ACTION_VIEW 意图启动应用程序并检查打开屏幕上的所有视图.
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.
但是看起来 Espresso(甚至 espresso-intents)没有这个功能,需要定义 Activity 类.
But looks like Espresso (even espresso-intents) doesn't have this functionality, and require to define Activity class.
我尝试过这种方式,但它不能正常工作,因为启动了两次应用程序 - 使用 AppLauncherActivity 标准启动(Espresso 需要)并通过深层链接启动.
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?
推荐答案
我找到了一个选项 - 只是为现有意图添加了深层链接打开参数并使用标准活动启动:
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;
}
};
这篇关于如何为 Android 中的深层链接编写测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!