• 【初学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()

    感觉还能继续优化,做个记录。

  • 相关阅读:
    20160205.CCPP体系详解(0015天)
    盘点各专业到古代都能干些啥
    一份简单的在 Linux下编译及调试 C 代码的指南
    WordPress Gravatar国内加载缓慢解决办法
    linux修改mysql字符集编码
    阿里合伙人邵晓锋:什么是创业者应该抵制的诱惑?
    6月27日云栖精选夜读:细数智能家居的痛点
    6月26日云栖精选夜读:成为一名Java高级工程师你需要学什么
    搭建百万级别邮件发送平台
    黑科技实验室:这些高级装备厉害了!
  • 原文地址:https://www.cnblogs.com/levelksk/p/3387623.html
Copyright © 2020-2023  润新知