【发布时间】:2012-11-20 06:38:58
【问题描述】:
我正在尝试使用 monkeyrunner 来配置连接到同一台电脑的多个平板电脑。该代码适用于 1 台平板电脑,但当我尝试在多台平板电脑上运行它时,一切都崩溃了。
这里是调用 monkeyrunner python 文件的代码。 mr1.py 是我要运行的 monkeyrunner 文件。
import sys
import util
import threading
import commands
class myThread (threading.Thread):
def __init__(self, threadID, deviceId,env_path):
self.threadID = threadID
self.deviceId = deviceId
self.path = env_path
threading.Thread.__init__(self)
def run(self):
print "Starting " + self.deviceId
ret = commands.getstatusoutput(self.path+"monkeyrunner mr1.py "+self.deviceId)
print ret
print "Exiting " + self.deviceId
def main():
connected_devices = util.get_connected_devices()
count = 0
path = "/Users/ad/Desktop/android-sdk-macosx/tools/"
for device in connected_devices:
thread = myThread(count,device[0],path)
thread.start()
count = count + 1
if __name__ == "__main__":
main()
我看到了这篇博文,其中描述了 monkeyrunner 的比赛条件。我不确定这是否是导致问题的原因。
http://distributedreasoner.blogspot.com/2011/06/android-monkeyrunner-and-google-adb.html
我也尝试使用上述博文中提到的 MAML 库,但我仍然无法让 monkeyrunner 在多个设备上同时执行。这是实际的monkeyrunner代码。
import sys
import maml
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
deviceId = sys.argv[1]
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection(10.0,deviceId)
packagename = "com.android.settings"
classname = "com.android.settings.DisplaySettings" #SecuritySettings" #".DisplaySettings"
componentname = packagename + "/" + classname
device.startActivity(component=componentname)
maml.click(device,1088,300)
MonkeyRunner.sleep(0.4)
maml.click(device,864,361)
MonkeyRunner.sleep(0.4)
maml.click(device,612,621)
MonkeyRunner.sleep(0.5)
device.press ('KEYCODE_HOME', 'DOWN_AND_UP')
print "Exiting for device !" + deviceId
基于 Commonsware 的问题,我用以下顺序代码替换了线程代码,它似乎工作正常,但显然这不是最理想的情况。
for device in connected_devices:
print device[0]
ret = commands.getstatusoutput(path+"monkeyrunner mr1.py "+device[0])
print ret
因为 Android 不允许您以编程方式修改位置/语言设置等,而且我需要配置许多平板电脑来更改设置,所以直接的选择是使用 MonkeyRunner。有几点注意事项,我愿意使用除 monkeyrunner 之外的其他工具来解决这个问题。 对此问题的任何帮助将不胜感激。
【问题讨论】:
-
您是要在多台平板电脑上按顺序运行还是同时运行?您所说的“一切都爆炸了”究竟是什么意思?
-
同时。 “全部炸毁”我的意思是什么都没有发生。例如,在我粘贴的monkeyrunner代码中,它应该首先打开设置,然后单击自动旋转屏幕,然后将睡眠更改为始终开启。当我在连接到我的电脑的 2 台设备上运行它时,我注意到只打开了显示设置。其他点击没有发生。同样,每次我运行脚本时它的行为都不完全相同。有时,某些点击在 1 台设备上有效,但在其他设备上无效。
-
顺便说一句,我假设平板电脑是横向的。
标签: android python multithreading monkeyrunner