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

时间:2017-04-13 23:16:00

标签: android android-espresso

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

测试会启动Foo c = (a < b).value_or(b);,其中AlertDialog需要输入密码。测试的开始如下:

EditText

此测试打开对话框,找到EditText,并在文本中键入内容。然后它没有尝试找到&#34; Ok&#34;按钮:

    // 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())

    ...

看起来它选择了非聚焦活动窗口而不是聚焦对话窗口(您可以根据它们的大小来区分)。这没有任何意义,因为前两个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}} 调用找到了正确的窗口就好了。

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

1 个答案:

答案 0 :(得分:0)

我绝对不是Espresso专家,而且我还远未对您的代码进行审核,但我怀疑 inRoot 调用在您的代码中被过度使用。正如人们可以在 inRoot 方法规范中看到的那样,它旨在“改变/建立一个范围与给定根匹配器选择的根的交互”。您似乎使用它来确保您仍然在对话框上下文中执行操作。因此,最终的 preform 方法调用可能不会针对正确的 View 。希望它有用。