Home » Android » android – Is it possible to take a screenshot and test pixel values with espresso?

android – Is it possible to take a screenshot and test pixel values with espresso?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I started using espresso and wrote some simple UI Tests for clicking on buttons, typing in text and checking if everything is there where it should be.
Now I wanted to take a screenshot and test some pixel values, but I can’t find a way to do this.
Has anyone any suggestions? Is it even possible with espresso?

How to&Answers:

As espresso tests are simple InstrumentationTestCases, you could use paparazzo and celebrity to take screenshots of your application.

Spoon also as a very nice screenshot system. Sharper than paparazzi but that can only take a screenshot of activities, not dialogs for instance.

And last but not least, since android SDK 16, you can use screencap utility to take very fast and sharp screenshots of an app. I think this solution is really the best but it works only with SDK 16+.

Finally to test screenshots against a reference screenshot, there is no real tools. You can find great ideas of implementation on stack over flow, but up to now, noone came with a reference tool that can be used on android.

Answer:

There’s a tool made by Facebook for comparing screenshots if that’s what you’re looking for: http://facebook.github.io/screenshot-tests-for-android/

Iterating on UI code is hard. How do you quickly verify that your layout or view changes work correctly in all configurations?
screenshot-tests-for-android can solve these problems by providing a test framework that checks for visual differences across changes.

Answer:

I fork a version to add Espresso run with Spoon to a great quality tools suit for android, check it out

Just run you will see:

mvn clean install -P espresso-spoon

Answer:

As an alternative to doing screenshot comparison, you could consider comparing layout state itself (i.e. positions of the views) by using libraries such as LayoutVerifier.

While it will not give you a visual representation of the layout in form of a screenshot, it essentially performs the same function by comparing states of the views. Also it runs on Robolectric, removing the need for running it on a real device.