为什么不通过回收站视图测试?

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

我已经编写了测试,测试是否显示了回收站视图(id:comments_view),但是它始终失败,我也不知道为什么。当我检查布局(id:cm)时,测试通过。

我有以下片段代码:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="post"
            type="com.example.kotlinpostapi.apiObjects.Post" />
        <variable
            name="comments"
            type="java.util.List" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".views.MainActivity"
        android:id="@+id/cm">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/comments_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

测试代码(我正在导航到另一个代码):

@RunWith(AndroidJUnit4::class)
class CommentsListTest{

    @get: Rule
    val activityScenario = ActivityScenarioRule(MainActivity::class.java)

    @Test
    fun testCommentsAreDisplayed() {
        onView(withId(R.id.posts_view)).perform(actionOnItemAtPosition<PostAdapter.PostsViewHolder>(0, MyMatchers.clickChildView(R.id.show_comments_button)))

        //it fails
        onView(withId(R.id.comments_view)).check(matches(isDisplayed()))
        //it passes
        onView(withId(R.id.cm)).check(matches(isDisplayed()))
    }
}

怎么可能,如何测试我的回收站视图?

android kotlin android-recyclerview android-espresso
1个回答
0
投票

RecyclerView的高度设置为wrap_content,如果该元素不可见至少90%,则测试失败。

您可以做的是检查一个RecyclerView子级。

我首先声明以下方法:

fun nthChildOf(parentMatcher: Matcher<View?>, childPosition: Int): Matcher<View?>? {
    return object : TypeSafeMatcher<View>() {
        override fun describeTo(description: Description) {
            description.appendText("with $childPosition child view of type parentMatcher")
        }

        override fun matchesSafely(view: View): Boolean {
            if (view.parent !is ViewGroup) {
                return parentMatcher.matches(view.parent)
            }
            val group = view.parent as ViewGroup
            return parentMatcher.matches(view.parent) && group.getChildAt(childPosition) == view
        }
    }
}

由此,您可以检查是否显示了它的第一个孩子:

onView(nthChildOf(withId(R.id.comments_view), 0)).check(matches(isDisplayed()))

并检查其子元素中的一个元素(例如,recyclerview_element_id):

onView(allOf(
        withId(R.id.recyclerview_element_id),
        isDescendantOfA(
                nthChildOf(withId(R.id.comments_view), 0))
)).check(matches(isDisplayed()))

如果RecyclerView扩展到屏幕的可用空间,您可以尝试做的另一件事是更改RecyclerView的布局,以设置所有约束,并将height设置为0dp。

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/comments_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

我有这种方式,正在做:

onView(withId(R.id.myRecyclerviewId)).check(matches(isDisplayed()))

为我工作。

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