我正在寫測試使用espresso,我的應用程序打算手機相機,在那裏我手動按下單擊按鈕,然後它遷移到下一個屏幕,我不能自動化測試代碼中的圖像點擊按鈕,我如何使用代碼來訪問攝像機,通過它我可以做到這一點。 謝謝。咖啡:如何點擊圖片點擊按鈕手機相機
回答
你不應該打開相機的意圖,否則你將無法從中得到任何結果圖像(無需手動按下相機按鈕)。
看一看在打樁這個網站的攝像頭部分: https://guides.codepath.com/android/UI-Testing-with-Espresso#stubbing-out-the-camera
這樣你通過模擬實際圖像測試活動從攝像機「返回」到你的應用程序。
更新
這是我用得到位圖來測試方法:
public static Bitmap getTestBitmap(Context context, String resourceName) {
Resources resources = context.getResources();
Bitmap ret = null;
int imageResource = resources.getIdentifier(
resourceName, "drawable", context.getPackageName());
Uri pictureUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ resources.getResourcePackageName(imageResource) + '/'
+ resources.getResourceTypeName(imageResource) + '/'
+ resources.getResourceEntryName(imageResource));
try {
ret = MediaStore.Images.Media.getBitmap(context.getContentResolver(), pictureUri);
} catch (Exception e) {
}
return ret;
}
然後我保存在內部存儲的位圖,並得到URI:
public static Uri saveToInternalStorage(Context context, Bitmap bitmapImage, String fileName) {
ContextWrapper cw = new ContextWrapper(context);
// path to /data/data/yourapp/app_data/pictures
File directory = cw.getDir("pictures", Context.MODE_PRIVATE);
// Create imageDir
File mypath = new File(directory, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (Exception e) {
}
}
return Uri.fromFile(new File(mypath.getAbsolutePath()));
}
我是獲取5-27 15:20:24.183 3135-3553/com.android.local E/BitmapFactory:無法解碼流:java.io.FileNotFoundException:/storage/emulated/0/Mohit/IMG_20160527_152024.jpg:打開失敗:ENOENT (沒有這樣的文件或目錄) 05-27 15:20:24.183 3135-3553/com.android.local E/BitmapFactory:無法解碼流:java.io.FileNotFoundException:/ storage/emulated/0/Mohit/IMG_20160527_152024 .jpg:打開失敗:ENOENT(沒有這樣的文件或目錄) –
消息說該文件不存在於該設備中,但可能是由於應用程序權限有問題。無論如何,我從不使用外部圖像進行測試,我認爲使用來自應用程序資源的圖像更好。用我用來獲取位圖測試的方法查看我的更新。 – jeprubio
- 1. 咖啡 - 點擊的TextView(按鈕)
- 2. 意式咖啡執行點擊位於片段的按鈕
- 3. Kendo UI手機按鈕點擊數據點擊兩次
- 4. 通過點擊按鈕重新啓動手機點擊
- 5. 如何點擊手機
- 6. 安卓相機捕獲按鈕點擊
- 7. 從按鈕點擊資源中隨機播放隨機圖片
- 8. 隨機圖片點擊
- 9. 點擊圖片機械化
- 10. 點擊圖片按鈕
- 11. JavaScript點擊圖片按鈕
- 12. Javascript隨機圖像點擊按鈕
- 13. 咖啡:點擊按鈕動作ListView中頁腳
- 14. 意式濃縮咖啡點擊webView按鈕
- 15. 如何模擬圖片按鈕點擊
- 16. 如何處理圖片點擊按鈕點擊事件
- 17. 如何在點擊按鈕時使用圖片框彈出相機?
- 18. 咖啡測試,點擊X/Y座標
- 19. 劍道UI手機 - 使用按鈕點擊另一個按鈕
- 20. 上的ImageButton的Android咖啡點擊點擊錯誤的位置
- 21. 如何在按鈕點擊事件上鎖定手機? - android
- 22. 如何自動點擊手機鏈接(提交按鈕)
- 23. 點擊按鈕,隨機圖片全屏顯示
- 24. 如何在listview中點擊listview項目按鈕啓動相機?
- 25. 如何去android上手機的後退按鈕點擊上一個片段?
- 26. 手機firefox點擊攔截
- 27. 手機導航點擊jquery
- 28. 點擊android手機屏幕
- 29. jQuery手機(點擊事件)
- 30. 相機視圖中不relesing按鈕的點擊監聽器
您無法使用Espresso訪問相機UI,因爲它不適用於各種設備應用程序。相反,您可以使用[Espresso-Intents](https://google.github.io/android-testing-support-library/docs/espresso/intents/index.html)來模擬行爲。如果你想訪問攝像頭代碼,你可以使用[UIAutomator](https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html),它跨設備應用程序。 – Droidwala