Selecting an alternative capture method
Testify provides three built-in bitmap capture methods. Each method will capture slightly different results based primarily on API level. In addition, a Fullscreen Capture Method is provided as an optional extension.
The three standard capture methods available are:
- DrawingCache: Pulls the view's drawing cache bitmap using the deprecated View.getDrawingCache (deprecated in API level 28).
- Canvas: Render the view (and all of its children) to a given Canvas, using View.draw
- PixelCopy: Use Android's recommended PixelCopy API to capture the full screen, including elevation.
caution
For legacy compatibility reasons, DrawingCache
mode is the default Testify capture method.
The default will change to PixelCopy
in a future release.
In addition, capture methods are extensible.
- Fullscreen: The Testify Fullscreen Capture Method can be used to capture UI elements presented outside of your root view. This includes elements rendered in a different Window such as dialogs, alerts, notifications, or overlays.
Code Configuration
To configure a custom capture method, provide a function that conforms to the CaptureMethod
signature. A CaptureMethod
returns a Bitmap
from the provided Activity
and View
.
typealias CaptureMethod = (activity: Activity, targetView: View?) -> Bitmap?
tip
See Customizing the captured bitmap for an example of how to provide your own CaptureMethod
.
Or, you can specify one of the provided methods from the dev.testify.core.processor.capture
package:
::canvasCapture
: Canvas::createBitmapFromDrawingCache
: DrawingCache::pixelCopyCapture
: PixelCopy
- ScreenshotTestRule
- ScreenshotScenarioRule
@ScreenshotInstrumentation
@Test
fun testDefault() {
rule
.configure {
captureMethod = ::canvasCapture
}
.assertSame()
}
@ScreenshotInstrumentation
@Test
fun testDefault() {
launchActivity<TestHarnessActivity>().use { scenario ->
rule
.withScenario(scenario)
.configure {
captureMethod = ::canvasCapture
}
.assertSame()
}
}
Manifest Configuration
danger
testify-pixelcopy-capture and testify-canvas-capture are deprecated and will be removed in a future version.
Alternatively, you can also select an alternative capture method in your manifest.
- testify-pixelcopy-capture: PixelCopy capture method
- testify-canvas-capture: Canvas capture method
<manifest package="dev.testify.sample"
xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<meta-data android:name="testify-canvas-capture" android:value="true" />
</application>
</manifest>