2017-03-11 42 views
2

記錄儀生成的代碼在記錄後立即運行失敗。使用DatePicker記錄Espresso測試

原因是,在錄音時,我點擊一年,一年的微調彈出,然後向後滾動,然後選擇其中一個年份。錄像機不捕獲滾動。

在Xcode中,他們添加了滾動到該項目的方法。在Espresso中找不到類似的東西。

(使用過Android Studio 2.3。)

回答

4

我沒有使用過的記錄在很長一段時間,而是用手工寫我的測試,所以不知道這個答案對你有所幫助。

我用這條線來設置一個日期選擇器日期:

onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth)); 

PickerActions是一個特殊的咖啡支持庫中 - 在espresso-contrib - 像這樣把它添加到您的gradle這個文件(我需要幾個排除到防止編譯錯誤,由於不匹配支持庫版本):

androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') { 
    exclude group: 'com.android.support', module: 'appcompat' 
    exclude module: 'support-annotations' 
    exclude module: 'support-v4' 
    exclude module: 'support-v13' 
    exclude module: 'recyclerview-v7' 
    exclude module: 'appcompat-v7' 
} 

我然後在點擊打開的日期選擇器按鈕一個輔助方法使用,設置日期和點擊確定按鈕證實了這一點:

public static void setDate(int datePickerLaunchViewId, int year, int monthOfYear, int dayOfMonth) { 
    onView(withParent(withId(buttonContainer)), withId(datePickerLaunchViewId)).perform(click()); 
    onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, monthOfYear, dayOfMonth)); 
    onView(withId(android.R.id.button1)).perform(click()); 
} 

,然後使用它像這樣在我的測試:

TestHelper.setDate(R.id.date_button, 2017, 1, 1); 
//TestHelper is my helper class that contains the helper method above 
+0

等待,什麼是PickerActions?那應該是TestHelper? – Rob

+0

我的壞,這是一個額外的圖書館照顧選擇。我更新了我的回答 – stamanuel

+0

好的謝謝,哇,錄音機是如此無用..痛苦..會試試這個,但上帝很麻煩,做一些應該很簡單的事情... :( – Rob

相關問題