如何使用Espresso测试嵌套的RecyclerView(RecyclerView内部的RecyclerView) [英] How to test nested RecyclerView (RecyclerView inside RecyclerView) with Espresso

查看:117
本文介绍了如何使用Espresso测试嵌套的RecyclerView(RecyclerView内部的RecyclerView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在另一个RecyclerView(parentRecyclerView)内实现了RecyclerView(childRecyclerView),图片说明了所有问题:

I have implemented RecyclerView(childRecyclerView) inside another RecyclerView(parentRecyclerView), picture blow explains all the implentation:

我想编写一个浓缩咖啡测试,检查childRecyclerView内部的所有TextView是否均具有预期的文本,我已经检查了此答案 https://stackoverflow.com/a/34141230/3522182 ,所以在我的情况下是这样的:

I want to write an espresso test that checks all TextViews inside the childRecyclerView are with the expected text, I have checked this answer https://stackoverflow.com/a/34141230/3522182, so in my case is this:

onView(allOf(isDescendantOfA(withRecyclerView(R.id.parentRecyclerView).atPosition(0)),
                isDescendantOfA(withRecyclerView(R.id.childRecyclerView).atPosition(0)),
                withId(R.id.textView1)))
                .check(matches(withText("some text")));

可以,但是我遇到的问题是,当我在 withRecyclerView(R.id.parentRecyclerView).atPosition(0)中为atPosition传递1时,我得到一个错误,指出视图不是在层次结构中找到:

it's ok, but the problem I had was that when I pass 1 for atPosition in withRecyclerView(R.id.parentRecyclerView).atPosition(0) I get an error that says the view is not found in the hierarchy:

No views in hierarchy found matching: (is descendant of a: RecyclerView with id: com.example.testapp:id/parentRecyclerView at position: 1

还有另一种方法来测试childRecyclerView的内容吗?

is there another way to test childRecyclerView contents?

推荐答案

我使用了它并且有效:

onView(withId(R.id.parentRecyclerView))
    .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(0))

onView(
    allOf(
        isDescendantOfA(allOf(
            withId(R.id.contentRootLayout),
            hasDescendant(withText("title text"))
        )),
        isDescendantOfA(withId(R.id.childRecyclerView)),
        withId(R.id.textView1),
        withText("some text")
    )
).check(matches(isDisplayed()))

这篇关于如何使用Espresso测试嵌套的RecyclerView(RecyclerView内部的RecyclerView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆