Target
在Android UI Test時,想要screenshot去記錄當時的畫面,
主要介紹有哪些方式可以實作出來。
Action Items
- Solution 1: View drawing cache
 - Solution 2: Use screengrab (library)
 - Solution 3: Use FalconSpoon (library)
 
Quick Start
Solution 1: View drawing cache
Step 1: screenshot function
1  | 
  | 
- Setup view’s drawing cache
 - Drawing cache convert to Bitmap
 - Save file with Bitmap
 
Step 2: Setup permission
1  | <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  | 
Comment
當擷取畫面需要包含 dialog 元件時,需要額外取得 dialog 元件,
並且透過 Canvas ,將 Activity View 與 dialog View 合併起來。
Solution 2: Use screengrab (library)
Step 1: Gradle dependency
build.gradle(Module: Application) -> dependencies1
androidTestCompile 'tools.fastlane:screengrab:0.2.0'
Step 2: Example
Test is file name.1
Screengrab.screenshot("Test");
Comment
可以截取畫面,但是 dialog 無法截取。
Solution 3: Use FalconSpoon (library)
Step 1: Gradle dependency
build.fradle(Module: Application) -> dependencies1
androidTestCompile 'com.jraska:falcon-spoon-compat:1.0.1'
Step 2: Example
tag include file name.
1  | FalconSpoon.screenshot(activity, tag);  | 
Comment
可以截取畫面,畫面中可包含 dialog。