gpt4 book ai didi

android - 如何使用 Android Espresso 测试 TextInputLayout 值(提示、错误等)?

转载 作者:IT王子 更新时间:2023-10-28 23:35:27 27 4
gpt4 key购买 nike

如果我的 TextInputLayout View 有特定提示,我正在尝试使用 Espresso 进行测试。我使用了如下代码:

Espresso.onView(ViewMatchers.withId(R.id.edit_text_email))
.check(ViewAssertions.matches(
ViewMatchers.withHint(R.string.edit_text_email_hint)))

这适用于普通的 EditText View ,而不是包裹在 TextInputLayout 中。但是,当它环绕时,它不再起作用。

我尝试使用 Android Espresso - How to check EditText hint? 的解决方案,但它仍然无法正常工作。

我还研究了:https://code.google.com/p/android/issues/detail?id=191261报告了这个问题,它说通过指向当前的 withHint 代码解决方法很容易,但我无法让它工作。

有解决此问题的想法吗?

最佳答案

这是我的自定义匹配器:

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))));

}

如果您想检查 TextInputLayouterrorText,请更改此行:

     CharSequence error = ((TextInputLayout) view).getHint();

     CharSequence error = ((TextInputLayout) view).getError();

希望对你有帮助

关于android - 如何使用 Android Espresso 测试 TextInputLayout 值(提示、错误等)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842034/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com