【发布时间】:2019-10-21 02:48:16
【问题描述】:
我希望有些人可以帮助我摆脱一些轻量级的自定义视图。我运行了 Record Espresso 测试,android-studio 生成了以下代码。
ViewInteraction textInputEditText = onView(
allOf(withId(R.id.edit_text_last_name),
childAtPosition(
allOf(withId(R.id.layout_item_viewpager),
childAtPosition(
withId(R.id.layout_my_activity),
0)),
1),
isDisplayed()));
textInputEditText.perform(replaceText("a"), closeSoftKeyboard());
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
Record Espresso Test 生成的代码有效,我不确定子视图如何工作或 0 和 1 在此代码块中的作用。
我已经阅读了大量有关 Hamcrest 匹配器以及如何创建自定义匹配器的信息,但我很难找到与 android 生成的代码相关的材料。这里的任何帮助将不胜感激。
【问题讨论】:
标签: java android android-espresso matcher