我想运行monkeyrunner python脚本。
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from modjy.modjy_params import INTEGER
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
easyDevice = EasyMonkeyDevice(device)
MonkeyRunner.sleep(2)
easyDevice.touch(By.id('id/btnSignUp'),MonkeyDevice.DOWN_AND_UP)
以下是我用来运行脚本的命令
c:\path_to_and_sdk_tool> monkeyrunner test.py
但是当我得到" EasyMonkeyDevice"对象
File "C:\python\test.py", line 19, in <module>
easyDevice = EasyMonkeyDevice(device)
java.lang.RuntimeException: Could not connect to the view server
at com.android.chimpchat.hierarchyviewer.HierarchyViewer.setupViewServer
(HierarchyViewer.java:57)
at com.android.chimpchat.hierarchyviewer.HierarchyViewer.<init>(Hierarch
yViewer.java:43)
at com.android.chimpchat.adb.AdbChimpDevice.getHierarchyViewer(AdbChimpD
evice.java:95)
at com.android.monkeyrunner.easy.EasyMonkeyDevice.<init>(EasyMonkeyDevic
e.java:64)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.python.core.PyReflectedConstructor.make(PyReflectedConstructor.ja
va:67)
at org.python.core.PyJavaType$1.new_impl(PyJavaType.java:517)
at org.python.core.PyType.invokeNew(PyType.java:466)
at org.python.core.PyType.type___call__(PyType.java:1558)
请分享我可以从哪里获得EasyMonkeyDevice导入的链接
答案 0 :(得分:1)
您必须实现ViewServer才能使用EasyMonkeyDevice: https://github.com/romainguy/ViewServer 因为easyMonkey使用Hierarchy Viewer。
在您的活动中:
为层次结构查看器启用ViewServer,如下所示:
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewServer.get(this).addWindow(this);
}
public void onDestroy() {
super.onDestroy();
ViewServer.get(this).removeWindow(this);
}
public void onResume() {
super.onResume();
ViewServer.get(this).setFocusedWindow(this);
}
}
答案 1 :(得分:0)
或者,您可以使用 A ndroidViewClient ,但您不会被迫在您的应用中实施ViewServer
。
这将与您之前的脚本相同:
#! /usr/bin/env python
from com.dtmilano.android.viewclient import ViewClient
ViewClient(*ViewClient.connectToDeviceOrExit()).findViewByIdOrRaise('id/btnSignUp').touch()
您也可以通过运行culebra -UG
(在AndroidViewClient工具中)生成此类脚本或单元测试,然后单击设备的UI表示以自动生成touch()
调用。