【初学python】使用python调用monkey测试
目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下:
1 import os 2 apk = {'j': 'com.***.test1', 3 'h': 'com.***.test2'} 4 print 'enter \'j\' to test test1' 5 print 'enter \'h\' to test test2' 6 #choose apk 7 while True: 8 9 test_apk = raw_input('which apk do you want to test?\n(\'h\' or \'j\'):') 10 try: 11 apk_name = apk[test_apk] 12 except KeyError: 13 print 'Please enter \'j\' or \'h\'! - -#' 14 else: 15 break 16 #check the input value 17 while True: 18 event_num = raw_input('How many pseudo random events(-v) do you want?\nenter int num(>0):') 19 if event_num.isdigit() and int(event_num) > 0: 20 print 'OK, your events are ' + event_num 21 break 22 else: 23 print 'Please enter a number and the number > 0' 24 #check the input value 25 while True: 26 for_time = raw_input('How many times monkey test do you want?\ntimes(>1):') 27 if for_time.isdigit() and int(for_time) > 1: 28 print 'OK, you want to loop ' + for_time + 'times monkey test' 29 break 30 else: 31 print 'Please enter a number and the number > 1' 32 #the log path 33 log_path = 'D:\\' 34 #the log name 35 log_name = 'monkeytestlog.txt' 36 #monkey shell script 37 monkey_shell = 'adb shell monkey -v -v -v -p '+ apk_name+ ' -v ' + event_num + ' >'+log_path 38 39 def monkeytest(): 40 print 'now let\'s check your phone' 41 phonedevice = os.popen('adb devices').read() 42 if phonedevice.strip().endswith('device'): 43 print 'OK, your phone get ready,let\'s start moneky test!' 44 for i in range(1, int(for_time)+1): 45 print 'The', i, 'monkey test starting...' 46 os.system(monkey_shell+str(i)+log_name) 47 print i, 'complete!' 48 print 'OK, moneky test all complete! The log is in D:\\' 49 else: 50 print 'please check your phone has linked your computer well' 51 52 #find 'adb' command at your os 53 sysPath = os.environ.get('PATH') 54 if not sysPath.find('platform-tools'): 55 print '''please install the android-sdk and put the 'platform-tools' dir in your system PATH''' 56 else: 57 58 #kill the 'tadb.exe' 59 tadb = os.popen('tasklist').read() 60 if tadb.find('tadb.exe') != -1: 61 print 'Find \'tadb.exe\', it must be killed!!!!!!' 62 os.system('taskkill /im tadb.exe /F') 63 print 'OK,the \'tadb.exe\' has been killed, let\'s go on' 64 monkeytest() 65 else: 66 print 'not find \'tadb.exe\',great! go on!' 67 monkeytest()
感觉还能继续优化,做个记录。