Unexpected error while obtaining UI hierarchy:使用uiautomatorviewer定位元素报错

1.问题描述

在使用Uiautomatorviewer 进行元素定位时,点击 快照 按钮出现以下错误:

Unexpected error while obtaining UI hierarchy:使用uiautomatorviewer定位元素报错_第1张图片

2.发生原因

  Unexpected error while obtaining UI hierarchy(获取UI层次结构时发生意外错误)。

  发生这个错误一般是由这两种情况导致的:

  1.jdk版本问题 (1.9jdk是不兼容uiautomatorviewer的,1.8的支持,再向下是否兼容本人不太清楚,可自行尝试)

   2.android版本问题

       我这里发生这个报错就是因为这个原因:

       我使用的是android 9.0的设备,而uiautomatorviewer.bat不支持对9.0系统直接截屏(好像对8.0以后的android系统都不支持直接截屏)

 3.解决方案

      虽然不能直接进行截屏,但是我们可以间接来截屏:

      即:用Uiautomatorviewer去读取已经准备好的截图文件,从而进行元素定位

      具体办法如下:

      1.操作你的待测设备到你需要截图的界面

       2.点击Uiautomatorviewer的图标,可以看到Open UI dump Files需要两个文件:

              ScreenshotUI  XML  Dump,开始准备需要的文件

       确保你的pc已经安装adb工具,然后打开命令行工具,在Dos窗口中输入以下指令:

       获取UI XML Dump的指令

            adb shell uiautomator dump /sdcard/sc.uix(将uix文件转储到手机上,位置自己指定)

            adb pull /sdcard/sc.uix  e:/

       获取Screenshot的指令

           adb shell screencap  -p /sdcard/sc.png((将获取的截图文件转储到手机上,位置自己指定))

           adb pull /sdcard/sc.png  e:/

      具体看图:

Unexpected error while obtaining UI hierarchy:使用uiautomatorviewer定位元素报错_第2张图片

     3.加载截图文件

Unexpected error while obtaining UI hierarchy:使用uiautomatorviewer定位元素报错_第3张图片

    4.点击ok后,则可以正常加载文件

你可能感兴趣的:(软件测试)