mark cerqueira well-rounded nerd

Proper Permissions for Spoon Screenshots

I’ve been working on setting up a UI testing framework for the Android team here at Twitch. Good QA is a puzzle so working through the challenges of UI tests is a must if you want to reliably deliver quality with each and every release. Fortunately between former, very knowledgable colleagues on the subject and great libraries like Espresso and Spoon setting up UI tests on Android isn’t too challenging.

I did run into one pretty big hiccup: getting screenshots working with Spoon. Fortunately after repeatedly banging my head on my desk, breaking the problem down into smaller solvable problems, and typing things here and there, I got it resolved!

And sharing is caring so let’s walk through it!

The key to getting screenshots working is to get your app to have the WRITE_EXTERNAL_STORAGE permission but strip it of the maxSdkVersion attribute. Your test app does not need it; only the app your testing does. Note that even if you don’t specify this attribute, it might get merged into your manifest during manifest merging. If you open AndroidManifest.xml you can click on Merged Manifest near the bottom left to see the merged manifest. Adding this snippet to your manifest file will ensure the attribute gets stripped away.

Then you can try building the app and test app, granting permissions, and then running the tests.

If each of these steps works for you, you should have screenshots after the Spoon runner finishes running! A few notes:

  • At first I was using the Gradle Spoon plugin which basically wraps the above script into a single Gradle task. I opted to use this script because it let me see which steps were failing and let me avoid learning more about Gradle. 🙃
  • spoon-runner-2.0.0-20180425-all.jar was assembled by checking out the master branch of the Spoon project, opening the project in Android Studio, and assembling the spoon-runner target. I am using Spoon 2 (not yet released) because it runs each test in separate instrumentation process which makes things generally more stable.
  • I highly recommend passing the --debug flag to the Spoon runner command. It lets you see which test classes are running but most importantly helped me realized GIF generation is super slow, hence why I disabled it with --disable-gif.

Happy UI testing!