Android Espresso: Как мне ввести текст в editText, встроенный в TextInputLayout - PullRequest
0 голосов
/ 10 июля 2020

Ищу способ replaceText или typeText в тексте редактирования, встроенном в TextInputLayout.

Я пробовал несколько таких вещей:

perform(ViewActions.typeTextIntoFocusedView("String")); 

, но всегда получаю это:

java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(((is displayed on the screen to the user) and has focus on the screen to the user) and (supports input methods or is assignable from class: class android.widget.SearchView))

1 Ответ

0 голосов
/ 10 июля 2020

Вам нужно будет создать новый ViewAction, так как Espresso не имеет доступа к EditText, встроенному в TextInputLayout. Вы можете использовать что-то вроде этого

class TextInputLayoutActions {
    class ReplaceTextAction(private val text: String): ViewAction {
        override fun getDescription(): String {
            return String.format(Locale.ROOT, "replace text(%s)", text)
        }

        override fun getConstraints(): Matcher<View> {
            return allOf(isDisplayed(), ViewMatchers.isAssignableFrom(TextInputLayout::class.java))
        }

        override fun perform(uiController: UiController?, view: View?) {
            (view as? TextInputLayout?)?.let {
                it.editText?.setText(text)
            }
        }

    }

    companion object {
        @JvmStatic
        fun replaceText(text: String): ViewAction {
            return actionWithAssertions(ReplaceTextAction(text))
        }
    }
}

, а затем использовать его как

<ViewInteraction>.perform(TextInputLayoutActions.replaceText(<text>)
...