Skip to main content

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. Entire device
  2. An Activity
  3. A View

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:

  1. canTakeScreenshot(): This method checks whether you can take a screenshot.
  2. 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:

Inside your instrumented testing class use these 3 methods:

ScreenshotDeviceInstrumentedTest.kt
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")
}
}
}

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:

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:

ScreenshotActivityInstrumentedTest
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")
}
}

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:

ScreenshotViewInstrumentedTest
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")
}
}

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:

app/build.gradle.kts
android {
defaultConfig {
// Add this line:
testInstrumentationRunnerArguments["useTestStorageService"] = "true"
}
}
dependencies {
// Add this line:
androidTestUtil("androidx.test.services:test-services:1.4.2")
}

References​