AndroidX测试对话框

问题描述 投票:0回答:1

自AndroidX发布以来,我们在单元测试中使用Robolectric和Espresso。但我们无法找到测试对话框的方法。 Espresso社区建议我们这样做:

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中的一些错误导致视图无法获得窗口焦点?有办法解决吗?目前我们正在回归Robolectric阴影。

我们正在使用Robolectric 4.2AndroidX test 1.1.0版本

更新:显然在显示对话框后,活动会保持窗口焦点,所以很可能是Robolectric错误(或者我的误解)。

android unit-testing robolectric androidx androidx-test
1个回答
0
投票

Robolectric有一种方法,就是这样

ShadowAlertDialog.getLatestDialog()

要在“确定”按钮上执行单击操作,您可以像这样使用它:

((AlertDialog) ShadowAlertDialog.getLatestDialog())
        .getButton(AlertDialog.BUTTON_POSITIVE).performClick();
© www.soinside.com 2019 - 2024. All rights reserved.