In previous post we analyzed what is needed to develop, run and debugmonkeyrunner scripts usingEclipseAndpydev.
#! /usr/bin/env monkeyrunner'''Created on Sep 10, 2012@author: diego'''import reimport sysimport osimport java# This must be imported before MonkeyRunner and MonkeyDevice,# otherwise the import fails.# PyDev sets PYTHONPATH, use ittry: for p in os.environ['PYTHONPATH'].split(':'): if not p in sys.path: sys.path.append(p)except: passtry: sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))except: passfrom com.dtmilano.android.viewclient import ViewClient, Viewfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice# usage: script [serialno]serialno = sys.argv[1] if len(sys.argv) > 1 else 'emulator-5554'device = MonkeyRunner.waitForConnection(30, serialno)try: device.wake()except java.lang.NullPointerException, e: print "ERROR: Couldn't connect to %s: %s" % (serialno, e)
These are the lines you shoshould add to every monkeyrunner script. Here you are a brief explanation of the snippet.
- The
Shebang line to invoke
Monkeyrunner interpreter if you are using Linux or Mac OS X. unfortunately this is not available on Windows. eclipse does not use this line but is needed if you want to simplify the way you are running the scripts from the command line.
- Some standard imports
- Pydev uses pythonpath whilemonkeyrunner ignores it. this snippet adds the components present inpythonpath
To SYS. Path and makes them visible tomonkeyrunner.
- Following, We need to locateAndroidviewclientWhich you shoshould have added to the environment. This can be also added inEclipseInRun events-> Environment.
Android_view_client_home shoshould point to yourAndroidviewclientInstallation to the parent folderSRC. That is, if you have downloadedAndroidviewclient
In/opt/androidviewclient and kept the same structure as the distribution, you shoshould setAndroid_view_client_home =/opt/androidviewclient
- The imports, which will now succeed because SYS. path contains the right components
- Gets the device's serial number from the command line or default
Emulator-5554.
- Connect to the device
- Check if the connection was successful. Because
Monkeyrunner. waitforconnection () returns
Monkeydevice even when the connection fails we need to go to this extra step to verify it.
Note: Enable pydev in eclipse, select interpreter monkeyrunner, configure the environment, and add variables.Android_view_client_home =/opt/androidviewclient, See
From: http://dtmilano.blogspot.ca/2012/09/monkeyrunner-importing-from-pythonpath.html