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 whilemonkeyrunner ignores it. This snippet adds the components present inPYTHONPATH to sys.path and makes them visible tomonkeyrunner.
  4. Following, we need to locate AndroidViewClient which you should have added to the environment. This can be also added inEclipsein Run Configurations -> Environment.
    ANDROID_VIEW_CLIENT_HOME should point to your AndroidViewClient installation to the parent folder ofsrc. That is, if you have downloadedAndroidViewClient in /opt/AndroidViewClient and kept the same structure as the distribution, you should setANDROID_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.

说明:在Eclipse中打开Pydev,然后选择Interpreter MonkeyRunner,然后配置环境Environment,添加变量ANDROID_VIEW_CLIENT_HOME=/opt/AndroidViewClient/AndroidViewClient,见下图



转自:http://dtmilano.blogspot.ca/2012/09/monkeyrunner-importing-from-pythonpath.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值