使用Espresso显示对话框时在“活动”中访问视图

时间:2020-01-05 22:11:14

标签: android android-espresso android-testing

我有一个具有logsTv视图的活动,我想使用以下方法获取其文本:

fun ViewInteraction.getText(): String {
    var text = ""

    perform(object : ViewAction {
        override fun getConstraints(): Matcher<View> {
            return ViewMatchers.isAssignableFrom(TextView::class.java)
        }

        override fun getDescription(): String {
            return "Text of the view"
        }

        override fun perform(uiController: UiController, view: View) {
            val tv = view as TextView
            text = tv.text.toString()
        }
    })

    return text
}

private fun getLogsText(): String {
    return onView(withId(R.id.logsTv))
        .inRoot(withDecorView(`is`(getActivity()!!.window.decorView)))
        .getText()
}

问题在于,此刻有一个活动对话框(我无法将其隐藏),所以它失败了:

 java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Root:

是否可以在不关闭对话框的情况下从logsTv获取文本?

P.S。我需要获取文本而不是匹配文本。

0 个答案:

没有答案
相关问题