在 Espresso 中执行对 RecyclerView 中子项按钮的点击。

huangapple go评论75阅读模式
英文:

Performing click on a button of a child in recycler view using espresso

问题

在一个活动中,我有一个包含2个文本视图和2个按钮的自定义回收视图,如下所示。

在 Espresso 中执行对 RecyclerView 中子项按钮的点击。

垃圾桶图标按钮的ID是deleteButton。我想在Espresso测试中访问此按钮,以便我可以模拟点击。

我尝试过但失败了的两种方法如下:

        onView(withId(R.id.basket))
                .perform(actionOnItemAtPosition(4, click()));

        onView(withText("Cherry"))
                .perform(
                        RecyclerViewActions.actionOnItem(
                                hasDescendant(withId(R.id.deleteButton)),
                                ViewActions.click()
                        )
                );

提前感谢您的任何帮助。

英文:

Within an activity, I have a custom recycler view that contains 2 textviews and 2 buttons as shown below.

在 Espresso 中执行对 RecyclerView 中子项按钮的点击。

The id for the recycling bin icon button is deleteButton. I would like to access this button within espresso testing so that I can mock a click.

Two methods that I have attempted and failed are shown below:

        onView(withId(R.id.basket))
                .perform(actionOnItemAtPosition(4, click() ));

        onView(withText("Cherry"))
                .perform(
                        RecyclerViewActions.actionOnItem(
                                hasDescendant(withId(R.id.deleteButton)),
                                ViewActions.click()
                        )
                );

Thank you for any help in advance.

答案1

得分: 2

创建自定义视图操作是在RecyclerView项视图内单击视图的一种方法:

public static ViewAction actionOnItemView(Matcher<View> matcher, ViewAction action) {

    return new ViewAction() {

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

        @Override public Matcher<View> getConstraints() {
            return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
        }

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

然后,在您的RecyclerView上使用RecyclerViewActions之一,然后在成功后将actionOnItemView作为项视图上的后续操作:

ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));
英文:

One way to click on a view inside a RecyclerView item view is by creating a custom view action:

public static ViewAction actionOnItemView(Matcher&lt;View&gt; matcher, ViewAction action) {

    return new ViewAction() {

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

        @Override public Matcher&lt;View&gt; getConstraints() {
            return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
        }

        @Override public void perform(UiController uiController, View view) {
            List&lt;View&gt; results = new ArrayList&lt;&gt;();
            for (View v : TreeIterables.breadthFirstViewTraversal(view)) {
                if (matcher.matches(v)) results.add(v);
            }
            if (results.isEmpty()) {
                throw new RuntimeException(String.format(&quot;No view found %s&quot;, StringDescription.asString(matcher)));
            } else if (results.size() &gt; 1) {
                throw new RuntimeException(String.format(&quot;Ambiguous views found %s&quot;, StringDescription.asString(matcher)));
            }
            action.perform(uiController, results.get(0));
        }
    };
}

Then use one of the RecyclerViewActions on your RecyclerView, then actionOnItemView as subsequent action on the item view if successful:

ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));

huangapple
  • 本文由 发表于 2020年4月3日 21:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定