androidandroid-testingandroid-monkey

Save Android Monkey random run to script format for later replay


Is there a way to save the random run of Android Monkey into a script with the proper format to later replay it by running:

adb shell monkey -p <package_name> -f script_file 1

EDIT:

I know there is a seed flag (-s), but that's not what I want. I have to be able to work with the generated script before feeding it back to the Monkey.


Solution

  • Not an easy way, but you could do a reverse engineering on the monkey script source to create a script that takes the output of the monkey command and generates the monkey script.

    So you could run:

    adb shell monkey -p <package_name> -v -v 1 > monkey-logs.txt

    And then*:

    convert-to-monkey-script.sh monkey-logs.txt

    For example, one output of the monkey call:

    Replaying 11 events with speed 1.0
    :Sending Touch (ACTION_DOWN): 0:(450.0,450.0)
    :Sending Touch (ACTION_UP): 0:(450.0,450.0)
    Sleeping for 45 milliseconds
    ...
    

    Becomes the following monkey script (read the monkey source to understand better the arguments):

    type= raw events
    count= 2
    speed= 1.0
    start data >>
    DispatchPointer(6934862,6934862,0,450.0,450.0,0.0,0.0,0,1.0,1.0,0,0)
    DispatchPointer(6934862,6934862,1,450.0,450.0,0.0,0.0,0,1.0,1.0,0,0)
    

    Which can be run with (with the content above in the monkey.script file and after a adb push):

    adb shell monkey -p <package_name> -f monkey.script 1

    I've made a simple gist for myself that convert adb taps commands into monkey script format (because they're faster) here, so I think that is possible to make a general script for that.

    *Note: convert-to-monkey-script.sh doesn't exist. As I said, someone COULD do it