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

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

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

问题

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

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

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

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

  1. onView(withId(R.id.basket))
  2. .perform(actionOnItemAtPosition(4, click()));
  3. onView(withText("Cherry"))
  4. .perform(
  5. RecyclerViewActions.actionOnItem(
  6. hasDescendant(withId(R.id.deleteButton)),
  7. ViewActions.click()
  8. )
  9. );

提前感谢您的任何帮助。

英文:

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:

  1. onView(withId(R.id.basket))
  2. .perform(actionOnItemAtPosition(4, click() ));
  3. onView(withText("Cherry"))
  4. .perform(
  5. RecyclerViewActions.actionOnItem(
  6. hasDescendant(withId(R.id.deleteButton)),
  7. ViewActions.click()
  8. )
  9. );

Thank you for any help in advance.

答案1

得分: 2

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

  1. public static ViewAction actionOnItemView(Matcher<View> matcher, ViewAction action) {
  2. return new ViewAction() {
  3. @Override public String getDescription() {
  4. return String.format("performing ViewAction: %s on item matching: %s", action.getDescription(), StringDescription.asString(matcher));
  5. }
  6. @Override public Matcher<View> getConstraints() {
  7. return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
  8. }
  9. @Override public void perform(UiController uiController, View view) {
  10. List<View> results = new ArrayList<>();
  11. for (View v : TreeIterables.breadthFirstViewTraversal(view)) {
  12. if (matcher.matches(v)) results.add(v);
  13. }
  14. if (results.isEmpty()) {
  15. throw new RuntimeException(String.format("No view found %s", StringDescription.asString(matcher)));
  16. } else if (results.size() > 1) {
  17. throw new RuntimeException(String.format("Ambiguous views found %s", StringDescription.asString(matcher)));
  18. }
  19. action.perform(uiController, results.get(0));
  20. }
  21. };
  22. }

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

  1. ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
  2. 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:

  1. public static ViewAction actionOnItemView(Matcher&lt;View&gt; matcher, ViewAction action) {
  2. return new ViewAction() {
  3. @Override public String getDescription() {
  4. return String.format(&quot;performing ViewAction: %s on item matching: %s&quot;, action.getDescription(), StringDescription.asString(matcher));
  5. }
  6. @Override public Matcher&lt;View&gt; getConstraints() {
  7. return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
  8. }
  9. @Override public void perform(UiController uiController, View view) {
  10. List&lt;View&gt; results = new ArrayList&lt;&gt;();
  11. for (View v : TreeIterables.breadthFirstViewTraversal(view)) {
  12. if (matcher.matches(v)) results.add(v);
  13. }
  14. if (results.isEmpty()) {
  15. throw new RuntimeException(String.format(&quot;No view found %s&quot;, StringDescription.asString(matcher)));
  16. } else if (results.size() &gt; 1) {
  17. throw new RuntimeException(String.format(&quot;Ambiguous views found %s&quot;, StringDescription.asString(matcher)));
  18. }
  19. action.perform(uiController, results.get(0));
  20. }
  21. };
  22. }

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

  1. ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
  2. 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:

确定