无法检查布局是否无法滚动

时间:2019-04-17 12:17:35

标签: android android-espresso

这是我的布局:

...
dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR1"
    }
}

如您所见,布局“ <?xml version="1.0" encoding="utf-8"?> <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"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/traderSummaryContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/mcgpalette0_100" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/jsonViewToolBar"> <TextView android:id="@+id/symbolLabelTextView" style="@style/labelTextViewStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="@dimen/default_margin" android:layout_marginTop="@dimen/half_default_margin" android:text="@string/symbol" app:layout_constraintEnd_toStartOf="@+id/symbolValueTextView" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/walletsRecyclerView" 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_toBottomOf="@+id/traderSummaryContainer" tools:listitem="@layout/wallet_list_item" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout> ”位于traderSummaryContainer之外,因此结果不可滚动。 真好 现在,我想编写检查此内容的Esrpesso的测试:

RecyclerView

但是测试失败,并显示以下消息:

@Test
    fun traderSummaryContainer_notScroll() {
        onView(withId(R.id.traderSummaryContainer))
                .perform(scrollTo())
        fail()
}

那我该如何检查呢?

P.S。

我用这个:

$ adb shell am instrument -w -r   -e debug false -e class 'com.myproject.activity.TraderDetailsActivityTest#traderSummaryContainer_notScroll' com.myproject.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

androidx.test.espresso.PerformException: Error performing 'scroll to' on view 'with id: com.myproject.debug:id/traderSummaryContainer'.
at androidx.test.espresso.PerformException$Builder.build(PerformException.java:84)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:87)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:316)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:177)

这是一个好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

我不确定这是否是一种好习惯,但我认为您想这样做

@Test
fun traderSummaryContainer_notScroll() {
    try {
        onView(withId(R.id.traderSummaryContainer)).perform(scrollTo())
        fail()
    } catch (e: PerformException) {
        // passed
    }
}

或者,您可以检查traderSummaryContainer是否不在ScrollView内:

onView(withId(R.id.traderSummaryContainer))
    .check(matches(not(isDescendantOfA(isAssignableFrom(ScrollView::class.java)))))