2016-05-25 147 views
1

我正在寫測試使用espresso,我的應用程序打算手機相機,在那裏我手動按下單擊按鈕,然後它遷移到下一個屏幕,我不能自動化測試代碼中的圖像點擊按鈕,我如何使用代碼來訪問攝像機,通過它我可以做到這一點。 謝謝。咖啡:如何點擊圖片點擊按鈕手機相機

+0

您無法使用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

回答

1

你不應該打開相機的意圖,否則你將無法從中得到任何結果圖像(無需手動按下相機按鈕)。

看一看在打樁這個網站的攝像頭部分: 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())); 
} 
+0

我是獲取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(沒有這樣的文件或目錄) –

+0

消息說該文件不存在於該設備中,但可能是由於應用程序權限有問題。無論如何,我從不使用外部圖像進行測試,我認爲使用來自應用程序資源的圖像更好。用我用來獲取位圖測試的方法查看我的更新。 – jeprubio