如何为 Android 中的深层链接编写测试?
How to write tests for deep links in Android?
我想使用 UI 测试框架 (Espresso) 为 Android app with deep link cases 编写测试 - 仅使用 ACTION_VIEW intent 启动应用程序并检查打开屏幕上的所有视图。
但看起来 Espresso(甚至是 espresso-intents)没有这个功能,需要定义 Activity class.
我尝试过这种方式,但它无法正常工作,因为启动了两次应用程序 - 使用 AppLauncherActivity 标准启动(Espresso 要求)并通过深度启动 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()));
}
}
我想在没有标准启动的情况下仅使用深度 link 启动测试应用程序。
你知道怎么做吗?
@Rule
public ActivityTestRule<AppLauncherActivity> activityRule = new ActivityTestRule<>(AppLauncherActivity.class, false, false);
有多个用于创建 ActivityTestRule 的构造函数。第三个是launchActivity
。如上所示将其设置为 false,因为您随后使用 activityRule.launchActivity(intent)
手动启动了 activity。这应该可以防止它启动两次
我找到了一个选项 - 只是为现有意图添加了深度 link 开放参数并使用标准 activity 启动:
@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;
}
};
接受的答案很有帮助,但如今 ActivityTestRule
class 已 deprecated。
我们可以用ActivityScenario
代替。
这是一个 Kotlin 示例:
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))
}
}
我还找到了这个 blog post 以及其他示例。
我想使用 UI 测试框架 (Espresso) 为 Android app with deep link cases 编写测试 - 仅使用 ACTION_VIEW intent 启动应用程序并检查打开屏幕上的所有视图。
但看起来 Espresso(甚至是 espresso-intents)没有这个功能,需要定义 Activity class.
我尝试过这种方式,但它无法正常工作,因为启动了两次应用程序 - 使用 AppLauncherActivity 标准启动(Espresso 要求)并通过深度启动 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()));
}
}
我想在没有标准启动的情况下仅使用深度 link 启动测试应用程序。 你知道怎么做吗?
@Rule
public ActivityTestRule<AppLauncherActivity> activityRule = new ActivityTestRule<>(AppLauncherActivity.class, false, false);
有多个用于创建 ActivityTestRule 的构造函数。第三个是launchActivity
。如上所示将其设置为 false,因为您随后使用 activityRule.launchActivity(intent)
手动启动了 activity。这应该可以防止它启动两次
我找到了一个选项 - 只是为现有意图添加了深度 link 开放参数并使用标准 activity 启动:
@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;
}
};
接受的答案很有帮助,但如今 ActivityTestRule
class 已 deprecated。
我们可以用ActivityScenario
代替。
这是一个 Kotlin 示例:
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))
}
}
我还找到了这个 blog post 以及其他示例。