[英]Androidx : How to test Slider in UI tests (Espresso)?
How can I perform actions on slider (move cursor)?如何在 slider(移动光标)上执行操作?
How can I check slider value?如何检查 slider 值?
Are there any existing functions for this?是否有任何现有的功能?
Slider look like that: Slider 看起来像这样:
<com.google.android.material.slider.Slider
android:id="@+id/sliderRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stepSize="1"
android:valueFrom="1.0"
android:valueTo="5.0" />
And Espresso check: onView(withId(R.id.sliderRating)).check( -> TODO <- )
和浓缩咖啡检查:
onView(withId(R.id.sliderRating)).check( -> TODO <- )
It's easy to create a couple of methods to do that.很容易创建几个方法来做到这一点。
Here is what I have:这是我所拥有的:
fun withValue(expectedValue: Float): Matcher<View?> {
return object : BoundedMatcher<View?, Slider>(Slider::class.java) {
override fun describeTo(description: Description) {
description.appendText("expected: $expectedValue")
}
override fun matchesSafely(slider: Slider?): Boolean {
return slider?.value == expectedValue
}
}
}
fun setValue(value: Float): ViewAction {
return object : ViewAction {
override fun getDescription(): String {
return "Set Slider value to $value"
}
override fun getConstraints(): Matcher<View> {
return ViewMatchers.isAssignableFrom(Slider::class.java)
}
override fun perform(uiController: UiController?, view: View) {
val seekBar = view as Slider
seekBar.value = value
}
}
}
The first one, the custom view matcher, is to test that the Slider
has a specific value and the second one, the custom view action, is to set a new value.第一个,自定义视图匹配器,用于测试
Slider
是否具有特定值,第二个,自定义视图操作,用于设置新值。
A simple test example using them just to get the idea of how they work:一个简单的测试示例,使用它们只是为了了解它们是如何工作的:
@Test
fun testSlider() {
onView(withId(R.id.sliderRating)).check(matches(withValue(1.0F)))
onView(withId(R.id.sliderRating)).perform(setValue(2.0F))
onView(withId(R.id.sliderRating)).check(matches(withValue(2.0F)))
}
This test checks first of all if the slider has value 1.0, then changes its value to 2.0 and uses the matcher to check that the value is now 2.0.该测试首先检查 slider 的值是否为 1.0,然后将其值更改为 2.0,并使用匹配器检查该值现在是否为 2.0。
I have the code in kotlin but if you need it in java it can be easily transformed (android studio can do that).我在 kotlin 中有代码,但如果你在 java 中需要它,它可以很容易地转换(android studio 可以做到)。 As the question is not tagged with the language I don't know which one do you need, tag it next time.
由于问题没有用语言标记,我不知道您需要哪一种,下次标记它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.