如何在edittext中插入值?(AlertDialog,Espresso)

时间:2018-02-20 09:04:07

标签: android kotlin android-espresso

我有这样的对话框和文本框,如何自动插入文本?

enter image description here

@Test
fun myTest() {
...
onView(allOf(childAtPosition(
                allOf(withId(android.R.id.custom),
                        childAtPosition(
                                withClassName(`is`("android.widget.FrameLayout")),
                                0)),
                0),
                isDisplayed()))
                .perform(replaceText("а"), closeSoftKeyboard())

private fun childAtPosition(
            parentMatcher: Matcher<View>, position: Int): Matcher<View> {

        return object : TypeSafeMatcher<View>() {
            override fun describeTo(description: Description) {
                description.appendText("Child at position $position in parent ")
                parentMatcher.describeTo(description)
            }

            public override fun matchesSafely(view: View): Boolean {
                val parent = view.parent
                return (parent is ViewGroup && parentMatcher.matches(parent)
                        && view == parent.getChildAt(position))
            }
        }
    }
}

我是这样做的,但它并不总是有效并且发生了下降。

1 个答案:

答案 0 :(得分:-1)

你可以用:

editText.setText("Hello World")

或如果您想使用提示:

editText.setHint("Hello World")

你可以在xml上设置它:

android:hint="Hello World"