从 Android Espresso libs 3.6.0 开始,
captureToBitmap
已更改
fun ViewInteraction.captureToBitmap(): Bitmap {
使用它
onView(isRoot()).captureToBitmap().writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-open")
我才发现
class CaptureToBitmapAction(val bitmapReceiver: ViewActions.BitmapReceiver) : ViewAction {
所以我尝试了
class BitmapReceiver(name: String) : ViewActions.BitmapReceiver {
override fun onBitmapCaptured(bitmap: Bitmap?) {
TODO("Not yet implemented")
}
}
val receiver = BitmapReceiver("${javaClass.simpleName}_${nameRule.methodName}-open")
onView(isRoot()).perform(CaptureToBitmapAction(receiver))
但是我不知道该做什么
onBitmapCaptured
现在的问题是,如何使用 3.6.x 进行 Espresso 测试屏幕截图?
我让它工作了
import android.graphics.Bitmap
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.action.ViewActions
class BitmapReceiver(val name: String) : ViewActions.BitmapReceiver {
override fun onBitmapCaptured(bitmap: Bitmap?) {
bitmap?.writeToTestStorage(name)
}
}
使用它
val receiver = BitmapReceiver("${javaClass.simpleName}_${nameRule.methodName}-open")
onView(isRoot()).perform(CaptureToBitmapAction(receiver))