android - 如何用 espresso 测试 DeepLinks

标签 android android-intent android-espresso

我想为我所有的深层链接添加测试,以启动它们并查看是否启动了所需的 Activity 并对 Activity 中显示的内容进行断言。

我该怎么做?

编辑

我最终只测试了深度链接匹配,如 https://medium.com/@singwai/testing-deep-linking-with-espresso-and-burst-5e1bdb3c5e29说。

在 Kotlin 中:

@Throws(Exception::class)
fun test_deepLink_isResolvedBy(url: String, canonicalActivityName: String) {
    val appContext = InstrumentationRegistry.getTargetContext()
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    val resolvedActivities =
        appContext.packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL)

    val resolverActivityMissing = resolvedActivities.none {
      it.activityInfo.packageName == appContext.packageName &&
          it.activityInfo.name == canonicalActivityName
    }

    if (resolverActivityMissing) {
      fail("$url is not resolved for $canonicalActivityName")
    }
}

然后我检查是否有解析我的 url 的 Activity ,并在其自己的测试中测试每个 Activity 。

最佳答案

应该能够使用如下内容:

launchActivityWithIntent(getActivity().getPackageName(),
        YourActivityThatHandlesDeepLink.class,
        new Intent(Intent.ACTION_VIEW).setData(Uri.parse(link)));

关于android - 如何用 espresso 测试 DeepLinks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47577635/

相关文章:

java - 未指定尺寸的 Android 裁剪图像

java - 尝试打开文件时,不同 Android 版本的 URI 方案不同

java - 我如何使用 android espresso web 找到类名称中的所有元素并选择我需要的

android - 自定义对话框不显示,只是褪色的黑色背景

javascript - 音频元素 “currentTime”在Android 6设备上不起作用

android - 在 Android 项目中集成 MuPDF 库

android - 使用 Espresso stub Intent 时出错

android - AndEngine中的投影

android - 调用 Intent.getStringExtra() 会定期抛出 StackOverflowError

android - Seekbar 测试不调用 OnSeekBarChangeListener