Sök…


Exempel på Anpassad matcher för att testa TextView-felmeddelande

  1. Skapa ett ErrorMatcher i ditt testpaket med koden nedan:
public class ErrorMatcher {

    @NonNull
    public static Matcher<View> withError(final String expectedErrorText) {
        Checks.checkNotNull(expectedErrorText);
        return new BoundedMatcher<View, TextView>(TextView.class) {    
            @Override
            public void describeTo(final Description description) {
                description.appendText("error text: ");
                stringMatcher.describeTo(description);
            }
                
            @Override
            public boolean matchesSafely(final TextView textView) {
                return expectedErrorText.equals(textView.getError().toString());
            }
        };
    }
}

Matchande logik är att hitta TextView elementet, vilket felmeddelandetext är lika med förväntat feltextvärde, gå igenom underuppsättningen TextView fält som finns i layouthierarkin. describeTo metod används för felsökningsresultat.

  1. Sedan kan du använda din anpassade matcher i testfallet som visas nedan:
@Test  
public void verifiesSignInErrorIsShown() {
    onView(withId(R.id.email_sign_in_button)).perform(click());
    onView(ErrorMatcher.withError("Your error text")).check(matches(isDisplayed()));
}


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow