I am looking for a way to take a screenshot of device after test failed and before get closed.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I haven't used screenshots in my Android testing yet, but I know some solutions, which might be useful:
Spoon
the best way to do it would be use
Emma
orSpoon
framework.Here you would find some useful information how to do it: http://elekslabs.com/2014/05/creating-test-reports-for-android-with-spoon-and-emma.html
Visit also
Spoon
's official Github site: https://github.com/square/spoon and its Gradle's plugin: https://github.com/stanfy/spoon-gradle-pluginand check related topic: How to get Spoon to take screenshots for Espresso tests?
screenshot-tests-for-android/
You can also try this Facebook's library: https://facebook.github.io/screenshot-tests-for-android/
Robotium's ScreenshotTaker
As I've already know, with
Robotium
test framework is possible to create test with screenshots. Check: Correct way to take screenshot with Robotium and CucumberIf you don't want to use any library, check source code of
Robotium
framework class calledScreenshotTaker.java
[click on link to see] and write your ownScreenshotTaker
class.Hope it will help.
Easiest way that I found:
Another improvement to previous answers. I'm using the experimental Screenshot API
I've created CustomScreenCaptureProcessor because BasicScreenCaptureProcessor uses /sdcard/Pictures/ folder and I encountered IOExceptions on some devices when creating the folder/image. Please note that you need to place your processor in the same package
Then, in your base Espresso test class just add
If you wish to use some protected folder, this did the trick on an emulator, tho it didn't work on a physical device
@Maragues answer ported to Kotlin:
Helper classes:
Usage:
Libraries declared in app's
build.gradle
:This setup saves a screenshot every time a test finished in the folder:
/sdcard/Android/data/your.package.name/files/Pictures
Navigate there via Android Studio's Device File Explorer (on the right sidebar)If you like to save screenshots only for failed tests, override the
failed
method ofTestWatcher
instead of thefinished
Writing a custom TestWatcher like the other answers explained is the way to go.
BUT (and it took us a long time to notice it) there is a caveat: The rule might fire too late, i.e. after your activity was already destroyed. This leaves you with a screenshot of the device's home screen and not from the failing activity.
You can solve this using RuleChain: Instead of writing
You have to write:
This makes sure that the screenshot is taken first and then the activity is destroyed
I made some improvements on this answer. No need to add an extra dependency for UiAutomator and it also works below api level 18.
Then include the following line in your test class: