Android GUI crawler

2019-04-13 23:48发布

问题:

Anyone knows a good tool for crawling the GUI of an android app? I found this but couldn't figure out how to run it...

回答1:

Personally, I don't think it would be too hard to make a simple GUI crawler using MonkeyRunner and AndroidViewClient.

Also, you may want to look into uiautomator and UI Testing

Good is a relative term. I have not used Robotium, but it is mentioned in these circles a lot.

EDIT - Added example based on comment request.

Using MonkeyRunner and AndroidViewClient you can make a heirarchy of views. I think AndroidViewClient has a built-in mechanism to do this, but I wrote my own. This code tries to produce a layout similar to that used by the Linux tree command. The space and line variables are used to setup the "prefix" prepended on each line. Of course, this code uses a depth-first, recursive traversal.

def printViewListDepth(view, depth=0):
    space = '|' * int(not depth == 0) 
    space += (' ' * 2 * (depth-1)) + '|' * int(not depth-1 <= 0)
    line  = '_' * int(not depth == 0) * 2
    text = view.getText()
    text = text[:10] + int(len(text) > 10) * '...'
    print " [*] %s%s%s %s %s" % (
        space, line, view.getUniqueId(), 
        view.getClass().replace('android.widget.', ''), text)
    for ch in view.children:
        printViewListDepth(ch, depth+1)

You call printViewListDepth as follows, using a ViewClient returned by AndroidViewClient:

printViewListDepth(viewClient.root)

Note that in the above implementation, the class of View is truncated, by removing "android.widget." and the the text of a View is truncated at 10 characters. You can change these to suit your needs.

Edit Crawling the GUI

With AndroidViewClient you can query whether a View is clickable, someView.isClickable(). If it is clickable, you can invoke a touch event on it, someView.touch(). Assuming most button clicks open a different Activity, you will need to come up with a mechanism of getting back to where you came from to do the recursion.

I imagine that it can be done with some effort, but you may want to start with something as simple as invoking a BACK button press, device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP). You will probably need to handle application-specific special cases as they arise, but this should get you off to a decent start.



回答2:

You can take a look at Testdroid App Crawler This is available in Testdroid Cloud as project type and you can easily run it on 15 free devices.