3

I have an issue running uiautomator in my python script (a similar shell script runs fine). I use the following method for convenience:

    import subprocess

    def host_exec(cmd):
      p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
      out= p.communicate()
      return out

then I use a combination of

    host_exec("path_to_adb shell uiautomator dump /sdcard/dump.xml")
    host_exec("path_to_adb pull /sdcard/dump.xml ./dump.xml")
    --read file here--
    host_exec("path_to_adb shell rm /sdcard/dump.xml")
    host_exec("rm ./dump.xml")

I use this a lot, and the first 1-2 run throughs usually work, after that it seems to sporadically fail at line 1 (creating the dump.xml) giving error code

    ERROR: null root node returned by UiTestAutomationBridge.

I looked into UiTestAutomationBridge.java (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/accessibilityservice/UiTestAutomationBridge.java) and it seems to me that the cause is that the timeout that the devs placed on line 65, that is sometimes enough, and sometimes not enough for the bridge to form correctly. (correct me if I'm wrong.

What really bugs me is that I've been using the same code for almost a week, and I only got the error for the first time this morning, and now it's happening almost every single time. I've checked to make sure I close the file after reading it, I delete the files before dumping and copying new files, and I have tried up to time.sleep(10) before and after each command, but still no go. I've run the 'time' command from command line for my shell script that hasn't failed a single time yet, and it takes about 2 seconds for the entire process, so 10 seconds should be enough for anything that is bogging down to get sorted out.

4

2 回答 2

0

我在调试https://github.com/xiaocong/uiautomator时遇到了几乎相同的问题。我不知道为什么我在启动并杀死 uiautomator 一段时间后总是出现异常,即使重新启动设备也无法解决它。

我尝试过的唯一解决方法是在设置中将设备恢复出厂设置。

于 2013-08-26T15:42:11.253 回答
0

加载时,该问题通常在对话框之间出现(可能是“应用程序停止响应”对话框出现)。其他需要注意的是could not get idle state简单的closed

这些都表明手机上有一些非理想的用户界面状态,通常只能通过非直观的方法来解决。也许快速更新的 textView 阻止了 ui 锁定 1000 毫秒的空闲状态,并且您必须终止应用程序或单击您知道导航向上箭头所在的位置,而这次没有明确引用 xml 转储。

无论哪种方式,它都需要你退后一步思考。我可以告诉您,通常空根节点会在活动加载时自行解决,因此您只需输入一个 while 循环,直到满足某些条件(因为它最终相当可靠)。正如我建议的那样,该条件是输出字符串以UI hierchary dumped to:

同样,xml 并不总是完美的,可能会缺少等号、引号或括号,但同样的原则也适用。拿一个新的转储,你很好。

于 2018-05-22T00:20:46.290 回答