Tuesday, September 11, 2012

monkeyrunner: importing from PYTHONPATH

In previous post we analyzed what is needed to develop, run and debug monkeyrunner scripts using Eclipse and PyDev.


#! /usr/bin/env monkeyrunner
'''
Created on Sep 10, 2012

@author: diego
'''

import re
import sys
import os
import java

# This must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails.
# PyDev sets PYTHONPATH, use it
try:
    for p in os.environ['PYTHONPATH'].split(':'):
       if not p in sys.path:
          sys.path.append(p)
except:
    pass

try:
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient, View
from 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 should add to every monkeyrunner script. Here you are a brief explanation of the snippet.

  1. 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.
  2. Some standard imports
  3. PyDev uses PYTHONPATH while monkeyrunner ignores it. This snippet adds the components present in PYTHONPATH to sys.path and makes them visible to monkeyrunner.
  4. Following, we need to locate AndroidViewClient which you should have added to the environment. This can be also added in Eclipse in Run Configurations -> Environment.
    ANDROID_VIEW_CLIENT_HOME should point to your AndroidViewClient installation to the parent folder of src. That is, if you have downloaded AndroidViewClient in /opt/AndroidViewClient and kept the same structure as the distribution, you should set ANDROID_VIEW_CLIENT_HOME=/opt/AndroidViewClient/AndroidViewClient
  5. The imports, which will now succeed because sys.path contains the right components
  6. Gets the device's serial number from the command line or default to emulator-5554.
  7. Connect to the device
  8. Check if the connection was successful. Because MonkeyRunner.waitForConnection() returns a MonkeyDevice even when the connection fails we need to go to this extra step to verify it.



5 comments:

Durairaj said...

Is there a way to include AndroidViewClient with robotframework and write test cases/keywords ?

Diego Torres Milano said...

@நாகை நண்பன்,
Cannot answer this. I haven't used robotframework with android.
You can give it a try a post here your results.
If in the process you have any questions don't hesitate to as here as well.

Smitha said...

Do you have any info about the architecture of Monkeyrunner.
Amused by its way of working:)

Also, wanted to know if Monkeyrunner can be used to test phones in the market already?
Does this work for the Dual Sim phones too?

Thanks,
Smtiha

Diego Torres Milano said...

@Smitha,
The source code should be able to get you the answers.

Yes, should work on non-development phones.
I can't see why it wouldn't work on Dual Sim phones, but never tried.

odile said...

Hi,
I am trying to run a monkeyRunner script from eclipse, and I am almost there, I think.
I am using the latest of everything and I don't know if that is a problem.
I downloaded JyDev, created my project.. using the script that was here somewhere. It compiles.
Now, when it comes to running it, I get errors.
Either I run it as a pyrun configuration (having added monkeyrunner as an interpreter) and I get a command line error, or I run it as a jyrun, and I get an error when it is trying to connect to the device, in this case, the emulator.
How do I actually run the script?
Odile