为什么在RecyclerView上使用swipeRight时测试不会失败?

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

这里是我的带有RecyclerView的xml

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/tradersRecyclerView"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:visibility="@{handler.tradersList.size > 0 ? View.VISIBLE : View.GONE}"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/tradersToolBar"        
   tools:listitem="@layout/trader_list_item" />

在列表项目上向左滑动比我显示子项目(恢复,停止)

像这样的东西:

enter image description here

子项目仅在向左滑动时显示。向右滑动没有任何反应。

这里Espresso的测试检查swipeLeft是否正常工作

 @Test
 fun scroll_itemList_swipeLeft() {
    // scroll
    onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
    // swipe
    onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, swipeLeft()))
 }

尼斯。测试通过。

现在我创建一个必须在向右滑动时失败的测试。因为滑动时没有任何反应

@Test
fun scroll_itemList_notSwipeRight() {
     // scroll
     onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
     // swipe
     onView(withId(R.id.tradersRecyclerView))
                .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, ViewActions.swipeRight()))
 }

但这次测试也成功通过。为什么?我需要创建测试检查而不是刷卡没有任何反应

附:在向左滑动并向右滑动时进行测试

@Test
fun itemList_swipeLeft_isDisplayedSwipeContainer() {
    onView(withId(R.id.tradersRecyclerView))
            .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
    onView(withId(R.id.tradersRecyclerView))
            .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, swipeLeft()))
    onView(withRecyclerView(R.id.tradersRecyclerView).atPositionOnView(checkItemCount, R.id.swipeContainer))
            .check(matches(isDisplayed()))
}

@Test
fun itemList_swipeRight_notDisplayedSwipeContainer() {
    onView(withId(R.id.tradersRecyclerView))
            .perform(scrollToPosition<RecyclerView.ViewHolder>(checkItemCount));
    onView(withId(R.id.tradersRecyclerView))
            .perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(checkItemCount, ViewActions.swipeRight()))
    onView(withRecyclerView(R.id.tradersRecyclerView).atPositionOnView(checkItemCount, R.id.swipeContainer))
            .check(matches(not(isDisplayed())))
}
android android-recyclerview android-espresso ui-testing
1个回答
1
投票

将这样的东西添加到你的睾丸(两者):

onView(withId(R.id.*your_resume_button_id*)).check(mathes(isDisplayed()))

这将添加一个view可见性的检查,只有在向左滑动时才能看到它。

因此,第一个测试将通过,第二个测试将失败,因为当向右滑动时,此视图将不可见。

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.