无法从列表视图中删除项目

时间:2018-10-16 15:00:50

标签: android automation android-espresso

我已选择该项目,并将其拖动到左侧。但是我无法单击删除图像。

onView(withId(R.id.storewalk_list)) .perform(RecyclerViewActions.actionOnItemAtPosition(0, swipeLeft()));

enter image description here

enter image description here

在此图像中,每个产品之后的右侧都有GAP,它具有相似的属性

我尝试使用以下代码:

onView(allOf(withId(R.id.delete_button), isDisplayed())).perform(actionOnItemAtPosition(0,click()));

它给我一个错误:

android.support.test.espresso.AmbiguousViewMatcherException: '(with id: com.cit:id/delete_button and has parent matching: (with id: com.cit:id/swipeActions and has parent matching: with id: com.cit:id/swipe_layout) and is clickable)' matches multiple views in the hierarchy.

有人可以让我知道如何解决此问题吗?

2 个答案:

答案 0 :(得分:0)

我通过创建自定义类获得了解决方案。

https://mobikul.com/recycler-view-multiple-match-problem-in-espresso/

答案 1 :(得分:0)

您可以创建一个自定义的ViewAction来接受所选位置的匹配器:

public static ViewAction actionOnView(Matcher<View> matcher, ViewAction action) {
    return new ViewAction() {
        @Override public Matcher<View> getConstraints() {
            return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());;
        }

        @Override public String getDescription() {
            return String.format("performing ViewAction: %s on item matching %s", action.getDescription(), matcher);
        }

        @Override public void perform(UiController uiController, View view) {
            List<View> matches = new ArrayList<>();
            for (View item : TreeIterables.breadthFirstViewTraversal(view)) {
                if (matcher.matches(item)) {
                    matches.add(item);
                }
            }
            switch (matches.size()) {
                default: throw new RuntimeException(String.format("Ambiguous views found %s", matcher));
                case 0: throw new RuntimeException(String.format("No view found %s", matcher));
                case 1: action.perform(uiController, matches.get(0));
            }
        }
    };
}

假设删除按钮已经可见,那么您可以执行以下操作:

onView(withId(R.id.storewalk_list)).perform(actionOnItemAtPosition(0, actionOnView(withId(R.id.delete_button), click())))