2016-08-29 26 views
8

Bir EditText alanında bir hata olup olmadığını test etmek istiyorum (editText.setError ile ayarlanır ("Boş bırakılamaz!")).Android'de Espresso ile EditText hatalarını test etme

EditText field with error

Ben Espresso testleri kaydetmek için yeni AndroidStudio 2.2 özelliği ile bir Espresso test durumda, oluşturduk. Bu yüzden kod oldukça otomatik olarak oluşturulur. Ama şimdilik sadece editText'in görüntülenip görüntülenmediğini kontrol ediyor.

@RunWith(AndroidJUnit4.class) 
public class CreateNoteActivityTitleCannotBeBlank { 

    @Rule 
    public ActivityTestRule<CreateNoteActivity> mActivityTestRule = new ActivityTestRule<>(CreateNoteActivity.class); 

    @Test 
    public void createNoteActivityTitleCannotBeBlank() { 
     ViewInteraction floatingActionButton = onView(
       allOf(withId(R.id.fab_add_note), 
         withParent(allOf(withId(R.id.activity_create_note), 
           withParent(withId(android.R.id.content)))), 
         isDisplayed())); 
     floatingActionButton.perform(click()); 

     ViewInteraction editText = onView(
       allOf(withId(R.id.tiet_note_title), 
         childAtPosition(
           childAtPosition(
             withId(R.id.til_title), 
             0), 
           0), 
         isDisplayed())); 
     editText.check(matches(isDisplayed())); 

    } 

    private static Matcher<View> childAtPosition(
      final Matcher<View> parentMatcher, final int position) { 

     return new TypeSafeMatcher<View>() { 
      @Override 
      public void describeTo(Description description) { 
       description.appendText("Child at position " + position + " in parent "); 
       parentMatcher.describeTo(description); 
      } 

      @Override 
      public boolean matchesSafely(View view) { 
       ViewParent parent = view.getParent(); 
       return parent instanceof ViewGroup && parentMatcher.matches(parent) 
         && view.equals(((ViewGroup) parent).getChildAt(position)); 
      } 
     }; 
    } 
} 

hata görüntülenir olmadığını test etmek için bir yol var mı?

+0

editText.check(matches(hasErrorText("Cannot be blank!"))); değiştirmek adnotation [Android Espresso @Nullable – Genehme

+0

Olası yinelenen eklemek deneyin. TextInputLayout içinde ErrorText nasıl kontrol edilir] (http://stackoverflow.com/questions/34285782/android-espresso-how-to-check-errortext-in-textinputlayout) –

cevap

17

Sen editText.check(matches(isDisplayed()));

+0

Ya bu! :) Teşekkür ederim. Sadece [Hile Sayfası] 'nı (https://google.github.io/android-testing-support-library/docs/espresso/cheatsheet/) kontrol ettim, ancak bazı yöntemler eksik. –