2013-02-28 57 views
1

我有一個使用monkeyrunner製作的腳本。 該腳本將安裝一個應用程序,對模擬器進行一些關鍵指示,然後卸載。在我得到的東西殼命令無響應例外,這將毀了我的腳本。在android模擬器中Shell命令無響應異常?

你知道我做錯了什麼導致這種例外。 這裏的錯誤消息

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb.AdbChimpDevice]錯誤開始命令: 猴--port 12345

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb.AdbChimpDevice] com.android.ddmlib.ShellCommandUnresponsiveException

130228 16:44:49.210 :我[pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice] at com.android。 ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb.AdbChimpDevice]在 com.android .ddmlib.Device.executeShellCommand(Device.java:453)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb.AdbChimpDevice]在 融爲一體。 android.chimpchat.adb.AdbChimpDevice $ 1.run(AdbChimpDevice.java:105)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb.AdbChimpDevice]在 個java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb。 AdbChimpDevice]在 java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:334)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat .adb.AdbChimpDevice]在 java.util.concurrent.FutureTask.run(FutureTask.java:166)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android。 chimpchat.adb.AdbChimpDevice] java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb.AdbChimpDevice]在 java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:603)

130228 16:44:49.210:I [池-1-螺紋-1] [com.android.chimpchat.adb .adbChimpDevice] java.lang.Thread.run(Thread.java:679)130228 16:46:25.631:I [main] [com.android.chimpchat.ChimpManager] Monkey Command:quit。

下面的代碼的後期

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

該代碼將有所幫助。你能補充一點嗎?並請說明你在哪一行得到例外。 – 2013-03-01 07:37:00

+0

這是一個ChimpChat錯誤 – 2013-03-01 18:59:58

+0

@dtmilano如果這是一個錯誤,我該怎麼做才能避免這個錯誤?我不應該使用哪些命令,以便我不會得到相同的錯誤?謝謝! – 2013-03-03 15:06:14

回答

0

類的答案,但MonkeyRunner是在谷歌的ddmlib的頂部可能運行。在ddmlib中,在設備上調用的所有shell命令的缺省超時值爲5000毫秒。如果進程在5000毫秒內沒有打印任何內容,則進程將被關閉,並引發ShellCommandUnresponsiveException。如果您的進程運行時間超過5000毫秒,您可能需要優化代碼,以避免在調用shell命令的時間內進行長時間處理。或者,您可以檢查MonkeyRunner API以查看是否有任何延長超時的方法。或者你可以簡單地連續輸出到shell以防止超時過期。