AndroidX测试对话框
自从AndroidX发布以来,我们在单元测试中使用了Robolectric和Espresso。但是我们无法找到一种测试对话框的方法。 浓缩咖啡社区建议我们:AndroidX测试对话框,android,unit-testing,robolectric,androidx,androidx-test,Android,Unit Testing,Robolectric,Androidx,Androidx Test,自从AndroidX发布以来,我们在单元测试中使用了Robolectric和Espresso。但是我们无法找到一种测试对话框的方法。 浓缩咖啡社区建议我们: onView(withId(android.R.id.button1)) .inRoot(isDialog()) .check(matches(isDisplayed())) .perform(click()) 但这里的测试例外地失败了 Wai
onView(withId(android.R.id.button1))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click())
但这里的测试例外地失败了
Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds.
调试代码时,我们发现找到了该对话框,但不知何故,decorView.hasWindowFocus()
返回false
是不是因为Robolectric中的某些缺陷,视图无法获得窗口焦点?有办法解决吗?目前,我们正在求助于机器人分子阴影
我们使用的是Robolectric4.2
和AndroidX测试1.1.0
版本
更新:
显然,在显示对话框之后,活动保持窗口焦点,所以很可能是Robolectric bug(或我的误解)。Robolectric有一种方法可以解决这个问题,它是
ShadowAlertDialog.getLatestDialog()
要在“确定”按钮上执行单击操作,可以如下使用:
((AlertDialog)ShadowAlertDialog.getLatestDialog())
.getButton(AlertDialog.BUTTON_正值).performClick();