简体   繁体   English

Androidx:如何在 UI 测试(Espresso)中测试 Slider?

[英]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.

相关问题 AndroidX Espresso 测试:未找到测试且测试套件为空 - AndroidX Espresso Test: No tests were found and Empty test suite 什么是AndroidX.Test框架,它如何影响我的单元/机器人/咖啡测试? - What is the AndroidX.Test Framework and how does it affect my unit/robolectric/espresso tests? androidx.test.espresso.AmbiguousViewMatcherException:如何在浓缩咖啡测试中对框架布局内的通用卡片视图执行操作? - androidx.test.espresso.AmbiguousViewMatcherException: How can you perform actions on a generic card view inside a frame layout in espresso tests? 如何使用 Espresso 测试记录器为以编程方式创建的视图创建 UI 测试 - How to create UI tests with Espresso Test Recorder for programmatically created views 如何为 Espresso 测试切换 AndroidX 架构组件任务执行器 - How to switch out AndroidX Architecture Component task executor for Espresso tests 如何在androidx.test.espresso上单击指定recyclerview? - How do I click on specify recyclerview on androidx.test.espresso? 我应该如何正确关闭我的AndroidX Espresso测试? - How should I properly shutdown my AndroidX Espresso test? 如何在 Espresso 的 UI 测试中模拟 textwatcher? - How to emulate textwatcher on UI tests with Espresso? 为什么androidx espresso .check()导致androidx.test.espresso.IdlingResourceTimeoutException - Why is androidx espresso .check() causing androidx.test.espresso.IdlingResourceTimeoutException 如何在espresso UI测试中检查动态文本 - How to check dynamic text in espresso UI test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM