2017-06-29 37 views
2

我已經有兩個DatePicker控件和一些文本輸入和按鈕。我想用espresso測試一些情景:在Android Espresso框架中檢查DatePicker日曆值

  1. 設置日期爲第一個DatePicker
  2. 設置一些文字和點擊按鈕女巫觸發我的業務邏輯和設置日期秒DatePicker
  3. 查看第二天​​的日期DatePicker

在第三步,我想要做一些像onView(withId(R.id.end_date_picker)).check(matches(withText("25-01-2017")));。我可以使用什麼來代替withText來檢查DatePicker日曆/日期值?謝謝你的幫助。

回答

2

要達到你想要什麼,你需要兩樣東西:

要設置日期爲日期選擇器的最好辦法就是要利用咖啡PickerActions的,我對他們說herehere,但會複製一些地區我的答案在那裏的:

最簡單的方法就是要利用PickerActions.setDate方法:

onView(withId(R.id.start_date_picker)).perform(PickerActions.setDate(2017, 6, 30)); 

得到這個工作,你必須添加com.android.support.test.espresso:espresso-contrib庫y中我們的gradle文件,也許排除一些圖書館,請檢查我上面鏈接的答案,如果你有這個問題。

對於第二部分,檢查DatePicker的日期,我認爲你必須使用自定義匹配器。

你可以將這個方法添加到您的代碼:

public static Matcher<View> matchesDate(final int year, final int month, final int day) { 
    return new BoundedMatcher<View, DatePicker>(DatePicker.class) { 

     @Override 
     public void describeTo(Description description) { 
      description.appendText("matches date:"); 
     } 

     @Override 
     protected boolean matchesSafely(DatePicker item) { 
      return (year == item.getYear() && month == item.getMonth() && day == item.getDayOfMonth()); 
     } 
    }; 
} 

你那麼可以用它來檢查這樣的日期:

onView(withId(R.id.end_date_picker)).check(matches(matchesDate(2017, 6, 30)));