我是一个新手使用Espresso。我无法通过以下代码测试我想要的内容:
onData(withId(R.id.relativelayout_tag))
.inAdapterView(withId(R.id.recyclerview_tag_list))
.onChildView(withId(R.id.imageview_tag))
.atPosition(1)
.check(matches(isDisplayed()));
在这里,R.id.imageview_tag
是 R.id.relativelayout_tag
的子项。而 R.id.relativelayout_tag
包含了适配器项目的全部内容。R.id.recyclerview_tag_list
是我赋予特定 RecyclerView
适配器的名称。
这是一个非常基础的测试。以下是用户流程:
- 选择
RecyclerView
上的第一项(我不在乎视图上显示的文本),也不要建议使用视图文本来标识第一项。我不关心适配器项目的内容,甚至不需要在某个视图上放置唯一标签。 - 选择后,指示器视图(一个显示所选项目的 ImageView)应该出现。
非常基础和简单。但是,使用 Espresso 编写这个基本用户故事的测试却很难。当我运行特定的测试时,它总是失败并报错如下:
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "RecyclerView{id=2131624115, res-name=recyclerview_tag_list, visibility=VISIBLE, width=480, height=1032, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=15}"
我已经能看到列表,所以这没有意义。我甚至可以成功运行这个测试:
onView(withId(R.id.recyclerview_tag_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(1, click()));
这是完整测试:
在这里输入内容
@Test
public void shouldTagToggleSelected()
{
onView(withId(R.id.recyclerview_tag_list))
.perform(RecyclerViewActions
.actionOnItemAtPosition(1, click()));
onData(withId(R.id.relativelayout_tag))
.inAdapterView(withId(R.id.recyclerview_tag_list))
.onChildView(withId(R.id.imageview_tag))
.atPosition(1)
.check(matches(isDisplayed()));
//onView(withId(R.id.imageview_tag))
// .check(matches(isDisplayed()));
}
我想测试指示器视图是否仅在特定项目(或我选择的任何项目)中设置了可见性为
visible
。 你有什么想法吗?也许我错过了一些重要的东西。非常感谢!
onView(withId(R.id.recyclerview_tag_list))
,我知道这就是你的意思。谢谢,现在我能通过测试了。我仍然有点难以理解BoundedMatchers。如果可能的话我会尽量避免使用它们。但似乎不是这种情况。 - Neon Warge