当前位置: 首页 > 知识库问答 >
问题:

Android测试:等待视图层次结构的根具有窗口焦点

费朗
2023-03-14

在Android Ui测试中,我想点击对话框中的一个旋转项,但它弹出了这个错误:

va.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@2dac97c7, window-token=android.view.ViewRootImpl$W@2dac97c7, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#10 ty=1 fl=#81810100 pfl=0x8 wanim=0x1030461 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x0}, decor-view-string=MultiPhoneDecorView{id=-1, visibility=VISIBLE, width=1600, height=2560, 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=1}}
. All Roots:
Root{application-window-token=android.view.ViewRootImpl$W@3c913e1, window-token=android.view.ViewRootImpl$W@21b23506, has-window-focus=true, layout-params-type=1002, layout-params-string=WM.LayoutParams{(310,600)(722x480) gr=#10000033 sim=#1 ty=1002 fl=#1860200 fmt=-3 wanim=0x10302db surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x0}, decor-view-string=PopupViewContainer{id=-1, visibility=VISIBLE, width=722, height=480, 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@3c913e1, window-token=android.view.ViewRootImpl$W@3c913e1, has-window-focus=false, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1800002 pfl=0x8 fmt=-3 wanim=0x1030462 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x10}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1136, height=1058, 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=1}}
Root{application-window-token=android.view.ViewRootImpl$W@2dac97c7, window-token=android.view.ViewRootImpl$W@2dac97c7, has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) sim=#10 ty=1 fl=#81810100 pfl=0x8 wanim=0x1030461 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x0}, decor-view-string=MultiPhoneDecorView{id=-1, visibility=VISIBLE, width=1600, height=2560, 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=1}}
at android.support.test.espresso.base.RootViewPicker.get(RootViewPicker.java:99)
at android.support.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:69)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:23)
at android.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:9)
at android.support.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:68)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:120)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

我试过了

onData(allOf(is(instanceOf(String.class)),containsString("A4"))).inRoot(isPlatformPopup()).perform(click());

而且

onView(withText(containsString("A4"))).inRoot(isFocusable()).check(matches(isDisplayed()));
onView(withText(containsString("A4"))).inRoot(withDecorView(not(getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));

但没有一个管用...有谁能告诉我怎么弄根吗?

共有1个答案

刁英朗
2023-03-14

您可以通过在运行测试之前取消系统对话框在代码中解决此问题:

MyActivity activityUnderTest = activityTestRule.getActivity();
activityUnderTest.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

或者使用adb在命令行上发送广播:

adb shell am broadcast -a android.intent.action.CLOSE_SYSTEM_DIALOGS

错误的另一个原因是后台应用程序被冻结(ANR)或运行缓慢,系统对话框显示“应用程序没有响应。是否关闭它?[等待][确定]”:

dependencies {
    ...
    androidTestImplementation "androidx.test.uiautomator:uiautomator:2.2.0"
}
companion object {
    @JvmStatic
    @BeforeClass
    fun dismissANRSystemDialog() {
        val device = UiDevice.getInstance(getInstrumentation())
        // If running the device in English Locale
        var waitButton = device.findObject(UiSelector().textContains("wait"))
        if (waitButton.exists()) {
            waitButton.click()
        }
        // If running the device in Japanese Locale
        waitButton = device.findObject(UiSelector().textContains("待機"))
        if (waitButton.exists()) {
            waitButton.click()
        }
    }
}
@BeforeClass
public static void dismissANRSystemDialog() throws UiObjectNotFoundException {
    UiDevice device = UiDevice.getInstance(getInstrumentation());
    // If the device is running in English Locale
    UiObject waitButton = device.findObject(new UiSelector().textContains("wait"));
    if (waitButton.exists()) {
        waitButton.click();
    }
    // If the device is running in Japanese Locale
    waitButton = device.findObject(new UiSelector().textContains("待機"));
    if (waitButton.exists()) {
        waitButton.click();
    }
}
# On a 320x480 screen, click at screen location [x=233,y=293] to tap an "OK" dialog button.
# Just in case there is a "Launcher isn't responding" system dialog.
adb shell input tap 233 293

# Send keystroke Arrow Right
sleep 3; adb shell input keyevent 22
# Send keystroke Arrow Right again
sleep 3; adb shell input keyevent 22
# Send keystroke Enter to press a button on the dialog
sleep 3; adb shell input keyevent 66

更多信息:

    null
 类似资料:
  • 问题内容: 您如何测试浏览器是否具有焦点? 问题答案: 我尚未在其他浏览器中对此进行过测试,但似乎可以在Webkit中使用。我让您尝试IE。:o) 单击开始间隔后,请更改浏览器窗口的焦点以查看结果更改。同样,仅在Webkit中进行了测试。

  • 问题内容: 我刚刚用ViewController类创建了一个单视图应用程序项目。我想显示一个位于我自己的类中的函数的UIAlertController。 这是我的课程,带有警告。 这是执行警报的ViewController。 这是我得到的,而不是优美的警报。 警告:尝试在Sprint1.AlertController:0x797cc500上显示其视图不在窗口层次结构中的UIAlertControl

  • 问题内容: 此函数失败,并显示运行时错误: 有人遇到过吗? UPD: 在模拟器iOS 8.1 / 8.4上失败。9.3工作正常。 UPD2: 创建如下: 问题答案: 通过在项目中放置以下修复程序,我使视图调试器再次工作: 当您的项目加载时,该方法将执行,如果当前尚未实现,则会导致并使用该实现,因此,视图调试使iOS8拥有了所寻找的行为。 要将其添加到您自己的项目中,请在您的项目中创建一个新的空Ob

  • 我在我的应用程序中使用MVVM模式。我有以下(简化版)VM类: 因此,一个Module2601_VM包含几个属性,以及Module2610_VM和ComPort_VM对象的列表。 我有一个MainModule_VM类中Module2601_VM对象的列表。 我想将这个Module2601集合及其子项绑定到树状视图中,并使用以下层次结构: 网关: 网关#0 COM#1 我的问题是,我的层次结构正常,

  • 我试图重现在Protege中显示给我的OWL本体(OWL.ttl)的类层次结构,您可以在标准URI位置http://www.w3.org/2002/07/OWL#下载。 我遇到的问题是,当我调用以获取层次结构根类时,返回的结果为零。因此我没有根类可以向下递归并构建层次结构。 ==============================================================

  • 我正在使用MultipartEntityBuilder将图像发送到php服务器,我将图片和视频发送到设备摄像头的目录中,我需要将文件保存在新的ArrayList上并将其发送到php服务器。 总是报告错误: 我的代码 更新