Dav*_*lva 0 android android-espresso android-espresso-recorder
LinearLayout (productList) 在运行时动态填充子视图,如下所示:
@ViewById(R.id.ll_products)
LinearLayout productList;
public void createProductList() {
productList.addView(getView(mobilePhone))
productList.addView(getView(internet))
productList.addView(getView(television))
}
public View getView(Product product) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView layout = (LinearLayout) inflater.inflate(R.layout.row_product, null);
TextView productIcon = (TextView) layout.findViewById(R.id.tv_product_row_icon);
productIcon.setText(product.getProductIcon());
productName.setText(product.getName());
}
Run Code Online (Sandbox Code Playgroud)
productList 故意是 LinearLayout,而不是 ListView。
产品列表包含三个产品 - 每个产品都有图标(可能有重复)。
我想记录一个场景,我点击第二个产品的图标。这种情况如何避免 AmbiguousViewMatcherException ?
不幸的是,以下代码不起作用 - 将找到三个 R.id.tv_product_row_icon ...
ViewInteraction appCompatTextView = onView(withId(R.id.tv_product_row_icon));
appCompatTextView.perform(scrollTo(), click());
Run Code Online (Sandbox Code Playgroud)
如何指定要单击的第二个图标?
不幸的是,您必须Matcher为这种情况创建自定义。
以下View与指定索引匹配:
public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) {
return new TypeSafeMatcher<View>() {
int currentIndex = 0;
@Override
public void describeTo(Description description) {
description.appendText("with index: ");
description.appendValue(index);
matcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
return matcher.matches(view) && currentIndex++ == index;
}
};
}
Run Code Online (Sandbox Code Playgroud)
适合您情况的用法:
Matcher<View> secondIconMatcher = allOf(withId(R.id.tv_product_row_icon));
onView(withIndex(secondIconMatcher , 1)).perform(click());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
882 次 |
| 最近记录: |