我有一个使用monkeyrunner制作的脚本。 该脚本将安装一个应用程序,为模拟器做一些关键的预设然后卸载。在某些事情中,我得到 Shell命令无响应异常,这将破坏我的脚本。
你知道我做错了什么导致这种异常。 这是错误信息
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]启动命令时出错: monkey --port 12345
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice] com.android.ddmlib.ShellCommandUnresponsiveException
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 com.android.ddmlib.Device.executeShellCommand(Device.java:453)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 com.android.chimpchat.adb.AdbChimpDevice $ 1.run(AdbChimpDevice.java:105)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:334)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 java.util.concurrent.FutureTask.run(FutureTask.java:166)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:603)
130228 16:44:49.210:我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]来了 java.lang.Thread.run(Thread.java:679)130228 16:46:25.631:我[主要] [com.android.chimpchat.ChimpManager] Monkey命令:退出。
这是代码
def getTimeNow():
timeStamp = ""
timeStamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return timeStamp
def saveScreenshot(screenshot, apkID, Code, path):
fName = "".join([path, '/', Code, apkID, '.png'])
screenshot.writeToFile(fName,'png')
def found(screenshot, ScreenshotRef, apkPack):
Result = "N"
acceptance = 0.9
imageCap = ScreenshotRef + 'sshot'
imageRef = MonkeyRunner.loadImageFromFile(imageCap)
imageTuple = 78, 447, 547, 64
#imageTuple = 16, 177, 64, 60
ref = imageRef.getSubImage(imageTuple)
y=0
while y < 1280 - 64:
imageTuple2 = 78, int(y), 547, 64
if screenshot.getSubImage(imageTuple2).sameAs(ref, acceptance):
detResult = "B"
uninAppMk(apkPack)
MonkeyRunner.sleep(15)
device.press("KEYCODE_ESCAPE", MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(15)
device.press("KEYCODE_HOME", MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(15)
print "Found at points 78," + str(y)
break
y=y+1
return Result
def uninAppMk(apkPack):
device.removePackage(apkPack)
print "Package Uninstalled."
def writeToLogFile(logFile, Code, apkID, apkName, status, timeStamp):
logText = "\t".join([Code, apkID, apkName, status, timeStamp, '\n'])
db = zxJDBC.connect("jdbc:mysql://" + server + ":" + str(portnum) + "/" + schema, username , password, "com.mysql.jdbc.Driver")
c = db.cursor()
mysql_output = c.execute("INSERT INTO table VALUES(?, ?, ?, ?, ?)",(Code, status, timeStamp, apkID , "0"))
db.commit()
print "mysql error output => " + str(mysql_output)
f = open(logFile, "a")
f.write(logText)
f.close()
#+++++ ######## START HERE ######## ++++++#
#+++++ Connect to emulator ++++++#
timeout = 120
device = MonkeyRunner.waitForConnection()
MonkeyRunner.sleep(10)
#+++++ Getting CMD parameters ++++++#
apkID = sys.argv[1]
apkName = sys.argv[2]
apkPack = sys.argv[3]
#+++++ Read config file ++++++#
ConfigFileName = os.path.dirname(os.path.realpath(__file__)) + '/config.ini'
cp = ConfigParser.ConfigParser()
cp.read(ConfigFileName)
adbLoc = cp.get('tools','adb')
Code = cp.get('product','prodcode')
outputFolder = cp.get('output','App')
apkPath = cp.get('source','App')
ScreenshotRef = cp.get('source','ScreenshotRef')
logFile = outputFolder + 'logs.txt'
#----------read database section----------#
server = cp.get('database','server')
portnum = int(cp.get('database','port'))
username = cp.get('database','username')
password = cp.get('database','password')
schema= cp.get('database','schema')
#+++++ Check if output folder exist ++++++#
print "All screenshots will be found here " + outputFolder
if not os.path.exists(outputFolder):
os.makedirs(outputFolder)
#++++++ Test Starts ++++++#
print "APK ID: " + apkID
print "APK Name: " + apkName
print "APK Package Name: " + apkPack
print "APK Path: " + apkPath
apkIns = apkPath + apkName
print "APK:: " + apkIns
try:
device.installPackage(apkIns)
except(SocketException):
print "~~~~Error installing"
MonkeyRunner.sleep(95)
#++++++ Take screenshot ++++++#
screenshot = device.takeSnapshot()
MonkeyRunner.sleep(10)
timeStamp = getTimeNow()
saveScreenshot(screenshot, apkID, Code, outputFolder)
MonkeyRunner.sleep(3)
#++++++ Check detection ++++++#
status = found(screenshot, ScreenshotRef, apkPack)
if status == 'N':
uninAppMk(apkPack)
#++++++ Write to log file ++++++#
writeToLogFile(logFile, Code, apkID, apkName, status, timeStamp)
答案 0 :(得分:1)
有点迟到的答案,但MonkeyRunner可能在谷歌的ddmlib上运行。在ddmlib中,在设备上调用的所有shell命令都具有5000毫秒的默认超时。如果进程没有为该5000毫秒打印任何内容,则将关闭该进程并抛出ShellCommandUnresponsiveException。如果您的进程运行时间超过5000毫秒,您可能希望优化代码以避免在调用shell命令的时间内进行长时间处理。或者,您可以检查MonkeyRunner API以查看是否有任何延长超时的方法。或者你可以简单地以某种方式连续输出到shell以防止超时到期。