Saturday, October 01, 2011

Android Application Testing Guide: Q&A

Q: i am working on android on automation i have setup all the environment required for it like(eclipse,jdk, Android SDk) and i am having monkeyrunner installed.

Now i want to automate the process to do this task--The monkeyrunner API can apply one or more test
suites across multiple devices or emulators. You can physically attach all the
devices or start up all the emulators (or both) at once, connect to each one in
turn programmatically, and then run one or more tests

i thought of doing this through monkeyrunner extending plugins concept, this concept doesn't help me becoz communicaton between two or more plugins creating a new problem. so ,please help me how to get my task done through other approach



Posted by vijju



A: Probably the best solution to your problem is based on the divide and conquer philosophy. Create your test scripts in a way that they receive a serial number argument to determine which device to connect to. The following is a very simple example of this concept. We will call it getprop.mr


#! /usr/bin/env monkeyrunner

import sys, os
from com.android.monkeyrunner import MonkeyRunner

prog = os.path.basename(sys.argv[0])

def usage():
        print >>sys.stderr, "usage: %s serial-no" % prog
        sys.exit(1)

def main():
        if len(sys.argv) != 2:
                usage()

        serialno = sys.argv[1]
        print "waiting for connection to %s..." % serialno
        device = MonkeyRunner.waitForConnection(30, serialno)

        s = ""
        for p in ['build.manufacturer', 'build.device', 'build.model']:
                s += " " + device.getProperty(p)
        print s

if __name__ == '__main__':
    main()

This script expects the first argument to be the serial number of the emulator or device where the specific tests will be run. In this simple case we are just obtaining some properties to identify it.


Once we have our test script we need a driver to run it in every device we specify. In this example we will be using a python script but a bash script would be good enough.


#! /usr/bin/env python

import os

devices = [ 'XXX00000001', 'emulator-5554' ]
cmd = 'getprop.mr'

for d in devices:
        os.system(cmd + " " + d)

This script will run the test, getprop.mr in this case, for each of the devices which serial numbers are in the list.
I hope this is the answer you were looking for.

4 comments:

Kavita said...

"getprop.mr" is it file name and is it included in src folder in python project in monkeyrunner?

Diego Torres Milano said...

"We will call it getprop.mr...", getprop.mr is the name of the script we created in this example.

itsmyworld said...

hii
plz plz can any one tell how to start monkey runner what should be installe dto run monkey runner scripts

Diego Torres Milano said...

monkeyrunner is part of the android SDK tools. You can run monkeyrunner scripts by /tools/monkeyrunner script.mr.