我正在尝试使用 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 之外的其他工具来解决这个问题。对此问题的任何帮助将不胜感激。