-1

寻找一种方法将文本或类型文本替换为嵌入在 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))
4

2 回答 2

0

您必须创建一个新的,因为EspressoViewAction无法访问. 你可以使用这样的东西EditTextTextInputLayout

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>)
于 2020-07-10T17:05:18.687 回答
0

我尝试过这种方式如果您可以访问该活动,您也可以使用 findviewById

 @Test
fun testSave() {
    val book = Book(
        title = "Machine Learning",
        author = "Mohammed Fahim khan",
        year = "2021", imageUrl = ""
    )
    val testViewModel = BookViewModel(FakeBookRepositoryIT())
    launchFragmentInHiltContainer<BookDetailsFragment>(factory = fragmentFactory) {
        viewModel = testViewModel
        binding.etTitle.editText?.setText(book?.title.toString())
        binding.etName.editText?.setText(book?.author.toString())
        binding.etYear.editText?.setText(book?.year.toString())

    }


    /*onView(withId(R.id.et_title)).perform(replaceText(book.title))
    onView(withId(R.id.et_name)).perform(replaceText(book.author))
    onView(withId(R.id.et_year)).perform(replaceText(book.year))*/
    onView(withId(R.id.btn_save)).perform(click())
    val list = testViewModel.bookList.getOrAwaitValue()
    assertThat(list).contains(book)
}
于 2021-06-10T20:34:06.280 回答