Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & App Percy

Capture screenshots

As part of debugging, capturing screenshots makes it easier to identify the exact step in your test where the failure occurred. These screenshots also help in identifying any layout or design-related issues in your application.

You can capture screenshots using either of the following two ways:

  • Native screenshot feature: A native screenshot feature in App Automate to capture screenshots. This feature is supported with all Android versions.
  • Spoon library: The screenshot feature provided as part of the Spoon library. This feature is supported only with Android 10 and lower versions.

Capture screenshots of your test execution

Use the information in the following tabs to edit your scripts and capture screenshots.

1. Add the native screenshots file to your project

Note: Check out our Sample Espresso project that uses the native screenshot feature to capture screenshots of test execution.

Create a file named NativeScreenshot.java in your project directory where your test files are located and add the following code to this file:

Copy icon Copy snippet
Copy icon Copy snippet

2. Edit your test scripts to take screenshots

Call the NativeScreenshot.capture("tag") method to capture screenshots from your Espresso tests, as shown in the following sample code snippet.

The tag parameter is used to provide a name to the screenshot on your App Automate’s session detail page.

@Test
  public void ensureAdditionWorks() {
    // test actions
    onView(withId(R.id.buttonOne)).perform(click());
    onView(withId(R.id.buttonTwo)).perform(click());
    onView(withId(R.id.buttonAdd)).perform(click());
    // other test actions ...
    onView(withId(R.id.editText)).check(matches(withText("33")));

    // capture the screenshot and add the 'post_addition' tag to the screenshot
    NativeScreenshot.capture("post_addition");
  }

3. Enable screenshots on BrowserStack

Add the debugscreenshots parameter in the REST API request to enable screenshots on your Espresso test session.

The following sample cURL command shows the parameters used to enable screenshots on Android v7.1:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/espresso/v2/build" \
-d '{"debugscreenshots": true, "devices": ["Google Pixel-7.1"], "app": "<app_id>", "testSuite": "<test_suite_id>"}' \
-H "Content-Type: application/json"

The following table lists the parameters used in the cURL request for enabling screenshots:

Parameter Description Values
debugscreenshots Capture screenshots during Espresso test execution true, false
Default: false
devices Name and OS version of the device to run the test on For example, Google Pixel-7.1.
Check out the list of devices available in App Automate.
app ID of the application you receive when you upload your application on App Automate For example, bs://3205774d1560ee3f5acdf
testSuite ID of the test suite you receive when you upload your test suite on App Automate For example, bs://4205214d1560we3f5acdfe

4. View screenshots of the test execution

Navigate to your App Automate dashboard. On your session detail page, click the Screenshots tab. You can view screenshots for each test case as shown in the following image:

View screenshots for your test execution on dashboard

The name of the screenshot is set to the tag value that you provided inside NativeScreenshot.capture("tag") method.

1. Add required dependencies to your project

Follow the instructions mentioned on the Spoon project’s page to add required dependencies to your project.

2. Change storage permissions

Add the WRITE_EXTERNAL_STORAGE permissions in your app’s AndroidManifest.xml file to save screenshots, as shown in the following sample code snippet:

AndroidManifest.xml
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
</manifest>
Note: If you’re targeting your app on Android v10, add the android:requestLegacyExternalStorage="true" tag in the Application tag of the AndroidManifest.xml file.

3. Edit your test scripts to take screenshots

Call the Spoon.screenshot(activityName, tag) method to capture screenshots from your Espresso tests, as shown in the following sample code snippet.

The tag parameter is used to provide a name to the screenshot on your App Automate’s session detail page.

@Test
public void ensureAdditionWorks() {
    onView(withId(R.id.buttonOne)).perform(click());
    onView(withId(R.id.buttonTwo)).perform(click());
    onView(withId(R.id.buttonAdd)).perform(click());
    onView(withId(R.id.editText)).check(matches(withText("33")));

    // capture the screenshot and add the 'post_addition' tag to the screenshot
    Spoon.screenshot(mainActivity, "post_addition");
}

4. Enable screenshots on BrowserStack

Add the debugscreenshots parameter in the REST API request to enable screenshots on your Espresso test session.

The following sample cURL command shows the parameters used to enable screenshots on Android v7.1:

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/espresso/v2/build" \
-d '{"debugscreenshots": true, "devices": ["Google Pixel-7.1"], "app": "<app_id>", "testSuite": "<test_suite_id>"}' \
-H "Content-Type: application/json"

The following table lists the parameters used in the cURL request for enabling screenshots:

Parameter Description Values
debugscreenshots Capture screenshots during Espresso test execution true, false
Default: false
devices Name and OS version of the device to run the test on For example, Google Pixel-7.1.
Check out the list of devices available in App Automate
app ID of the application you receive when you upload your application on App Automate For example, bs://3205774d1560ee3f5acdf
testSuite ID of the test suite you receive when you upload your test suite on App Automate For example, bs://4205214d1560we3f5acdfe

5. View screenshots of the test execution

Navigate to your App Automate dashboard. On your session detail page, click the Screenshots tab. You can view screenshots for each test case as shown in the following image:

View screenshots for your test execution on dashboard

The name of the screenshot is the tag value set in your script when you used the Spoon.screenshot(activityName, tag) method.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy