Taking Screenshots for Testing
It is possible to take a screenshot of your Android application when you're running instrumented testing.
Basic Usage​
It is possible to take a screenshot of:
1. Taking Screenshots of Entire Device​
To take a screenshot of an entire device you can use DeviceCapture
API. There are 2 main methods that you can utilize from DeviceCapture
API:
canTakeScreenshot()
: This method checks whether you can take a screenshot.takeScreenshot()
: This method takes the screenshot of the entire device as a Bitmap.
There is also a helper extension method that you might want to use:
writeToTestStorage()
: This extension method saves a Bitmap into test storage. If you're using this method, make sure to follow the test storage setup instruction below first.
Inside your instrumented testing class use these 3 methods:
- Classic
- Jetpack Compose
package com.hanmajid.androidnotebook
import androidx.test.core.app.canTakeScreenshot
import androidx.test.core.app.takeScreenshot
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
import org.junit.Rule
@RunWith(AndroidJUnit4::class)
@LargeTest
class ScreenshotDeviceInstrumentedTest {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)
@Test
fun testTakeScreenshot() {
if (canTakeScreenshot()) {
takeScreenshot().writeToTestStorage("my-screenshot")
}
}
}
package com.hanmajid.androidnotebook
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.core.app.canTakeScreenshot
import androidx.test.core.app.takeScreenshot
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.hanmajid.androidnotebook.ui.theme.MyApplicationTheme
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ScreenshotDeviceInstrumentedTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun myTest() {
composeTestRule.setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
if (canTakeScreenshot()) {
takeScreenshot().writeToTestStorage("my-screenshot")
}
}
}
Try running the instrumented testing.
If everything goes well, your screenshot will be created in your Internal Storage inside googletest/test_outputfiles
folder.
2. Taking Screenshots of an Activity​
To take a screenshot of an Activity you can use one these 2 methods depending on whether you're using Jetpack Compose or not:
captureToBitmap()
: (Classic) This method captures a node as a Bitmap.captureToImage()
: (Jetpack Compose) This method captures a node as a Bitmap.
There is also a helper extension method that you might want to use:
writeToTestStorage()
: This extension method saves a Bitmap into test storage. If you're using this method, make sure to follow the test storage setup instruction below first.
Inside your instrumented testing class use these 3 methods:
- Classic
- Jetpack Compose
package com.hanmajid.androidnotebook
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.screenshot.captureToBitmap
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@LargeTest
class ScreenshotActivityInstrumentedTest {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)
@Test
fun testTakeScreenshot() {
onView(isRoot())
.captureToBitmap()
.writeToTestStorage("my-screenshot")
}
}
package com.hanmajid.androidnotebook
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.hanmajid.androidnotebook.ui.theme.MyApplicationTheme
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ScreenshotActivityInstrumentedTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun myTest() {
composeTestRule.setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
composeTestRule.onRoot()
.captureToImage()
.asAndroidBitmap()
.writeToTestStorage("my-screenshot")
}
}
Try running the instrumented testing.
If everything goes well, your screenshot will be created in your Internal Storage inside googletest/test_outputfiles
folder.
3. Taking Screenshots of a View​
Taking a screenshot of View is quite similar with taking a screenshot of an Activity, you can use one of these 2 methods:
captureToBitmap()
: (Classic) This method captures a node as a Bitmap.captureToImage()
: (Jetpack Compose) This method captures a node as a Bitmap.
There is also a helper extension method that you might want to use:
writeToTestStorage()
: This extension method saves a Bitmap into test storage. If you're using this method, make sure to follow the test storage setup instruction below first.
Inside your instrumented testing class use these 3 methods:
- Classic
- Jetpack Compose
package com.hanmajid.androidnotebook
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.screenshot.captureToBitmap
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@LargeTest
class ScreenshotViewInstrumentedTest {
@get:Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)
@Test
fun testTakeScreenshot() {
onView(withText("Hello Android!"))
.captureToBitmap()
.writeToTestStorage("my-screenshot")
}
}
package com.hanmajid.androidnotebook
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.hanmajid.androidnotebook.ui.theme.MyApplicationTheme
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class ScreenshotViewInstrumentedTest {
@get:Rule
val composeTestRule = createComposeRule()
@Test
fun myTest() {
composeTestRule.setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
}
}
composeTestRule.onNodeWithText("Hello Android!")
.captureToImage()
.asAndroidBitmap()
.writeToTestStorage("my-screenshot")
}
}
Try running the instrumented testing.
If everything goes well, your screenshot will be created in your Internal Storage inside googletest/test_outputfiles
folder.
Test Storage Setup​
If you are using writeToTestStorage()
extension method, you need to adjust your app/build.gradle
file like this first before running the instrumented testing:
- Kotlin DSL
- Groovy
android {
defaultConfig {
// Add this line:
testInstrumentationRunnerArguments["useTestStorageService"] = "true"
}
}
dependencies {
// Add this line:
androidTestUtil("androidx.test.services:test-services:1.4.2")
}
android {
defaultConfig {
// Add this line:
testInstrumentationRunnerArguments useTestStorageService: 'true'
}
}
dependencies {
// Add this line:
androidTestUtil 'androidx.test.services:test-services:1.4.2'
}