I am using Karumi’s shot plugin (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/
The library has an issue when running with api bigger then 23 because it needs the WRITE_EXTERNAL_STORAGE permission and since api 23, granting permissions during tests is not a trivial task.
But in espresso 3.0 was added the GrantPermissionRule and with this, you can set permissions previously to the execution of the test easily.
Well, I added the Rule:
@Rule @JvmField
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
And took the screenshot with the following code:
Screenshot.snapActivity(activityTestRule.activity).record()
I have a custom TestRunner that runs:
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)
}
But when I execute the test I receive the following error:
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)
2
Answers
The plugin fails trying to save the screenshots in an API >= 23 because the permission has to be granted in the testing APK and not the APK under test. Using the rule named grant permission test rule does not provide this functionality. This is not supported by the official Facebook library and we don’t support it for now 🙁
I’ve also answered your question in the GitHub repository https://github.com/Karumi/Shot/issues/19#issuecomment-328334528
Try to use
android:requestLegacyExternalStorage="true"
in your manifest during test.Dont forget to remove it once you finished