Facebook 的屏幕截图测试失败,api 大于 23

Facebook`s screenshot tests failing with api bigger than 23

我正在使用 Karumi 的镜头插件 (https://github.com/karumi/shot) to take screenshots from my tests and compare then using facebook's library: http://facebook.github.io/screenshot-tests-for-android/

库在 运行 api 大于 23 时出现问题,因为它需要 WRITE_EXTERNAL_STORAGE 权限,并且由于 api 23,在测试期间授予权限不是琐碎的任务。

但在 espresso 3.0 中添加了 GrantPermissionRule,您可以轻松地在执行测试之前设置权限。

嗯,我添加了规则:

@Rule @JvmField
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)

并使用以下代码截图:

Screenshot.snapActivity(activityTestRule.activity).record()

我有一个运行的自定义 TestRunner:

override fun onCreate(args: Bundle) {
    super.onCreate(args)
    ScreenshotRunner.onCreate(this, args)
}

override fun finish(resultCode: Int, results: Bundle) {
    ScreenshotRunner.onDestroy()
    super.finish(resultCode, results)
}

但是当我执行测试时,我收到以下错误:

java.lang.RuntimeException: Failed to create the directory for screenshots. Is your sdcard directory read-only?
    at com.facebook.testing.screenshot.internal.ScreenshotDirectories.getSdcardDir(ScreenshotDirectories.java:66)

该插件尝试将屏幕截图保存在 API >= 23 中失败,因为必须在测试 APK 中而不是在测试中的 APK 中授予权限。使用名为 grant permission test rule 的规则不提供此功能。这不受官方 Facebook 库的支持,我们暂时不支持它:(

我也在 GitHub 存储库 https://github.com/Karumi/Shot/issues/19#issuecomment-328334528

中回答了你的问题

尝试在测试期间在您的清单中使用 android:requestLegacyExternalStorage="true"

完成后别忘了将其删除