【发布时间】:2019-03-12 18:50:18
【问题描述】:
美好的一天,我开始掌握 espresso 并尝试检查 TextInputLayout 的 PasswordVisibilityToggleEnabled 按钮是否可见。我知道这个按钮是一个带有 id 的 CheckableImageButton (R.id.text_input_password_toggle) 但不知道如何在 Espresso 中获得它。
我试过这样做:
onView(withId(R.id.passwordTextInputLayout)).check(hasDescendant(withId(R.id.text_input_password_toggle))).check(matches(not(isDisplayed())));
但这不起作用。我猜我可能必须根据 StackOverflow 上的几个问题使用自定义匹配器,但不确定是否正确。
public static Matcher<View> getPasswordToggleView(final Matcher<View> parentMatcher, int id) {
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View view) {
if(!(view.getParent() instanceof ViewGroup)) {
return parentMatcher.matches(view.getParent());
}
ViewGroup group = (ViewGroup) view.getParent();
return parentMatcher.matches(view.getParent()) && view.getId() == id;
}
@Override
public void describeTo(Description description) {
description.appendText("get View with matching id");
}
};
}
并尝试像这样使用它但仍然无法正常工作:
onView(getPasswordToggleView(withId(R.id.passwordTextInputLayout), R.id.text_input_password_toggle)).check(matches(not(isDisplayed())));
有什么想法吗?
【问题讨论】:
标签: android unit-testing android-espresso