Monkey测试

文章详细介绍了如何使用adb命令、Monkey、MonkeyRunner以及Maxim工具进行Android应用的自动化Monkey测试,包括命令行参数、脚本编写、白名单与黑名单配置、截图和日志收集等,用于测试应用的稳定性和性能。
摘要由CSDN通过智能技术生成

Monkey测试


原生monkey

命令:

adb shell monkey -p com.pinsmedical.pinslife --throttle 100 -s 999 --pct-touch 50 --pct-motion 50  --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes -v -v -v 1000>C:\Users\admin\Desktop\Monkey\log\20230117.log

adb shell settings put global policy_control immersive.full=* 关闭手机的状态栏
adb shell settings put global policy_control null 开启手机状态栏

monkey script

命令:

#发送到手机
adb push C:\Users\admin\Desktop\work\learn\Monkey\MonkeyScript\j3.txt /data/local/tmp
#执行
adb shell monkey -f /data/local/tmp/j3.txt -v-v-v 3 >C:\Users\admin\Desktop\Monkey\log\monkeyscript_log.txt

monkey runner

命令:

#完全固定操作
monkeyrunner C:\Users\admin\Desktop\work\learn\Monkey\Monkeyrunner\monkeyrunner_test.py
#不完全固定操作
monkeyrunner C:\Users\admin\Desktop\work\learn\Monkey\Monkeyrunner\monkeyrunner_test1.py

录制:

monkeyrunner C:\Users\admin\Downloads\android-sdk-windows\platform-tools\monkey_recorder.py

回放:

monkeyrunner C:\Users\admin\Desktop\work\learn\Monkey\Monkeyrunner\monkey_playback.py C:\Users\admin\Desktop\work\learn\Monkey\Monkeyrunner\playback

maxim

前置步骤:

 adb push framework.jar /sdcard
 adb push monkey.jar /sdcard

命令:

#mix模式
adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.pinsmedical.pinslife --uiautomatormix --running-minutes 1 --throttle 500 -v -v
#troy模式
adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.pinsmedical.pinslife --uiautomatortroy --running-minutes 1 --throttle 500 -v -v

 # 获取xml
 adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.api.Dumptree
 adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.api.CurrentActivity
 adb shell dumpsys window w |findstr \/ |findstr name=

白名单(awl.strings):

#发送到手机上  adb push awl.strings /sdcard
#命令行添加  --act-whitelist-file /sdcard/awl.strings
com.pinsmedical.pinslife.base.CommonFragmentActivity
com.pinsmedical.pinslife.component.home.MainActivity
com.pinsmedical.pinslife.component.inquiry.InquiryListActivity
com.pinsmedical.pinslife.component.inquiry.InquiryMainActivity
com.pinsmedical.pinslife.component.inquiry.InquiryResultActivity
com.pinsmedical.pinslife.component.inquiry.order.InquiryOrderDetailActivity
com.pinsmedical.pinslife.component.myDoctor.DoctorInfoNewActivity


#对应的也有黑名单abl.strings

固定操作(max.xpath.actions):

//编写完成后 adb push max.xpath.actions /sdcard
//踩坑 事件不稳定!切换activity后才能执行,同一activity未实现执行多个event
//注意:首页启动需要延迟一会,否则可能无法找到元素,需要在max.config设置启动后延迟时间
//max.startAfterNSecondsofsleep = 6000

//复现药品无说明书,点击说明书崩溃
[
  {
    "prob": 1,
    "activity": "com.pinsmedical.pinslife.component.home.MainActivity",
    "actions": [
      {
        "xpath": "//*[@resource-id='com.pinsmedical.pinslife:id/tv_click3' and @text='续方购药']",
        "action": "CLICK",
        "throttle": 2000
      }
    ]
  },
  {
    "prob": 1,
    "activity": "com.pinsmedical.pinslife.component.buymedicine.ui.BuyMedicineMainActivity",
    "actions": [
      {
        "xpath": "//*[@resource-id ='com.pinsmedical.pinslife:id/nameTv' and @text='[dong22222gaigaigai]哒哒哒哒哒哒哒哒哒哒哒哒哒哒哒哒哒哒结束']",
        "action": "CLICK",
        "throttle": 2000
      },
      {
        "xpath": "//*[@class='android.widget.TextView' and @text='说明书']",
        "action": "CLICK",
        "throttle": 2000
      }
    ]
  }
]
//点击问医生-伍医生-图文问诊
[
  {
    "prob": 1,
    "activity": "com.pinsmedical.pinslife.component.home.MainActivity",
    "actions": [
      {
        "xpath": "//*[@resource-id='com.pinsmedical.pinslife:id/tv_click1' and @text='问医生']",
        "action": "CLICK",
        "throttle": 1000
      }
    ]
  },
  {
    "prob": 1,
    "activity": "com.pinsmedical.pinslife.component.inquiry.InquiryMainActivity",
    "actions": [
      {
        "xpath": "//*[@resource-id ='com.pinsmedical.pinslife:id/tv_name' and @text='伍医生']",
        "action": "CLICK",
        "throttle": 1000
      }
    ]
  },
  {
    "prob": 1,
    "activity": "com.pinsmedical.pinslife.component.myDoctor.DoctorInfoNewActivity",
    "actions": [
      {
        "xpath": "//*[@resource-id ='com.pinsmedical.pinslife:id/tv_inquiry_goods_name' and @text='图文问诊']",
        "action": "CLICK",
        "throttle": 1000
      }
    ]
  }
]

屏蔽区域(max.widget.strings):

//图文问诊流程 禁止点击隐私政策和服务协议
[
  {
     "activity": "com.pinsmedical.pinslife.base.CommonFragmentActivity",
     "xpath": "//*[@resource-id ='com.pinsmedical.pinslife:id/privacy' and @text='《隐私政策》']"
  },
  {
     "activity": "com.pinsmedical.pinslife.base.CommonFragmentActivity",
     "xpath": "//*[@resource-id ='com.pinsmedical.pinslife:id/protocol' and @text='《服务协议》']"
  }
]
//首页 禁止点击导航栏
[
  {
    "activity": "com.pinsmedical.pinslife.component.home.MainActivity",
    "bounds": "[0,1175][720,1263]"
  }
]

控件优先级(max.xpath.selector):

[
  {
    "firstList": [
      {
        "xpath": "//*[contains(@text,'下一步')]"
      }
    ],
    "selectList": [
      {
        "xpath": "//*[@clickable='true']"
      },
      {
        "xpath": "//*[@clickable='true']//*[contains(name(),'Text')]"
      },
      {
        "xpath": "//*[@clickable='true']//*[contains(name(),'Button')]"
      },
      {
        "xpath": "//*[@clickable='true']//*[contains(name(),'Image')]"
      }
    ],
    "lastList": [
      {
        "xpath": "//*[../*[@selected='true']]"
      },
      {
        "xpath": "//*[../../*/*[@selected='true']]"
      },
      {
        "xpath": "//*[../../*/*[@selected='true'] and contains(@resource-id,'tab_')]"
      }
    ]
  }
]

自定义输入(max.strings)

#需要提前安装ADBKeyBoard
# max.config需要设置max.randomPickFromStringList = true 
伍
伍医生
wu

截图,启动配置(max.config):

#截图
max.takeScreenShot = true
# 崩溃时截图:
--imagepolling
max.takeScreenShot =True  开启截图  
max.flushImagesThreshold  =50 回溯区间大小xx张  
崩溃截图的生效条件: throttle > 200  &&  max.takeScreenShot = true &&  --imagepolling

fastbot

前置条件:

adb push framework.jar fastbot-thirdpart.jar monkeyq.jar /sdcard
adb push libs/*  /data/local/tmp/

命令:

adb shell CLASSPATH=/sdcard/monkeyq.jar:/sdcard/framework.jar:/sdcard/fastbot-thirdpart.jar exec app_process /system/bin com.android.commands.monkey.Monkey -p com.pinsmedical.pinslife --agent reuseq --running-minutes 30 --throttle 500 -v -v --output-directory /sdcard/monkeypng>C:\Users\admin\Desktop\Monkey\log\monkeylog.txt

--act-whitelist-file /sdcard/awl.strings

adb shell CLASSPATH=/sdcard/monkeyq.jar:/sdcard/framework.jar:/sdcard/fastbot-thirdpart.jar exec app_process /system/bin com.android.commands.monkey.Monkey -p com.pinsmedical.pinsdoctor --agent reuseq --running-minutes 30 --throttle 500 -v -v --output-directory /sdcard/monkeypng >C:\Users\admin\Desktop\Monkey\log\monkeylog.txt

adb logcat -c 
adb logcat -b main >C:\Users\admin\Desktop\Monkey\log\logcat.txt



Jenkins集成

monkey_fastbot

call 
cd C:\Users\admin\Desktop\Monkey
set a="%device_name%-%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%"
md %a%
adb logcat -c
start "" cmd /c adb logcat ^>C:\Users\admin\Desktop\Monkey\%a%\logcat.txt
adb -s %device_name% shell settings put global policy_control immersive.full=*
adb -s %device_name% shell CLASSPATH=/sdcard/monkeyq.jar:/sdcard/framework.jar:/sdcard/fastbot-thirdpart.jar exec app_process /system/bin com.android.commands.monkey.Monkey -p %package% --running-minutes %duration% --throttle %throttle% %white_list% --agent reuseq -v -v -v -v -v --output-directory /sdcard/max-output 
adb pull /sdcard/max-output $WORKSPACE
adb pull /sdcard/crash-dump.log $WORKSPACE
adb pull /sdcard/oom-traces.log $WORKSPACE
exit 0

monkey_maxim

call
cd C:\Users\admin\Desktop\Monkey
set a="%device_name%-%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%"
md %a%
adb logcat -c
start "" cmd /c adb logcat ^>C:\Users\admin\Desktop\Monkey\%a%\logcat.txt
adb -s %device_name% shell settings put global policy_control immersive.full=*
adb -s %device_name% shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p %package_name% --running-minutes %duration% --throttle %throttle% %white_list% --uiautomator%model% -v -v -v -v -v  --output-directory /sdcard/max-output
adb pull /sdcard/max-output $WORKSPACE
adb pull /sdcard/crash-dump.log $WORKSPACE
adb pull /sdcard/oom-traces.log $WORKSPACE
exit 0

config

call
adb -s %device_name%
adb shell rm  /sdcard/max.xpath.actions
adb shell rm  /sdcard/max.xpath.selector
adb shell rm  /sdcard/awl.strings
adb shell rm  /sdcard/max.config
adb shell rm  /sdcard/max.strings
adb shell rm  /sdcard/max.widget.black
exit 0

call
adb -s %device_name%
adb push C:\Users\admin\Desktop\work\learn\Monkey\config\%action% /sdcard
adb push C:\Users\admin\Desktop\work\learn\Monkey\config\%awl% /sdcard
adb push C:\Users\admin\Desktop\work\learn\Monkey\config\%selector% /sdcard
adb push C:\Users\admin\Desktop\work\learn\Monkey\config\%widget% /sdcard
adb push C:\Users\admin\Desktop\work\learn\Monkey\config\%string% /sdcard
adb push C:\Users\admin\Desktop\work\learn\Monkey\config\%config% /sdcard
exit 0

Activity名称

嘉医有品

com.pinsmedical.pinslife.component.inquiry.InquiryMainActivity问医生
com.pinsmedical.pinslife.component.inquiry.InquiryListActivity问医生搜索
com.pinsmedical.pinslife.component.inquiry.InquiryResultActivity搜索结果页
com.pinsmedical.pinslife.component.myDoctor.DoctorInfoNewActivity医生详情页
com.pinsmedical.pinslife.base.CommonFragmentActivity问诊过程页
com.pinsmedical.pinslife.component.inquiry.order.InquiryOrderDetailActivity订单详情页
com.pinsmedical.pinslife.component.home.MainActivity主页
  adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.pinsmedical.pinslife --uiautomatorapi --running-minutes 10 -v -v -v -v



adb devices 查看连接设备

# 1、将日志保存在自己的电脑上
adb shell monkey -f /sdcard/wdj.txt -v 1 >/存放路径/wdjj.txt
 
# 2、将日志保存在设备上面
第一步:adb shell 
第二步:root@android:/ # monkey -f /sdcard/wdj.txt -v 1 >/sdcard/wddj.txt
 
# 3、将日志中的标准流与错误流进行分开保存(1>保存的是标准流的日志信息,2>保存的是错误流的日志信息)
adb shell monkey -f /sdcard/wdj.txt -v 1 1>/存放路径/right.txt 2>/存放路径/error.txt
 

连接模拟器
adb connect 127.0.0.1:62001


adb shell monkey 200 针对手机进行200次随机事件
adb shell monkey -p 包名 200 对某个包进行200次的事件操作

查看第三方包名
adb shell pm list packages -3  或 abd shell am monitor 或adb shell dumpsys 


执行中发现了以下问题会结束APP。重新开始启动执行操作,继承次数。日志中记录
--ignore-crashes 崩溃 
--ignore-timeouts  ANR 超时


遇到了直接忽略,不报错不记录
--ignore-security-exceptions   安全证书


 序列值,种子值 2次monkey命令执行一样的seed值,那么操作流程(点击、滑屏)一致,但是操作对象不一定一致
-s SEED(数字)

日志级别程度 
-v -v -v

延迟时间(450ms)
--throttle MILLISEC

monkey测试结束
monkey finished
1.crash崩溃   logcat日志
2.anr无响应   abd shell  /data/anr/traces.txt
3异常中断

日志重定向
adb logcat >D:\logcat\logcat_2022-12-3.txt

记录特定的事件类型
adb logcat -b (radio、main、event)  >D:\logcat\logcat_2022-12-3.txt



日志组成:
1时间戳 adb logcat -v time
2优先级
V 开发调试用的
D 调试信息
I info 提示信息
W 警告
E 错误error
3标签(tag)   开发过滤筛选用 例如:ActivityManager
4PID 进程ID
5正文 主体内容


adb shell dumpsys window w |findstr \/ |findstr name=
adb shell dumpsys window | findstr mCurrentFocus
am force-stop com.pinsmedical.pinslife

日志分析:
/data/system/dropbox

 adb -s emulator-5554 shell CLASSPATH=/sdcard/monkeyq.jar:/sdcard/framework.jar exec app_process /system/bin com.android.commands.monkey.Monkey -p com.pinsmedical.pinslife --agent robot --running-minutes 5 --throttle 800 -v -v

adb shell settings put global policy_control immersive.full=* 关闭手机的状态栏
adb shell settings put global policy_control null 开启手机状态栏



adb shell monkey -p com.pinsmedical.pinslife --throttle 300 -s 999 --pct-touch 50 --pct-motion 50  --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 1000>C:\Users\admin\Desktop\Monkey\log\20230117.log

adb shell monkey -p com.pinsmedical.pinsdoctor --throttle 300  --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 1000>C:\Users\admin\Desktop\Monkey\log\2023011711111.log

com.pinsmedical.pinsdoctor
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值