3

I'm looking for a way to detect if a screen changed from previous screen.. I mean: I have "Screen A" with full of buttons - I'm trying to press all of them, but only 1 of them will take me to "Screen B"..

I'm looking for a way that I could loop through all the buttons and break after reaching "Screen B" - via adb

I tried looking into uiautomator dump from previous screen and after each click on a button getting the UI hierarchy of the "new" screen and comparing them - but results weren't successful.

Any ideas? Thanks

Ragnarok
  • 170
  • 14

2 Answers2

1

I'm assuming you have the adb commands for input tap X Y covered already.

I would recommend pulling a screencap 1 second before and after the input tap and compare the screenshots to see if the screen changed. The code for screencap will look like this:

adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
adb shell rm /sdcard/screen.png

There are several ways to go about comparing the images. Since you're using Python you can easily compare pixels inside the image, but another method that I would use is compare the exact file size of the two images, a .jpg screenshot of the same screen will have the exact same file size, unless the clock in the upper right hand corner updated or other notifications rolled in. If there is a major size difference between screenshots of the two windows, you can use a simple greater-than/less-than if statement to determine.

Hope this helps,

Aaron Gillion
  • 2,227
  • 3
  • 19
  • 31
0

The idea of comparing hierarchy dumps is much better than comparing screenshots: dumps are just text data (~10KB instead of ~100KB) and they can be compared without inaccuracy that comes when you compare images. It means that you can just see exactly if something has changed in the UI hierarchy.

The reason why it didn't work for you is probably that dumps contain some time-specific data. For example, current time in the status bar. So, the solution here is to remove all text data, e.g. using this function

    def normalize(hierarchy):
        return re.sub(r'text=".*"', 'text=""', hierarchy)

Also consider using this uiautomator Python wrapper, there's device.dump() method, which will return you hierarchy dump as a string. Then the whole code will look like this:

# go to screen B
hierarchy_b = normalize(device.dump())
# go back to the screen A and press all buttons in a loop
hierarchy = normalize(device.dump())
is_screen_b = hierarchy == hierarchy_b
alexal1
  • 394
  • 3
  • 7