我使用Karumi的射門插件(https://github.com/karumi/shot)從我的測試採取截圖和比較,然後使用Facebook的庫:http://facebook.github.io/screenshot-tests-for-android/Facebook`s截圖測試符合API失敗大於23
的庫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)