且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android Espresso-如何检查EditText提示?

更新时间:2021-11-03 06:23:40

看起来像我想通了.基本上,您需要创建自己的匹配器:

Looks like I figured it out. Basically, you need to create your own matcher:

public static Matcher<View> withHint(final String expectedHint) {
    return new TypeSafeMatcher<View>() {

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof EditText)) {
                return false;
            }

            String hint = ((EditText) view).getHint().toString();

            return expectedHint.equals(hint);
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}

然后您可以使用它:

onView(withId(R.id.locationInput)).check(matches(withHint("Location (Optional)")));