#Coding=utf-8ImportOSImport Time#Import monkeyrunner Three module fromCom.android.monkeyrunnerImportMonkeyrunner, Monkeydevice, Monkeyimageruncomponent="com.dest.mr/.ui.home.homelaunchernew"Path='d:/pic/'LogPath='d:/log/'#new log file.txtLog = open (LogPath +'dest'+". txt",'W')#Connection Devicedevice =monkeyrunner.waitforconnection ()if notDevice:log.write ('connection failed \ n') Sys.exit (1)Else: Log.write ('connection passed \ n') device.startactivity (component=runcomponent) Time.sleep (5)#Device.shell ("Logcat | grep ' com.dest.mr ' > sdcard/destlog.txt ")#Os.system ("adb logcat | findstr" com.dest.mr ">> d:/log/logcat.txt") forIinchRange (3): #Save Log to File.txtLog.write ('start app ....'+ STR (i) +'\ n') Desthome=device.takesnapshot () desthome.writetofile (Path+'Desthome'+ STR (i) +'. PNG','PNG') #Import Home Pictures for comparing the resultsHometure = Monkeyrunner.loadimagefromfile ('D:/pic/tdesthome.png') if(Desthome.sameas (hometure,0.9)): Print 'desthome pic is the same \ n'Log.write ('test passed \ n') Else: Print 'test byebye \ n'Log.write ('test failed \ n') #Open UserInfoDevice.touch (850,68,'down_and_up') Time.sleep (2) UserInfo=device.takesnapshot () userinfo.writetofile (Path+'UserInfo'+ STR (i) +'. PNG','PNG') Ufoture= Monkeyrunner.loadimagefromfile ('D:/pic/tuserinfo.png') if(Userinfo.sameas (ufoture,0.75)): Print 'userinfo pic is the same \ n'Log.write ('userinfo test passed \ n') Else: Print 'userinfo pic is not the same'Log.write ('userinfo test failed \ n') #Click Mobile, into MyInfo pageDevice.touch (550,200,'down_and_up') Time.sleep (2) MyInfo=device.takesnapshot () myinfo.writetofile (Path+'MyInfo'+ STR (i) +'. PNG','PNG') Device.drag (500,550), (300,250), 0.5) Time.sleep (2) device.press ('Keycode_back','down_and_up') Time.sleep (2) Device.touch (60,80,'down_and_up') Time.sleep (2)
Android Automation Test-Monkeyrunner (iii) handy Practice test scripts