Android Espresso .inRoot(isDialog()) 选择错误的 Root



我有几个使用Espresso测试我的应用程序的UI的androidTest测试。 我注意到最近有一个开始失败,原因似乎没有任何意义。

该测试启动一个AlertDialog,该在EditText中获取密码。 测试的开始如下所示:

    // click the icon that launches the dialog
    onView(withId(R.id.action))
            .perform(click())
    onView(withText("Enter Password"))
            .inRoot(isDialog())
            .check(matches(isDisplayed()))
    onView(withId(R.id.passwordInput))
            .inRoot(isDialog())
            .perform(typeText("test password"))
    onView(withText("Ok"))
            .inRoot(isDialog())
            .perform(click())
    ...

此测试将打开对话框,找到 EditText,然后键入文本。 然后,尝试查找"确定"按钮失败:

java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root:
Root{application-window-token=android.view.ViewRootImpl$W@c05d1c2, window-token=android.view.ViewRootImpl$W@c05d1c2, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 wanim=0x103045b needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}}
. All Roots:
Root{application-window-token=android.view.ViewRootImpl$W@3827e0d, window-token=android.view.ViewRootImpl$W@3827e0d, has-window-focus=true, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1800002 fmt=-3 wanim=0x103045c needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1026, height=468, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}}
Root{application-window-token=android.view.ViewRootImpl$W@c05d1c2, window-token=android.view.ViewRootImpl$W@c05d1c2, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 wanim=0x103045b needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}}

看起来它正在选择非焦点活动窗口而不是焦点对话框窗口(您可以根据它们的大小来区分(。 这是没有意义的,因为前两个.inRoot调用找到正确的窗口就好了。

关于根选择的文档非常少,所以我不确定为什么这不起作用,这是一个错误吗?

我绝对不是浓缩咖啡专家,我远未对您的代码进行审查,但我怀疑 inRoot 调用在您的代码中被过度使用。正如人们在 inRoot 方法规范中看到的那样,它旨在"更改/使交互作用域限定为给定根匹配器选择的根"。您似乎使用它来确保仍然在对话上下文中执行操作。因此,也许最终的预制表单方法调用并不针对正确的视图。希望对您有所帮助。

相关内容

最新更新