使用espresso的recyclerview套件,是bug吗?

问题描述 投票:0回答:1

我的目标是测试RecyclerView滚动。当我执行示例代码时,会发生此错误:

E/TestRunner: androidx.test.espresso.PerformException: Error performing 'scroll RecyclerView to position: 17' on view 'with id: com.jakchang.hwahae:id/recyclerView'.
        at androidx.test.espresso.PerformException$Builder.build(PerformException.java:84)
       blabla ~~
     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 androidx.recyclerview.widget.RecyclerView and is displayed on the screen to the user)
    Target view: "RecyclerView{id=2131296427, res-name=recyclerView, visibility=VISIBLE, width=1080, height=0, 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, layout-params=android.widget.LinearLayout$LayoutParams@209ca0a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=90.0, child-count=0}"

我一直在寻找这个,但找不到任何解决办法。这是我的代码。请帮助我

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Rule
    @JvmField
    val mainActivity = ActivityTestRule(MainActivity::class.java)

    @Test
    fun useAppContext() {
        //mainActivity.launchActivity(Intent())

        Espresso.onView(ViewMatchers.withId(R.id.recyclerView)) 
            .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(17))
        Thread.sleep(10000)
    }
}
android android-recyclerview android-espresso
1个回答
0
投票

根据您的错误日志RecyclerView{..., child-count=0},看来您的视图正在异步加载数据,而调用回收器视图操作为时过早。解决此问题的最简单方法是在操作前调用Thread.sleep(...)

或者,您可以在测试用例中使用Espresso的IdlingResource

fun waitUntil(matcher: Matcher<View>): ViewAction = object : ViewAction {

    override fun getConstraints(): Matcher<View> {
        return any(View::class.java)
    }

    override fun getDescription(): String {
        return StringDescription().let {
            matcher.describeTo(it)
            "wait until: $it"
        }
    }

    override fun perform(uiController: UiController, view: View) {
        if (!matcher.matches(view)) {
            ViewPropertyChangeCallback(matcher, view).run {
                try {
                    IdlingRegistry.getInstance().register(this)
                    view.viewTreeObserver.addOnDrawListener(this)
                    uiController.loopMainThreadUntilIdle()
                } finally {
                    view.viewTreeObserver.removeOnDrawListener(this)
                    IdlingRegistry.getInstance().unregister(this)
                }
            }
        }
    }
}

接下来,您必须创建一个自定义的RecyclerView匹配器以检查其是否为空:

fun hasItemCount(itemCount: Matcher<Int>): Matcher<View> {
    return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
        override fun describeTo(description: Description) {
            description.appendText("has item count: ")
            itemCount.describeTo(description)
        }

        override fun matchesSafely(view: RecyclerView): Boolean {
            return view.adapter?.let {
                itemCount.matches(it.itemCount)
            } ?: false
        }
    }
}

然后,您可以同时使用动作和匹配器来等待,直到填充或准备好RecyclerView

onView(withId(R.id.recyclerView)).perform(
    waitUntil(hasItemCount(greaterThan(0))),
    RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(17)
)
© www.soinside.com 2019 - 2024. All rights reserved.