我有一个具有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。我需要获取文本而不是匹配文本。