4

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.

MishaP
  • 188
  • 1
  • 9

2 Answers2

1

I met almost the same issue when I was debugging https://github.com/xiaocong/uiautomator. I don't know the reason why I was always getting exception after I had started and killed the uiautomator for some times, even rebooting the device could not resolve it.

The only way to resolve it I tried was to factory reset the device in settings.

Xiaocong
  • 96
  • 3
0

The issue often presents itself between dialogs when loading (perhaps as the 'app stopped responding' dialog comes up). Other ones to watch out for are could not get idle state and simply closed.

These all indicate some non-ideal ui state on the phone which can often only be resolved by non-intuitive methods. Perhaps a rapidly updating textView is preventing the ui from locking the idle state of 1000ms, and you have to kill the app or click in the location you know the navigate up arrow is without explicitly referencing an xml dump this time.

Either way it requires you step back and think. I can tell you that typically null root node resolves itself as the activity loads so you need only enter a while loop til some condition is met (as it eventually fairly reliably is). That condition, as I suggest you make it, is that the output string begins with UI hierchary dumped to:

Similarly the xml is not always perfect, can be missing an equal sign or quotation or bracket, but again the same principle applies. Take a new dump and you're good.

gamesguru
  • 92
  • 1
  • 3
  • 6