将monkeyrunner导入python ViewClient

时间:2014-01-31 09:09:03

标签: android python monkeyrunner androidviewclient

我有兴趣使用MonkeyClunner的ViewClient扩展,遇到纯BeanRunner的Broken Pipe问题。

我还需要在device.press(...),device.touch(...),device.drag(...)函数中使用X / Y坐标进行某些测试(不是正常的)因为这个原因,我需要将MonkeyRunner导入到ViewClient Phyton脚本中 - 但是对于Android应用程序测试的一部分,我可以使用ViewClient来搜索heirarchy中的ID。除非有人知道如何在调用vc.touch()/ vc.drag()等之前在ViewClient中设置硬编码的X / Y?

因此,我的Python脚本有以下标题,它试图引入ViewClient环境和MonkeyRunner路径。但是,运行脚本始终会导致相同的错误:

  

ImportError:没有名为android.monkeyrunner的模块

设置环境的脚本部分以这种方式构建(Environment - Ubuntu 12.04 LTS)

#! /usr/bin/env python

# Import Class Files

import sys
import os

# 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.environ['ANDROID_VIEW_CLIENT_HOME'])
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
    sys.path.append(os.path.join(os.environ['ANDROID_SDK_ROOT'], 'tools/lib/monkeyrunner.jar'))
    sys.path.append(os.path.join(os.environ['ANDROID_SDK_ROOT'], 'tools/lib'))
except:
    pass

print sys.path

from com.dtmilano.android.viewclient import ViewClient, View
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

所以上面的输出如下所示:

mactwixs:~/AndroidViewClient/examples$ ./monkeyscript3.py
['/home/mactwixs/AndroidViewClient/examples', '/home/mactwixs/AndroidViewClient/examples', '/home/mactwixs/AndroidViewClient', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '', '/home/mactwixs/AndroidViewClient', '/home/mactwixs/AndroidViewClient/src', '/home/mactwixs/dev_env/ADT/sdk/tools/lib/monkeyrunner.jar', '/home/mactwixs/dev_env/ADT/sdk/tools/lib']
Traceback (most recent call last):
  File "./monkeyscript3.py", line 41, in <module>
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
ImportError: No module named android.monkeyrunner

谢谢!

更新:

有趣的是:

mactwixs:~/AndroidViewClient/examples$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path_hooks
[<type 'zipimport.zipimporter'>]
>>>

是否还有<type 'org.python.core.JavaImporter'>

UPDATE2:

所以区别在于:

mactwixs:~/AndroidViewClient/examples$ python
**Python** 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

mactwixs:~/AndroidViewClient/examples$ monkeyrunner
**Jython** 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35) 
[Java HotSpot(TM) 64-Bit Server VM (Sun Microsystems Inc.)] on java1.6.0_24
>>>

....所以让我尝试一下Jython!

UPDATE3:

不。现在它与ViewClient进行斗争,所以我回到原点...我找到了随ViewClient提供的check-import.py。出于这一点,我认为有些东西没有设置得恰到好处,因为monkeyrunner而不是python被称为viewclient.py&amp; adbclient.py ...

mactwixs:~/AndroidViewClient/examples$ monkeyrunner check-import.py --debug
sys.path= ['/home/mactwixs/dev_env/ADT/sdk/tools/lib/monkeyrunner.jar', '/home/mactwixs/AndroidViewClient/examples', '/home/mactwixs/dev_env/ADT/sdk/tools/lib/Lib', '/home/mactwixs/dev_env/ADT/sdk/tools/lib/jython-standalone-2.5.3.jar/Lib', '__classpath__', '__pyclasspath__/', '/home/mactwixs/AndroidViewClient/src']
/home/mactwixs/AndroidViewClient/src/com/dtmilano/android/viewclient.py:27: RuntimeWarning: 

    You should use a 'python' interpreter, not 'monkeyrunner' for this module


  warnings.warn(
/home/mactwixs/AndroidViewClient/src/com/dtmilano/android/adb/adbclient.py:26: RuntimeWarning: 

    You should use a 'python' interpreter, not 'monkeyrunner' for this module


  warnings.warn(
OK

1 个答案:

答案 0 :(得分:3)

我做了一些实验并且还查看了GIT回购并确定了我确实可以做拖累和放大触摸我需要但没有MonkeyRunner,纯粹用Python - 所以我的问题已经消失,因为我不必导入Monkey库。

import sys
import os
import time

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

from com.dtmilano.android.adb import adbclient
import com.dtmilano.android.viewclient as viewclient

device, serialno = viewclient.ViewClient.connectToDeviceOrExit(verbose=True)
device.drag((960,1497),(214,1496),0.15,10)

time.sleep(2)

device.touch(610, 1734, adbclient.DOWN_AND_UP)

time.sleep(2)