When a test fails, I would like to take a screenshot and have it as part of my test report. Espresso auto generates a HTML report, and it would be nice if my screenshots would be stored there. I am currently able to use the UiAutomator library to save a screenshot to the Android device, but that's not very helpful because pulling files off a device is a pain. Is there better way to do this? Does Espresso have a built in function for this?
My UiAutomator screenshot code looks like this, and I call it in my after test teardown:
File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/screenshots/" + getTargetContext().getPackageName());
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
String filename = "junk" + ".png";
device.takeScreenshot(new File(path, filename));
If this is totally wrong and I should be going about it another way, please let me know.