安卓 monkeyrunner笔记
安卓下载各种tools的网站,很赞。
AndroidDevTools - Android开发工具 Android SDK下载 Android Studio下载 Gradle下载 SDK Tools下载
monkeyrunner命令语法
monkeyrunner -plugin <plugin_jar> <programe_filename> <programe_option>
# -*- coding: utf-8 -*- # @Time : 5:46 PM # @Athor : WuHe # @File : MonkeyrunnerRandompress.py from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage import random,time device = MonkeyRunner.waitForConnection() if not device: print('Please connect a device to start') else: print('start test') file_object=open(r'C:\Users\WHE9SZH\Desktop\New\test.txt','w+') num = 0 while num < 20: x = random.randint(0, 1920) y = random.randint(0, 720) device.touch(x, y, 'DOWN_AND_UP') print(x) print(y) MonkeyRunner.sleep(0.5) num+=1 file_object.write(str('value: ')+str(x)+' '+str(y)+'\n') file_object.write(str('num=')+str(num)+'\n') file_object.write(time.asctime(time.localtime(time.time()))+'\n') file_object.write('logcat -v time *:W' + '\n')#没看懂这个log什么原理,还在研究 file_object.close() print('MonkeyRunner Complete')
启动monkeyrunner recorder的py程序:
#-*- coding: utf-8 -*- #@Time : 1/29/2022 #@Athor : WuHe #@File : monkeyrecorder.py #这里的monkey_recorder.py需要拷贝到和monkeyrunner同级目录下,即sdk/tools from com.android.monkeyrunner import MonkeyRunner as mr from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder device = mr.waitForConnection() recorder.start(device)
monkey 记录
白名单: monkey --pkg-whitelist-file /data/whitelist.txt --ignore-crashes --ignore-timeouts --ignore-security-exceptions --monitor-native-crashes --ignore-native-crashes --pct-touch 80 --pct-motion 5 --pct-appswitch 10 --pct-syskeys 0 --throttle 500 -v 10000000 黑名单: monkey --pkg-blacklist-file /data/blacklist.txt --ignore-crashes --ignore-timeouts --ignore-security-exceptions --monitor-native-crashes --ignore-native-crashes --pct-touch 80 --pct-motion 5 --pct-appswitch 10 --pct-syskeys 0 --throttle 500 -v 10000000 bat脚本 @echo off echo "********************************************************************************************************" echo "Please input the device ID,for example: 1234567" echo "********************************************************************************************************" set /p ADB_ID= title Monkey %ADB_ID% if %ADB_ID%==12345671 (color a0) if %ADB_ID%==12345672 (color b0) if %ADB_ID%==12345673 (color c0) python monkey_monitor_blacklist.py %ADB_ID% pause python脚本,cmd和txt的log import subprocess import logging import datetime import time import sys adb_id = sys.argv[-1] logging.basicConfig(filename='monkey_monitor_{}.log'.format(adb_id),filemode='w',level=logging.INFO) start_time = time.time() p = subprocess.Popen("adb -s {} shell \"monkey --pkg-blacklist-file /data/blacklist.txt --ignore-crashes --ignore-timeouts --ignore-security-exceptions --monitor-native-crashes --ignore-native-crashes --pct-touch 80 --pct-motion 10 --pct-syskeys 0 --throttle 500 -v -v -v 10000000\"".format(adb_id), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: out = p.stdout.readline() out = out.decode("utf-8").strip() if not out: continue now_ts = time.time() dt = str(datetime.datetime.now()) last = now_ts - start_time hour = round(last/60/60, 2) logging.info("{} - {} - lasted: {}s <-> {}h".format(dt, out, last, hour)) print("{} - {} - lasted: {}s <-> {}h".format(dt, out, last, hour)) logging.info("monkey exited")