Как проверить значения TextInputLayout (подсказка, ошибка и т.д.) С помощью Android Espresso?
Я пытаюсь проверить использование Espresso, если мои представления TextInputLayout
имеют конкретный намек. Я использовал код, как показано ниже:
Espresso.onView(ViewMatchers.withId(R.id.edit_text_email))
.check(ViewAssertions.matches(
ViewMatchers.withHint(R.string.edit_text_email_hint)))
Это отлично подходит для обычных представлений EditText
, не завернутых в TextInputLayout
. Однако, когда он обертывается, он больше не работает.
Я попытался использовать решение из Android Espresso - Как проверить подсказку EditText?, но он все еще не работает.
Я также рассмотрел: https://code.google.com/p/android/issues/detail?id=191261, в котором сообщалось об этой проблеме, говорится, что обходной путь довольно прост, указывая на текущий withHint
кода, но я не могу заставить его работать.
Любые идеи по устранению этой проблемы?
Ответы
Ответ 1
Здесь мой пользовательский соединитель:
public static Matcher<View> hasTextInputLayoutHintText(final String expectedErrorText) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof TextInputLayout)) {
return false;
}
CharSequence error = ((TextInputLayout) view).getHint();
if (error == null) {
return false;
}
String hint = error.toString();
return expectedErrorText.equals(hint);
}
@Override
public void describeTo(Description description) {
}
};
}
}
и здесь, как использовать:
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void testMyApp() {
onView(withId(R.id.textInputLayout)).check
(matches(hasTextInputLayoutErrorText(mRule.getActivity().getString(R.string
.app_name))));
}
Если вы хотите проверить errorText
of TextInputLayout
, измените эту строку:
CharSequence error = ((TextInputLayout) view).getHint();
с
CharSequence error = ((TextInputLayout) view).getError();
Надеюсь, что это поможет
Ответ 2
Котлин версия пиотрека 1543 ответ:
fun hasTextInputLayoutHintText(expectedErrorText: String): Matcher<View> = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description?) { }
override fun matchesSafely(item: View?): Boolean {
if (item !is TextInputLayout) return false
val error = item.hint ?: return false
val hint = error.toString()
return expectedErrorText == hint
}
}
Ответ 3
Более общее решение, которое будет работать с любым представлением, имеющим метод getHint:
public static Matcher<View> withCustomHint(final Matcher<String> stringMatcher) {
return new BaseMatcher<View>() {
@Override
public void describeTo(Description description) {
}
@Override
public boolean matches(Object item) {
try {
Method method = item.getClass().getMethod("getHint");
return stringMatcher.matches(method.invoke(item));
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
}
return false;
}
};
}
Использование:
onView(withId(R.id.SomeLayout)).check(matches(withCustomHint(is("SomeString"))));
Ответ 4
Если вы пытаетесь проверить ошибку в Material TextInputLayout, попробуйте что-то вроде этого:
onView(withId(viewId)).check(matches(textInputLayoutErrorTextMatcher(getString(stringId))))
Убедитесь, что вы указали идентификатор TextInputLayout, а не его дочерний элемент (т.е. TextInputEditText).
Ответ 5
Вышеупомянутые решения не помогли мне в использовании. Я хотел найти TextInputEditText и ввести текст в него. Вот мое решение:
@VisibleForTesting
class WithTextInputLayoutHintMatcher @RemoteMsgConstructor
constructor(@field:RemoteMsgField(order = 0)
private val stringMatcher: Matcher<String>) : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with TextInputLayout hint: ")
stringMatcher.describeTo(description)
}
public override fun matchesSafely(textInputEditText: View): Boolean {
if (textInputEditText !is TextInputEditText) return false
return stringMatcher.matches((textInputEditText.parent.parent as? TextInputLayout)?.hint)
}
}
/**
* Returns a matcher that matches [TextInputEditText] based on it hint property value.
*
*
* **Note:** View sugar for `withHint(is("string"))`.
*
* @param hintText [String] with the hint text to match
*/
fun withTextInputHint(hintText: String): Matcher<View> {
return withTextInputHint(Matchers.`is`(checkNotNull(hintText)))
}
/**
* Returns a matcher that matches a descendant of [TextInputEditText] that is displaying the hint
* associated with the given resource id.
*
* @param resourceId the string resource the text view is expected to have as a hint.
*/
fun withTextInputHint(resourceId: Int): Matcher<View> {
return withTextInputHint(getString(resourceId))
}
/**
* Returns a matcher that matches [TextView]s based on hint property value.
*
*
* **Note:** View hint property can be `null`, to match against it use `
* withHint(nullValue(String.class)`
*
* @param stringMatcher [`Matcher
`](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html) * of [String] with text to match
*/
fun withTextInputHint(stringMatcher: Matcher<String>): Matcher<View> {
return WithTextInputLayoutHintMatcher(checkNotNull(stringMatcher))
}
Использование:
onView(withTextInputHint(R.string.hint)).perform(ViewActions.typeText("Type text here"))
Ответ 6
Гораздо более простое решение - проверить, виден ли текст ошибки, например:
val text = mTestRule.getActivity().getString(R.string.error_text)
onView(withText(text)).check(matches(isDisplayed()))
Ответ 7
Отражений Java можно избежать. Кроме того, подсказки поддерживаются TextView и его потомками (включая TextInputEditText) или TextInputLayout. Таким образом,
fun withHint(expected: String) = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("TextView or TextInputLayout with hint '$expected'")
}
override fun matchesSafely(item: View?) =
item is TextInputLayout && expected == item.hint || item is TextView && expected == item.hint
}
И можно использовать так:
onView(withId(R.id.exampleView)).check(matches(withHint("example text")))
Ответ 8
у вас есть решения
-First
onView(withText(errorMessage)).check(matches(isDisplayed()))
-second
onView(withId(R.id.textinput_error)).check(matches(withText(errorMessage)))